Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: Dynamic Param Name or Param Name using Regex?

I'm trying out this grid component called jQuery Bootgrid. In AJAX mode, it POSTs parameters to the server and the one related to sorting is sent like this:

sort[colname]=desc

The colname part changes depending on how you sort the grid.

Is there any way in Spring MVC using @RequestParam to capture that sort param?

For example, something like:

@RequestParam("sort[{\\*}]") Map<String, String> sort

That's just a wild guess and I doubt there is any clean way to do it. Any suggestions on how to handle it would be great.


Update: Also tried this simpler version which I actually thought might work

@RequestParam("sort") Map<String, String> sort
like image 419
Dave L. Avatar asked Feb 27 '26 02:02

Dave L.


1 Answers

See on bootgrid forum: https://github.com/rstaib/jquery-bootgrid/issues/111

It is really silly but because cannot parse dynamic parameter on server side, you need to create new request parameters from the sort parameter by defining requestHandler in your bootgrid configuration in the following way:

requestHandler: function (request) {
  if (request.sort) {
    request.sortBy = Object.keys(request.sort)[0]; //this only gets first sort param
    request.sortDir = request.sort[request.sortBy];
    delete request.sort
  }
  return request;
}

And in Spring Controller:

@RequestParam(value = "sortBy", required = false) final String sortBy,
            @RequestParam(value = "sortDir", required = false) final String sortDir

Do not forget to mark these parameters as not required because sort is not always posted to server side.

like image 157
Rómeó Nagy Avatar answered Feb 28 '26 16:02

Rómeó Nagy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!