Spring MVC:动态参数名还是使用Regex的参数名? [英] Spring MVC: Dynamic Param Name or Param Name using Regex?
问题描述
我正在试用名为 jQuery Bootgrid 的网格组件.在AJAX模式下,它将POST
的参数发送给服务器,并与排序相关的参数发送如下:
I'm trying out this grid component called jQuery Bootgrid. In AJAX mode, it POST
s parameters to the server and the one related to sorting is sent like this:
sort[colname]=desc
colname
部分会根据您对网格的排序方式而变化.
The colname
part changes depending on how you sort the grid.
Spring MVC中是否可以使用@RequestParam
捕获该sort
参数?
Is there any way in Spring MVC using @RequestParam
to capture that sort
param?
例如,类似:
@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
推荐答案
在bootgrid论坛上查看: https://github.com/rstaib/jquery-bootgrid/issues/111
See on bootgrid forum: https://github.com/rstaib/jquery-bootgrid/issues/111
这确实很愚蠢,但是由于无法在服务器端解析动态参数,因此您需要通过以下方式在bootgrid配置中定义requestHandler来从sort参数中创建新的请求参数:
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;
}
在Spring Controller中:
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.
这篇关于Spring MVC:动态参数名还是使用Regex的参数名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!