简体   繁体   中英

How to integrate many view technology in Spring MVC

Is it possible to have two view technology in Spring MVC.

Let's say i want to have a JSP and Velocity,

on my dispatcher-servlet.xml

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basename" value="spring-views"/>
</bean>

on my spring-views.properties

item-list.(class)=org.springframework.web.servlet.view.JstlView
item-list.url=/WEB-INF/pages/add-item.jsp

image-list.(class)=org.springframework.web.servlet.view.velocity.VelocityView
image-list.url=/WEB-INF/velocity/image-list.vm
image-list.exposeSpringMacroHelpers=true

I've been search a whole day and I cannot find any answer. Any help is appreciated. Thanks!

Add a Velocity config to your dispatcher servlet:

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

You can use as many view technologies in the application as you want, provided you have configured view resolvers for each and there is no conflict between them (in terms of which should pick up a view returned by a controller). Here is an excellent example with full XML configuration and actual views.

Here is an example of using both a velocity view resolver and an internal view resolver. In addition, I have derived spring VelocityLayoutView to be able to use velocity tools in version 2.0. Order is 10 for velocity resolver and 20 for internal to give velocity resolver a chance to resolve its view before the internal resolver that allways resolve. Output is forced to UTF-8 for velocity views.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      id="viewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="20">
</bean>

<bean class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"
      id="vmViewResolver" p:order="10" p:suffix=".vm" p:prefix=""
      p:cache="true" p:contentType="text/html;charset=UTF-8"
      p:exposeRequestAttributes="false" p:exposeSessionAttributes="false"
      p:exposePathVariables="true" p:exposeSpringMacroHelpers="true"
      p:dateToolAttribute="date" p:toolboxConfigLocation="/WEB-INF/toolbox.xml"
      p:viewClass="org.sba.views.Velocity2LayoutView">
    <property name="attributesMap">
        <map>
            <entry key="messageSource" value-ref="messageSource"/>
        </map>
    </property>
</bean>

And here is the modified VelocityView :

public class Velocity2LayoutView extends VelocityLayoutView {
    ViewToolManager toolManager;

    @Override
    protected Context createVelocityContext(Map<String, Object> model,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ViewToolContext context = toolManager.createContext(request, response);
        context.putAll(model);
        return context;
    }

    @Override
    protected void initTool(Object tool, Context velocityContext) throws Exception {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        toolManager = new ViewToolManager(getServletContext(), false, false);
        if (getToolboxConfigLocation() != null) {
            XmlFactoryConfiguration config = new XmlFactoryConfiguration();
            config.read(getServletContext()
                    .getResourceAsStream(getToolboxConfigLocation()));
            toolManager.configure(config);
        }
        toolManager.setVelocityEngine(getVelocityEngine());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM