Spring MVC: Controllers

There’s actually a deep hierarchy of Controllers, from the Controller interface to the AbstractWizardFormController. In most cases, implementing the Controller interface for non-form pages and extending SimpleFormController for form pages will do the job.

Declaring an implementation of Controller in the context (remember, the web-specific context is a child of the main applicationContext, so you have access to service-layer beans as well):

<bean id="userController" name="/users.html"
    <property name="partyService" ref="partyService"/>

Declaring an extension of SimpleFormController in the context:

<bean id="userFormController" name="/editUser.html"
    <property name="commandName" value="user"/>
    <property name="formView" value="findUserByName"/>
    <property name="successView" value="redirect:users.html"/>
    <property name="validator" ref="userValidator"/>
    <property name="partyService" ref="partyService"/>
SimpleFormController property function
formView view automatically returned on validation error
successView view automatically returned without validation error (note redirect: prefix makes it a redirect, not a forward)
validator validator to be used on form
commandName command object in model
(partyService) (service layer bean used in this example)

It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply