Spring MVC 3 Form processing Annotation config
- Spring Constructor Injection
- Spring Setter Injection
- Passing parameters in an aspect with XML
- Spring auto component scanning
- Spring AOP @AfterThrowing annotation example
- Before Aspect XML configuration
- AfterThrowing Aspect XML configuration
- Spring AOP @After returning annotation example
- Spring AOP @Before annotation example
- Around Aspect XML configuration
- After Aspect XML configuration
- Spring AOP terminology
- Spring AOP @Around annotation example
- Spring AOP @After annotation example
- Spring AOP annotation configuration example
- Spring MVC 3 Form Validation XML config
- Spring MVC 3 BeanNameUrlHandlerMapping
- Spring MVC 3 Form processing Annotation config
- Spring MVC 3 Form processing XML config
- AfterReturning Aspect XML configuration
- AOP examples XML configuration
- Spring dependency injection multiple syntax
- Sending Mails With Spring MailSender
- Working with Spring's Application Context
- Wiring collections with spring
- Spring 3.0 java based configuration
- Spring Java Config @Import example
- Spring MVC 3 SimpleUrlHandlerMapping
- Spring MVC 3 example XML config
- Spring MVC 3 example annotation config
- Configure hibernate with Spring XML
- Configure hibernate in Spring Annotations
- Hibernate Named Query CRUD example
- Automatically create update database with Hibernate and Spring
- Spring MVC 3 BeanNameUrlHandlerMapping
- Spring MVC 3 SimpleUrlHandlerMapping
- Spring MVC 3 example XML config
- Spring MVC 3 Form Validation XML config
- Spring MVC 3 example annotation config
- Spring MVC 3 Form processing Annotation config
- Spring MVC 3 Form processing XML config
Comments (0)
In our previous example with xml configuration we tackled the XML configuration, but with Spring MVC 3 there is much better support for annotation based configuration. Just annotate your class with @Controller and spring'll automatically turn your class into a web controller that can handle request. In this tutorial you'll learn to handle form processing with annotation based configuration.
Maven configuration
You need the following jar files. So either add this pom.xml file to your application or download and add the jar files to your class-path.
Model
We are going to submit a model, so we need something to represent the actual data, for this example used a book for keeping things easy to understand.
package org.camelcode.model;
public class Book {
private String author;
private String title;
private Double price;
private Integer year;
public Book() {
}
public Book(String author, String title, Double price, Integer year) {
this.author = author;
this.title = title;
this.price = price;
this.year = year;
}
/*
* Getters & Setters
*/
}
@Controller
Our controller is annotated with @Controller to indicate that this class functions as a controller, the @RequestMapping tell's us witch url the controller is mapped to, here "/RegisterNewBook.htm". In our previous example with xml configuration we used a ModelAndView Object to return the correct view, but you can also return just a String (the view name) and Spring will automatically resolve the correct view. In the showForm() method we add a new Book class instance to the Model object and return the RegisterNewBook Jsp page witch the user is able to submit a form. Next if the user submits the form It will be a Post request and then the method processForm() will handle the request. If the form has any errors then it will return the same form with the errors. If the form completes without any errors then the controller will forward the view to the confirmation page.
package org.camelcode.controller;
import org.camelcode.model.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/RegisterNewBook.htm")
public class RegistrationController {
@RequestMapping(method= RequestMethod.GET)
public String showForm(ModelMap model){
Book book = new Book();
model.addAttribute("BOOK", book);
return "RegisterNewBook";
}
@RequestMapping(method=RequestMethod.POST)
public String processForm(@ModelAttribute(value="BOOK") Book book, BindingResult result){
if(result.hasErrors()){
return "RegisterNewBook";
}else{
return "Confirmation";
}
}
}
Spring ApplicationContext
Because we use annotations we must use <context:component-scan/> to automatically detect the annotated spring beans. - Note: You'll have to add a new namespace to the head of the file. - Next we register our message.properties file to be managed by spring. The InternalResourceViewResolver will forward the correct view depending on the view name.
messages.properties
This typeMismatch.price message is used if an incorrect price is filled in.
typeMismatch.price= Price field is invalid. Please enter a number.
Web.xml
Now we have to register the dispatcherServlet of spring to work as a frotController.
Screenshots








Latest Posts
Latest Comments
Tag cloud