Springmvc RequestMapping dla parametrów GET
Jak sprawić, by RequestMapping obsługiwał parametry GET w adresie url? Na przykład mam ten url
http://localhost:8080/userGrid?_search=false&nd=1351972571018&rows=10&page=1&sidx=id&sord=desc
(z jqGrid)
Jak powinno wyglądać moje RequestMapping? Chcę uzyskać parametry za pomocą HttpReqest
Próbowałem tego:
@RequestMapping("/userGrid")
public @ResponseBody GridModel getUsersForGrid(HttpServletRequest request)
Ale to nie działa. 6 answers
Użyj @ RequestParam w argumentach metody, aby Spring mógł je powiązać, użyj również @RequestMapping.params tablica do zawężenia metody, która będzie używana przez spring. Przykładowy kod:
@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
}
W ten sposób Spring wykona tę metodę tylko wtedy, gdy wszystkie parametry są obecne, oszczędzając Cię przed sprawdzaniem null i powiązanymi rzeczami.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-16 17:39:58
Możesz dodać @RequestMapping
w ten sposób:
@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) {
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-03 21:07:54
Spowoduje pobranie wszystkich parametrów z żądania. Tylko do celów debugowania:
@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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-10 00:48:55
Jeśli chcesz zmienić swój uri, możesz również użyć 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
}
Uwaga: pierwszy foo jest częścią ścieżki, drugi to PathVariable
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-21 08:33:38
To działa w moim przypadku:
@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) {
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-02 09:06:20
Należy wpisać rodzaj szablonu do @RequestMapping
:
http://localhost:8080/userGrid?_search=${search}&nd=${nd}&rows=${rows}&page=${page}&sidx=${sidx}&sord=${sord}
Teraz zdefiniuj swoją metodę biznesową w następujący sposób:
@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) {
...............
}
Tak więc framework odwzoruje ${foo}
na odpowiednie @RequestParam
.
Ponieważ sort może być albo asc albo desc zdefiniowałbym go jako enum:
public enum Sort {
asc, desc
}
Wiosna radzi sobie z enumami bardzo dobrze.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-03 20:23:16