Saturday, May 24, 2008

Simple Form using Spring MVC Annotations

I just posted a new example using Spring 2.5's new MVC annotations. The new annotations seem pretty nice. I'm not always for tightly coupling configuration with annotations, but in this case most controllers normally have a single purpose. Plus, you still have the XML configuration as an option if a looser coupling between controller and mappings is needed.

The webapp has basic create, update, delete, and search functionality for a person form. The form basically just has a hidden id (primary key), first name, and last name fields. The example uses Spring MVC annotations, Tiles 2 with Spring By Example's Dynamic Tiles Spring MVC Module, Hibernate configured with annotations and used in Spring with a Hibernate template, and internationalized messages. So a requested URL will go through a controller if there is a mapping, data may be put in place for the page to render, message resources are also put into scope for the page to render, and finally the Dynamic Tiles view renderer takes the request URL and uses it as the body of a default Tiles template.
The example is released under the Apache 2.0 license.

The @RequestMapping annotation is used to indicate what requests a method should handle and can handle Ant style patterns to match a request (not shown below). The @RequestParam annotation can be used to have a request parameter automatically mapped to a variable in the method signature. Or a class can be specified, like in the save method, and any values from the request that can be bound to an instance of the class will be and that instance will be passed into the method.

The create method shows that an Object returned will automatically put into the model based on the classes' name (Person.class --> 'person').

Excerpt from Controller
@Controller
public class PersonController {

final Logger logger = LoggerFactory.getLogger(PersonController.class);

private static final String FORM_VIEW_KEY = "/form/person";
private static final String SEARCH_VIEW_KEY = "/search/person";
private static final String FORM_MODEL_KEY = "person";
private static final String SEARCH_MODEL_KEY = "persons";

/**
* Creates a person object and forwards to person form.
*/
@RequestMapping(value="/form/person.html")
public Person create() {
return new Person();
}

/**
* Gets a person based on it's id.
*/

@RequestMapping(value="/info/person.html")
public ModelAndView info(@RequestParam("id") Integer id) {
Person result = personDao.findPersonById(id);

return new ModelAndView(FORM_VIEW_KEY,
FORM_MODEL_KEY
,
result
);
}

/**
* Saves a person.
*/

@RequestMapping(value="/save/person.html")
public ModelAndView save(Person person) {
personDao
.save(person);

return new ModelAndView(FORM_VIEW_KEY,
FORM_MODEL_KEY
,
person
);
}

...

/**
* Searches for all persons and returns them in a
* Collection as 'persons' in the
* ModelMap.
*/

@RequestMapping(value="/search/person.html")
public ModelMap search() {
Collection<Person> lResults = personDao.findPersons();

return new ModelMap(SEARCH_MODEL_KEY, lResults);
}

}

1 comment:

ByExample said...

I need to build a simple web form with a couple of radio buttons and a text box. When the form loads up, by default a list of objects must be displayed. The radio buttons and text box will be used to filter the list.

I would like to use Spring MVC and annotations, and am trying to get acclimatized with the plethora of Controller options. Can I get some direction on how to proceed with implementing the above?

Thanks.