Friday, February 15, 2013

Multi View Resolver in Spring MVC

Sometime we need to have more than one view resolver in Spring MVC.

For example: We need benefit from JSP and also from template page

To resolve this problem, it's very simple in Spring MVC

Add these lines to your *-servlet.xml file:

<!-- Velocity template resolver -->
<bean 
 class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
 <property name="cache" value="true" />
 <property name="prefix" value="" />
 <property name="suffix" value=".vm" />
 <property name="order" value="1" />

 <!-- if you want to use the Spring Velocity macros, set this property to true -->
 <property name="exposeSpringMacroHelpers" value="true" />
</bean>
Note

Line 7: Tell to Spring that please use this view resolver first
<!-- JSP resolver -->
<bean id="viewResolver"
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/views/" />
 <property name="suffix" value=".jsp" />
 <property name="order" value="2" />
</bean>
Note

Line 6: Tell to Spring that please use this after VelocityViewResolver

That's all thing we need to configure.

For the demonstration, please download Source code from my repository on Github

Use this links to invoke the appropriate features
For VelocityViewResolver: http://localhost:8080/multiresolver/fromvm
For InternalResourceViewResolver: http://localhost:8080/multiresolver/fromjsp

No comments:

Post a Comment