Search code examples
javaspringportletspring-portlet-mvc

Java - Spring - portlet API @Controller how to read ajax parameters


I have following latest jquery v aajx call with multiple parameters.

In java code i am able to get only first parameter value rest of them found null.

$.ajax({
        url : '<portlet:resourceURL id="entitledRequest"/>',
        data: '<c:out value="pfx= ${account.accountNumber}-${outletUI.outlet}-${count}&acc=${account.accountNumber}" />',
        cache: false,
        success : function(result) {

            //targetElem.html(result); update uI

        }
    });

Following is my java code only first parameter is not null after that all params are null, i have debug http request all parameters are present in the request object any clue whats wrong here ?

Controller("ajaxRequestController")
@RequestMapping(value = "VIEW")
public class AjaxRequestController implements PortletConfigAware  {


@ResourceMapping("entitledRequest")
    public void getServiceAutoComplete(ResourceRequest request, ResourceResponse response) throws IOException {


            String elemPrefix = request.getParameter("pfx");
            String acc = request.getParameter("acc"); // found null
            String mac = request.getParameter("mac"); // found null

}...

Solution

  • Use @RequestParam annotation to read the Parameters.

    @ResourceMapping("entitledRequest")
    public void getServiceAutoComplete(@RequestParam("pfx")  String elemPrefix, @RequestParam  String acc, @RequestParam String mac, ResourceRequest request, ResourceResponse response) throws IOException {
    
    }
    

    Reason:

    The Parameters sent by Ajax doesn't fall in the PortletNamespace. Hence, they are not part of the ResourceRequest but they can be found in normal HTTPServletRequest. Spring searches the @RequestParam names in HTTPServletRequest.

    You can either use the approach suggested by me -or- you can add the <portlet:namespace/> to your parameters in the Ajax call keeping your Controller code intact.