S Aditya
S Aditya

Reputation: 13

Passing a Parameter from .js file to controller

I'm trying to pass a parameter from my .js file to controller.

"fnCreatedRow" : function(nRow, aData, iDataIndex) {
            $(nRow).attr("row-id", aData["channelRole"]);
            $(nRow).hover(function(event) {
                var row = $(event.target.parentNode);
                row.addClass('row-selected');
            }, function(event) {
                var row = $(event.target.parentNode);
                row.removeClass('row-selected');
            });
            $(nRow).click(function(event) {
                var row = $(event.target.parentNode);
                document.location.href = "role-service-search.json?id=" + row.attr("row-id");
            });
        }

I need to pass the role to the controller code below. But when I debug, the value of role in controller is null. The code below has to work when i dont pass the role as well as when i pass the role through above script.

@RequestMapping(value = "/role-service-search", method = RequestMethod.GET )
public void searchRoleService(@ModelAttribute("request") final PageableRoleServiceSearchRequest request, final String role,
                   final BindingResult result) {
    if (request.getInput() == null) {
        RoleServiceSearchRequest newReq= new RoleServiceSearchRequest();
        if(role!=null)
        {
            newReq.setChannelRole(role);
        }

        request.setInput(newReq);
    }
}

Upvotes: 1

Views: 276

Answers (1)

Ahmad Shahwan
Ahmad Shahwan

Reputation: 1957

You need to annotate method argument role with @RequestParam to tell the framework how to map the query parameter.

@RequestMapping(value = "/role-service-search", method = RequestMethod.GET )
public void searchRoleService(
                   @ModelAttribute("request") final PageableRoleServiceSearchRequest request,
                   @RequestParam(name = "id", required = false) final String role,
                   final BindingResult result) {
    if (request.getInput() == null) {
        RoleServiceSearchRequest newReq= new RoleServiceSearchRequest();
        if(role!=null)
        {
            newReq.setChannelRole(role);
        }

        request.setInput(newReq);
    }
}

Since you call it id in your URL, you have to provide the name of role parameter as id. Otherwise, you can rename the parameter in your URL.

Upvotes: 1

Related Questions