Tags

After Spring MVC 2.5, controller methods don’t have to return a ModelAndView anymore. Instead, controllers are allowed to return nothing (void), a String representing a view, or just a model.

Forms

To create a very basic form you basically only need to handle a request to view the form and to submit the form. The form can be a POJO as simple as this:

public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
    }
}

Instances of this POJO will get manipulated by the UserFormController class we will create.  Please be noted since 2.5 the controller class no longer needs to extend some class like SimpleFormController. Instead you can use the @Controller annotation.

The SimpleFormController approach

You must implement some callback method like onSubmit.

@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {

User user = (User)command;
return new ModelAndView(“UserSuccess”);

}

The Annotation approach

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute(“user”) User user,BindingResult result, SessionStatus status) {
//clear the command object from the session
status.setComplete();

//return form success view
return “UserSuccess”;

}

form backing object

What is that? Remember the previous User POJO class? In “Old” Spring MVC the SimpleFormController caledl an instance of this ,the User class, the “form backing object”

With the old SimpleFormController, creating your own form backing object could be done by overriding the formBackingObject() method. In Spring MVC 2.5, you can achieve the same behavior by specifying the @ModelAttribute annotation on the method responsible for creating that object.

What is @ModelAttribute?
@ModelAttribute has two usage scenarios in controllers. When you place it on a method parameter, @ModelAttribute maps a model attribute to the specific, annotated method parameter. This is how the controller gets a reference to the object holding the data entered in the form.

You can also use @ModelAttribute at the method level to provide reference data for the model. For this usage, the method signature can contain the same types as documented previously for the @RequestMapping annotation. My understand is it is MVC’s M. In this way you can get an already populated model if not a new model is supposed to create.

Note
@ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.

Source: Spring 3 Reference Documentation

@ModelAttribute(“user”)
public User formBackingObject() {
return new User();
}

It is a good idea to populate your data  before the chosen @RequestMapping by using @ModelAttribute

@ModelAttribute(“webFrameworkList”)
public List<String> populateWebFrameworkList() {

//Data referencing for web framework checkboxes
List<String> webFrameworkList = new ArrayList<String>();
webFrameworkList.add(“Spring MVC”);
webFrameworkList.add(“Struts 1”);
webFrameworkList.add(“Struts 2”);
webFrameworkList.add(“JSF”);
webFrameworkList.add(“Apache Wicket”);

return webFrameworkList;
}

Conclusion

Spring MVC  is a well-known MVC vendor. This post just give you some basic idea about Spring MVC.

I hope you found this information useful. Please leave a comment in case you have questions about this topic, or want to share your experiences with Spring MVC.

Advertisements