Monday, 10 November 2014

Configuring multiple Spring ViewResolvers

View Resolvers are used to render models in browser, without being tied to any specific view technology. We can configure multiple View Resolvers in our Spring configuration file like,

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

<beans:bean class="org.springframework.web.servlet.view.XmlViewResolver">
<beans:property name="location">
                  <beans:value>/WEB-INF/spring-views.xml</beans:value>
                </beans:property>
</beans:bean>

In this case when the Controller returned a view name, which view resolver strategy will be used?? For this we have to specify priority through an 'order' property where lower order value has a higher priority. Like,

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

<beans:bean class="org.springframework.web.servlet.view.XmlViewResolver">
<beans:property name="location">
                  <beans:value>/WEB-INF/spring-views.xml</beans:value>
                </beans:property>
                <beans:property name="order" value="1"></beans:property>
</beans:bean>


Thanks..

No comments:

Post a Comment