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"
      class="{package}.UserController">
    <property name="partyService" ref="partyService"/>
</bean>

Declaring an extension of SimpleFormController in the context:

<bean id="userFormController" name="/editUser.html"
     class="{package}.FindUserFormController">
    <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"/>
</bean>
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 FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply