can be prioritized if they implement the Orderedinterface and this is what some of the con-
crete ViewResolvers do. Set the order bean property on the resolver to control chaining order,
as demonstrated in Listing 7-12.
Listing 7-12.Ordering View Resolvers
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- this will be consulted first as it has a lower 'order' value -->
<bean id="pdfViewResolver"
class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1"/>
<property name="location" value="/WEB-INF/views.xml"/>
</bean>
■CautionA lowervalue for the order property gives it a higherpriority in the chain. Values for the order
property are usually specified in the range 0 ..Integer.MAX_VALUE.
Chaining works in the following manner.
- Each ViewResolver in turn, according to its ordered property, gets the option of
returning a view.
•If the resolver returns null, the next resolver in the chain is consulted.
•If a resolver returns a view instance, the dispatcher uses that view, and no further calls
are made to other resolvers that might still be in the chain. View resolution ends at the
first successful attempt to resolve a view name.
Bear in mind, however, that not all ViewResolvers canreturn nulleven though the con-
tract of the interface permits it. In particular, InternalResourceViewResolvernever returns
nullbecause its implementation depends on calling the RequestDispatcherinternally. There’s
no other way to discover whether a JSP exists, and the RequestDispatcher.forward()method
can only be called once. This means that your ViewResolverwill alwaysreturn a view of some
description, and this particular resolver should only ever appear last in the chain. Practically,
that means that your application can return 404 errors where you don’t want them, so it’s
always important to configure a generic page to handle this type of error.
214 CHAPTER 7 ■THE VIEW LAYER