How to get Form data as a Map in Spring MVC controller?

You can also use @RequestBody with MultiValueMap e.g. @RequestMapping(value=”/create”, method=RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public String createRole(@RequestBody MultiValueMap<String, String> formData){ // your code goes here } Now you can get parameter names and their values. MultiValueMap is in Spring utils package

Neither BindingResult nor plain target object for bean name available as request attr [duplicate]

Make sure that your Spring form mentions the modelAttribute=”<Model Name”. Example: @Controller @RequestMapping(“/greeting.html”) public class GreetingController { @ModelAttribute(“greeting”) public Greeting getGreetingObject() { return new Greeting(); } /** * GET * * */ @RequestMapping(method = RequestMethod.GET) public String handleRequest() { return “greeting”; } /** * POST * * */ @RequestMapping(method = RequestMethod.POST) public ModelAndView processSubmit(@ModelAttribute(“greeting”) Greeting … Read more

Spring MVC 3 Validation – Unable to find a default provider

If you are using Maven, you must add a dependency to the Hibernate Validator Annotation Processor. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator-annotation-processor</artifactId> <version>4.1.0.Final</version> </dependency> You can find it in the JBoss repository : <repository> <id>JBoss Repository</id> <url></url> <name>JBoss Repository</name> </repository>

Spring Test session scope bean using Junit

I came across this simpler approach, thought I might as well post here in case others need it. <bean class=”org.springframework.beans.factory.config.CustomScopeConfigurer”> <property name=”scopes”> <map> <entry key=”session”> <bean class=””/> </entry> </map> </property> </bean> With this approach, you don’t have to mock any request/session objects. Source: Update for spring 5.x (works in spring 5.3.22, in a JUnit5 … Read more

Spring/json: Convert a typed collection like List

In Spring 3.2 there is now support for generic types using the new exchange()-methods on the RestTemplate: ParameterizedTypeReference<List<MyBean>> typeRef = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response =“”, HttpMethod.GET, null, typeRef); Works like a charm!

java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath()Ljava/lang/String;

java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath()Ljava/lang/String; That method was added in Servlet 2.5. So this problem can have at least 3 causes: The servlet container does not support Servlet 2.5. The web.xml is not declared conform Servlet 2.5 or newer. The webapp’s runtime classpath is littered with servlet container specific JAR files of a different servlet container make/version which … Read more