SpringMVC RequestMapping for GET Parameters

How to get RequestMapping to process GET parameters in a url? For example, I have this url

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

(from jqGrid)

What should my RequestMapping look like? I want to get parameters using HttpReqest

Tried this:

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

but that will not work.

+49
java spring spring-mvc
Nov 03 '12 at 20:12
source share
6 answers

Use @RequestParam in the method arguments, so Spring can bind them, and also use @ RequestMapping.params to narrow down the method that spring will use. Code example:

 @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 } 

Thus, Spring will execute this method only if ALL PARAMETERS are present that save you from zero checking and things related to it.

+99
Nov 03 '12 at 20:28
source share

You can add @RequestMapping like this:

 @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) { 
+28
Nov 03 '12 at 20:26
source share

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; } 
+12
Jun 10 '13 at 0:48
source share

If you want to change your uri, you can 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 is PathVariable

+1
Oct 21 '16 at 8:33
source share

You should write some kind of template in @RequestMapping :

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

Now define your business method as follows:

 @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, the structure will display ${foo} according to @RequestParam .

Since sorting can be either asc or desc, I would define it as an enumeration:

 public enum Sort { asc, desc } 

Spring works great with enumerations.

0
Nov 03 '12 at 20:23
source share

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
Mar 02 '17 at 9:06 on
source share



All Articles