SpringMVC RequestMapping for GET parameters

前端 未结 6 1997
执念已碎
执念已碎 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:34

    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
    }
    

    This way Spring will only execute this method if ALL PARAMETERS are present saving you from null checking and related stuff.

    0 讨论(0)
  • 2020-12-07 12:49

    You can add @RequestMapping like so:

    @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) {
    
    0 讨论(0)
  • 2020-12-07 12:50

    This works in my case:

    @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) {
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 12:54

    This will get ALL parameters from the request. For Debugging purposes only:

    @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;
    }
    
    0 讨论(0)
  • 2020-12-07 12:58

    If you are willing to change your uri, you could also use 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: The first foo is part of the path, the second one is the PathVariable

    0 讨论(0)
提交回复
热议问题