SpringMVC RequestMapping用于GET参数 [英] SpringMVC RequestMapping for GET parameters
问题描述
如何使RequestMapping处理url中的GET参数?
例如我有这个网址
How to make the RequestMapping to handle GET parameters in the url? For example i have this url
http://localhost:8080/userGrid?_search=false&nd=1351972571018&rows=10&page=1&sidx=id&sord=desc
(来自jqGrid)
(from jqGrid)
我的RequestMapping应该如何?我想使用HttpReqest获取参数
how should my RequestMapping look like? I want to get the parameters using HttpReqest
试过这个:
@RequestMapping("/userGrid")
public @ResponseBody GridModel getUsersForGrid(HttpServletRequest request)
但它不起作用。
推荐答案
使用 @ RequestParam 因此Spring可以绑定它们,也使用< a href =http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params%28%29\"rel =noreferrer > @ RequestMapping.params 数组缩小了spring将使用的方法。示例代码:
Use @RequestParam in your method arguments so Spring can bind them, also use the @RequestMapping.params array to narrow the method that will be used by spring. Sample code:
@RequestMapping("/userGrid",
params = {"_search", "nd", "rows", "page", "sidx", "sort"})
public @ResponseBody GridModel getUsersForGrid(
@RequestParam(value = "_search") String search,
@RequestParam(value = "nd") int nd,
@RequestParam(value = "rows") int rows,
@RequestParam(value = "page") int page,
@RequestParam(value = "sidx") int sidx,
@RequestParam(value = "sort") Sort sort) {
// Stuff here
}
这样,如果存在ALL PARAMETERS,Spring将只执行此方法,从而避免空检查和相关内容。
This way Spring will only execute this method if ALL PARAMETERS are present saving you from null checking and related stuff.
这篇关于SpringMVC RequestMapping用于GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!