SpringMVC RequestMapping用于GET参数

时间:2012-11-03 20:12:31

标签: java spring spring-mvc

如何让RequestMapping处理url中的GET参数? 例如,我有这个网址

http://localhost:8080/userGrid?_search=false&nd=1351972571018&rows=10&page=1&sidx=id&sord=desc

(来自jqGrid)

我的RequestMapping应该如何?我想使用HttpReqest

获取参数

试过这个:

@RequestMapping("/userGrid")
    public @ResponseBody GridModel getUsersForGrid(HttpServletRequest request)

但它不起作用。

6 个答案:

答案 0 :(得分:120)

在方法参数中使用@RequestParam,以便Spring可以绑定它们,也可以使用@RequestMapping.params数组来缩小spring将使用的方法。示例代码:

@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将只执行此方法,从而避免空检查和相关内容。

答案 1 :(得分:29)

您可以像这样添加@RequestMapping

@RequestMapping("/userGrid")
public @ResponseBody GridModel getUsersForGrid(
   @RequestParam("_search") String search,
   @RequestParam String nd,
   @RequestParam int rows,
   @RequestParam int page,
   @RequestParam String sidx) 
   @RequestParam String sord) {

答案 2 :(得分:13)

这将从请求中获取所有参数。仅用于调试目的:

@RequestMapping (value = "/promote", method = {RequestMethod.POST, RequestMethod.GET})
public ModelAndView renderPromotePage (HttpServletRequest request) {
    Map<String, String[]> parameters = request.getParameterMap();

    for(String key : parameters.keySet()) {
        System.out.println(key);
        String[] vals = parameters.get(key);
        for(String val : vals)
            System.out.println(" -> " + val);
    }

    ModelAndView mv = new ModelAndView();
    mv.setViewName("test");
    return mv;
}

答案 3 :(得分:5)

如果您愿意更改您的uri,您也可以使用PathVariable

@RequestMapping(value="/mapping/foo/{foo}/{bar}", method=RequestMethod.GET)
public String process(@PathVariable String foo,@PathVariable String bar) {
    //Perform logic with foo and bar
}

NB:第一个foo是路径的一部分,第二个是PathVariable

答案 4 :(得分:1)

这适用于我的情况:

@RequestMapping(value = "/savedata",
            params = {"textArea", "localKey", "localFile"})
    @ResponseBody
    public void saveData(@RequestParam(value = "textArea") String textArea,
                         @RequestParam(value = "localKey") String localKey,
                         @RequestParam(value = "localFile") String localFile) {
}

答案 5 :(得分:0)

您应该在@RequestMapping

中编写一种模板
http://localhost:8080/userGrid?_search=${search}&nd=${nd}&rows=${rows}&page=${page}&sidx=${sidx}&sord=${sord}

现在定义您的业务方法如下:

@RequestMapping("/userGrid?_search=${search}&nd=${nd}&rows=${rows}&page=${page}&sidx=${sidx}&sord=${sord}")
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) {
...............
}

因此,框架会将${foo}映射到适当的@RequestParam

由于sort可以是asc或desc,我将其定义为枚举:

public enum Sort {
    asc, desc
}

Spring很好地处理了枚举。