Expert Spring MVC and Web Flow

(Dana P.) #1

■Tip The second strategy will take precedence over the first, even if the first successfully resolves a match.


Over time, code gets refactored and URL links change, so the ParameterMethodNameResolver
supports a mapping between request parameter and the true method name found in the con-
troller. The strategy includes a logicalMappings java.util.Properties, containing a map
between the request parameter name or value and the actual method name. This can come
in very handy if you do not wish to expose real method names to the view layer.
Finally, this ParameterMethodNameResolvercan be configured with a default method name,
if no other method name can be resolved. To configure a default method name, configure the
defaultMethodNameproperty.
An example usage of this MethodNameResolveris contained in Listing 6-57, configuring a
list of parameters to look for and their mappings to real method names.


Listing 6-57.Example of ParameterMethodNameResolver


<bean id="methodNameResolver"
class=
"org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="methodParamNames">
<list>
<value>hello</value>
</list>
</property>
<property name="logicalMappings">
<props>
<prop key="hello">index</prop>
</props>
</property>
</bean>

With the example in Listing 6-57, if a URL like /app/controller?hello=trueis used, the
index()method will be called on the controller. Note that the value of the helloparameter
does not matter when using the methodParamNamesmatching strategy, as only the presence of
the parameter is what matters.


PropertiesMethodNameResolver


A third option for method name resolution is the org.springframework.web.servlet.mvc.
multiaction.PropertiesMethodNameResolver, which happens to be the most flexible of the
strategies. As its name implies, the mapping is done via java.util.Properties, with URL
paths acting as keys and the values as method names. The URL path can use the same
Ant-style pattern matching you’ve seen with other URL path matching strategies.
This strategy will first attempt an exact match on the URL path, excluding the web app
contact name and the servlet mapping, but including the mapping for the Controller. If this
exact match does not work, all the mappings will be attempted in iteration order through the
Propertiesinstance until a match is found.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 171
Free download pdf