简体   繁体   English

如何在Spring MVC中集成许多视图技术

[英]How to integrate many view technology in Spring MVC

Is it possible to have two view technology in Spring MVC. Spring MVC中是否可以有两种视图技术?

Let's say i want to have a JSP and Velocity, 假设我想拥有一个JSP和Velocity,

on my dispatcher-servlet.xml 在我的dispatcher-servlet.xml上

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

on my spring-views.properties 在我的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: 将Velocity配置添加到调度程序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. 是一个具有完整XML配置和实际视图的出色示例。

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. 另外,我派生了Spring VelocityLayoutView以便能够在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. 速度分解器的阶数为10,内部分解器的阶数为20,以便速度分解器有机会在始终分解的内部分解器之前解析其视图。 Output is forced to UTF-8 for velocity views. 对于速度视图,输出强制为UTF-8。

<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 : 这是修改后的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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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