0

Because of some business/technical constraints we should use spring3 MVC multiple view resolvers(JSP and Velocity). I tried to search on net on this but i couldn't find perfect solution. May be someone else had experienced the same scenario. So Could you please let me know is it possible to use both JSP and Velocity as vew resolvers in the SPring3 MVC application

All help is appreciated.

srk
  • 3,606
  • 2
  • 18
  • 23
  • You can have multiple view resolvers [You might want to read the content of this link](http://stackoverflow.com/questions/2288272/multiple-view-resolvers-in-spring-mvc) – Lee Meador Jan 08 '13 at 17:25

2 Answers2

1

Spring support multiple view resolvers. You chain view resolvers by adding more than one resolver to your application context and use the order property to specify ordering.

you can use chain these jsp and velocity like -

    <bean id="viewResolver" 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"/>
      <property name="order" value="2" />
    </bean>

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
</bean>

<bean id="viewResolver" 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" />
</bean>

Find out more about view chaining here

Avinash T.
  • 2,280
  • 2
  • 16
  • 23
  • 2
    The above won't work Avinash, the reason is since the JSP based ViewResolver has a higher priority it will get picked first and the problem with JSP based viewresolver is that it is a call to RequestDispatcher and it will never return with a null view for the dispatcher to try a different view resolver. It is critical like in my answer for the JSP based view resolver to be the last one. – Biju Kunjummen Jan 08 '13 at 20:34
  • Thank you so much Biju Kunjummen and Avinash T for the detail explanation. – srk Jan 09 '13 at 07:48
  • 1
    Also a problem with that code sample: Multiple beans with id "viewResolver". Otherwise this is correct way to use view resolver chaining. – Jukka Dahlbom Jul 06 '13 at 18:41
0

Yes, it is possible to configure multiple view resolvers, just ensure that you order the Velocity one higher than the JSP based view resolver:

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver" p:order="0">
  ...
</bean


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  <property name="prefix" value="/WEB-INF/jsp/"/>
  <property name="suffix" value=".jsp"/>
</bean>
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125