SpringMVC RequestMapping for GET parameters

前端 未结 6 2010
执念已碎
执念已碎 2020-12-07 12:21

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=135197257         


        
6条回答
  •  无人及你
    2020-12-07 12:50

    You should write a kind of template into the @RequestMapping:

    http://localhost:8080/userGrid?_search=${search}&nd=${nd}&rows=${rows}&page=${page}&sidx=${sidx}&sord=${sord}
    

    Now define your business method like following:

    @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) {
    ...............
    }
    

    So, framework will map ${foo} to appropriate @RequestParam.

    Since sort may be either asc or desc I'd define it as a enum:

    public enum Sort {
        asc, desc
    }
    

    Spring deals with enums very well.

提交回复
热议问题