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

家住魔仙堡 提交于 2019-12-10 18:24:47

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/32361170/spring-mvc-dynamic-param-name-or-param-name-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!