4
votes

I am currently reading Spring in Action and I am trying to configure a view resolver in spring, however the book didn't say "How spring chooses which View Resolver to use" Its describer to configure the InternalResourceViewResolver and how to add prefix and suffix. But, I could't not find how spring chooses which Resolver to use. What tell spring which resolver to use ?

Edited: quote from the book :

"" What’s missing here is how Spring knows about Tiles definitions. By itself, Tiles- ViewResolver doesn’t know anything about any Tiles definitions, but instead relies on a TilesConfigurer to keep track of that information. So we’ll need to add a Tiles- Configurer bean to spitter-servlet.xml: /WEB-INF/viewsviews.xml ""

The book never answered the question it asked: "how Spring knows about Tiles definitions"

5
Nothing. The DispatcherServlet consults all ViewResolvers in the application context until one of them returns a view. - M. Deinum
The order specified or depending on the order attribute if defined. However if you have a UrlBasedViewResolver as first in your view resolver chain the others will not be consulted. an UrlBasedViewResolver simply creates a URL and forwards to that URL. It doesn't check if that URL actually results in a view to render. - M. Deinum

5 Answers

3
votes

Look at property "order". But remember: "ONLY ONE InternalResourceViewResolver CAN EXIST IN CONTEXT, NO MORE"

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="1" />
        <property name="prefix" value="${view.prefix}" />
        <property name="suffix" value="${view.suffix}" />
        <property name="cache" value="true" />
</bean>
1
votes

The view resolvers implements org.springframework.core.Ordered interface, spring will order the resolvers according to this interface.

PS: TilesViewResolver from tiles2 or tiles3 packages are extends UrlBasedViewResolver.

1
votes

The TilesViewResolver is a specific ViewResolver for Apache Tiles that happens to need extra configuration in order to perform it's operation.

The extra information that it needs is the TilesConfigurer. That bean contains the information about the Tiles definitions and can be configured for example like:

<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/viewsviews.xml</value>
        </list>
    </property>
</bean>

As is mentioned by section 16.5.2 of the documentation:

Spring supports multiple view resolvers. Thus you can chain resolvers and, for example, override specific views in certain circumstances. You chain view resolvers by adding more than one resolver to your application context and, if necessary, by setting the order property to specify ordering. Remember, the higher the order property, the later the view resolver is positioned in the chain.

0
votes

This is the correct way to define a ViewResolver

<bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

If you don't declare any ViewResolver implementation in your application context, Springs registers one for you by default. The default is an automatically-registered InternalResourceViewResolver.

If you declare your own view resolver(s), then the default InternalResourceViewResolver will not be used. If there are multiple view resolvers, then they will be consulted in order until one of them returns a view object.

0
votes

InternalResourceViewResolver class inherite from UrlBasedViewResolver where you can find this. After you define bean for view resolver your view resolver maps your url,from where farther request for view will process

   public void setAttributesMap(Map<String, ?> attributes) {
        if (attributes != null) {
            this.staticAttributes.putAll(attributes);
        }
    }
public Map<String, Object> getAttributesMap() {
        return this.staticAttributes;
}