0

I am sending an AJAX request to a Spring MVC controller, like this:

var filters = {
    visual: visual, 
    Name: params['Name'], 
    Address: params['Address'], 
    documentType: params['documentType'], 
    itemQualifier: getItemQualifier(),
    itemIdentifier: params['itemIdentifier'], 
    currency: getCurrency(), 
    sellSide: getSellSide()
};

$.ajax({                                                                    
    type: "POST",
    url: "/reports/savefilters",
    data: filters,
    success: function(response) {
        alert('filters saved successfully: ' + response);
    },
    error: function(e) {
        alert('Error while saving filters: ' + e.message);
    }
});
@RequestMapping(value = "/savefilters", method = RequestMethod.POST)
public @ResponseBody String saveFilters(@RequestParam("visual") String visual, @RequestParam(value = "Name[]", required = false) String[] Name, 
        @RequestParam(value = "Address[]", required = false) String[] Address, @RequestParam(value = "documentType[]", required = false) String[] documentType, 
        @RequestParam("itemQualifier") String itemQualifier, @RequestParam(value = "itemIdentifier[]", required = false) String[] itemIdentifier, @RequestParam("currency") String currency,
        @RequestParam("sellSide") String sellSide, HttpServletRequest request)
{
    try
    {
        System.out.println("saveFilters");
        new DisplayService().saveFilters("ID", visual, Name, Address, documentType, itemQualifier, itemIdentifier, currency, sellSide);
        return "success";
    }
    catch (Exception ex)
    {
        return "error occured while inserting/updating db";
    }
}

I want to pass all these arguments as a class instead of sending like this.

How can I pass as a class from an AJAX call and how do I receive it in the Spring MVC controller code? Any suggestions please? Thanks.

Hemachandra
  • 69
  • 2
  • 14

1 Answers1

1

You can use the request object and using it you can get all the params as below.

public @ResponseBody String saveFilters(HttpServletRequest request){

String visual = request.getParameter("visual")

}
Tharsan Sivakumar
  • 6,351
  • 3
  • 19
  • 28
  • Thanks for the response. It is working with single values like visual but not with array parameters like name. I tried to get array values using request.getParameterValues() method also but couldn't get. – Hemachandra Jul 09 '16 at 17:36
  • For the array, you have to use the to get the array of params and then process it. Map parameterMap = request.getParameterMap(); http://stackoverflow.com/questions/7312436/spring-mvc-how-to-get-all-request-params-in-a-map-in-spring-controller. If it resolves issue please do accept the answer as correct:) – Tharsan Sivakumar Jul 09 '16 at 18:04
  • This works when key and value are both Strings. value is coming as null If value is array like partnerName. – Hemachandra Jul 11 '16 at 10:10
  • Did you try to get like this for arrays. Map parameterMap = request.getParameterMap(); – Tharsan Sivakumar Jul 11 '16 at 10:23
  • yes. I tried like that. Map parameterMap = request.getParameterMap(); if(parameterMap != null){ System.out.println(parameterMap.size()); System.out.println(parameterMap.get("KPI").toString()); System.out.println(parameterMap.get("partnerName")); } – Hemachandra Jul 11 '16 at 10:29
  • What's the partnetName. i.e How the array looks like ? – Tharsan Sivakumar Jul 11 '16 at 10:30
  • Sorry. It is Name. It is typo. Array values are comma separated. I am able to see all the names when I print client(JS) side. Spring mvc is giving this value as null – Hemachandra Jul 11 '16 at 10:44
  • Did you try something like this ? Map map = request.getParameterMap(); for(Object key : map.keySet()){ Object value = map.get(key); }. – Tharsan Sivakumar Jul 11 '16 at 10:49
  • Here both key and values are Objects, but if you know the data types of key and values, then you can cast like that – Tharsan Sivakumar Jul 11 '16 at 10:49
  • Hi Hemachandra, is it working ? You can loop through all of the keys in the array and get those values – Tharsan Sivakumar Jul 11 '16 at 11:08
  • yeah Sivakumar. I tried all these things but they are not working. – Hemachandra Jul 11 '16 at 11:17
  • Can you post your array object that you are posting from client end ? – Tharsan Sivakumar Jul 11 '16 at 11:20
  • ["NEWPHASE1", "Supply", "PERF_1", "PERF_2", "Rajesh_B_2", "SBD"] – Hemachandra Jul 11 '16 at 12:40