70

如何使 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)

但它不起作用。

4

6 回答 6

134

在方法参数中使用@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
}

这样,Spring 将仅在所有参数都存在时才执行此方法,从而使您免于检查空值和相关内容。

于 2012-11-03T20:28:06.083 回答
32

您可以@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) {
于 2012-11-03T20:26:01.977 回答
17

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

@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;
}
于 2013-06-10T00:48:55.140 回答
7

如果你愿意改变你的 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
}

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

于 2016-10-21T08:33:38.987 回答
2

这适用于我的情况:

@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) {
}
于 2017-03-02T09:06:20.097 回答
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 很好地处理了枚举。

于 2012-11-03T20:23:16.650 回答