Spring MVC-Storing and Retrieving @PathVariable Map <String, String>
I took 2 PathVariables, and instead of taking them separately, I want to store these 2 PathVariables in a Map and want to extract them from the map.
In Spring MVC 3.1.0, here is my controller class method:
@Controller
@RequestMapping("/welcome")
public class HelloController {
@RequestMapping(value="/{countryName}/{userName}",method=RequestMethod.GET)
public String getPathVar(@PathVariable Map<String, String> pathVars, Model model) {
String name = pathVars.get("userName");
String country = pathVars.get("countryName");
model.addAttribute("msg", "Welcome " + name+ " to Spring MVC & You are from" + country);
return "home";
}
My request url: http: // localhost: 3030 / spring_mvc_demo / welcome / India / ashrumochan123
But when submitting a request using that url, I get HTTP Status 400 -
Description: The request sent by the client was syntactically incorrect.
When I take these path variables separately, it works fine. Here is the code -
@RequestMapping(value="/{countryName}/{userName}", method=RequestMethod.GET)
public String goHome(@PathVariable("countryName") String countryName,
@PathVariable("userName") String userName, Model model) {
model.addAttribute("msg", "Welcome " + userName
+ " to Spring MVC& You are from " + countryName);
return "home";
}
Please tell me if I am doing something wrong?
Any help would be greatly appreciated.
source to share
According to Spring documentation , it has been there since 3.2.
For @PathVariable
c Map<String,String>
, I think you are missing <mvc:annotation-driven/>
in the Spring servlet configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven/>
...
I found it on this link .
source to share