Expert Spring MVC and Web Flow

(Dana P.) #1
Listing 6-58 contains an example bean definition and configuration of a
PropertiesMethodNameResolver.

Listing 6-58.Example of PropertiesMethodNameResolver

<bean id="methodNameResolver"
class=
"org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/account/findByUsername">findByUsername</prop>
<prop key="/account/*">index</prop> <!-- treated as default -->
</</props>
</property>
</bean>

<bean name="/account/*"
class="com.apress.expertspringmvc.flight.web.ViewAccountController">
<property name="methodNameResolver" ref="methodNameResolver" />
<property name="accountService" ref="accountService" />
</bean>

The default case, in the preceding listing as /account/*, should be the last property
specified, so it will attempted last. Also, notice how the mapping path begins with the
mapping specified for the controller. If you wish to change this behavior, consult the
AbstractUrlMethodNameResolversuperclass for options on how to configure the treatment
of the URL path.

MethodNameResolver Summary
You’ve seen three distinct strategies for mapping a request to a method on a
MultiActionController. The InternalPathMethodNameResolverwill parse the URL path for the
last element and use it as a method name. The ParameterMethodNameResolverlooks for request
parameters, either by value or by name, to resolve a method name. This strategy also supports
a default method name if no name was resolved. Finally, the PropertiesMethodNameResolveris
the least restrictive of the bunch, as it allows for a mapping of arbitrary URL paths to method
names.
Which one should you use? As with everything Spring Framework, the choice is ultimately
yours, and the framework doesn’t lean toward any preference. You should weigh how much
configuration each strategy will take under your circumstances, as generally less configuration
is better. We favor not relying on request parameters for method names, as we feel URL paths
are cleaner and generally friendlier to work with.

MultiActionController Example
For an example of the MultiActionController, we will create a ViewAccountControllerthat
exposes many different ways to find an Account. For instance, a user may want to find an
Accountby username or first name or last name. These are simple read-only methods without

172 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf