繁体   English   中英

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

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

Spring MVC中是否可以有两种视图技术?

假设我想拥有一个JSP和Velocity,

在我的dispatcher-servlet.xml上

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

在我的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

我已经搜寻了一整天,找不到任何答案。 任何帮助表示赞赏。 谢谢!

将Velocity配置添加到调度程序servlet:

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

您可以根据需要在应用程序中使用尽可能多的视图技术,只要已为每种视图配置了视图解析器,并且它们之间没有冲突(就应该选择控制器返回的视图而言)。 是一个具有完整XML配置和实际视图的出色示例。

这是同时使用速度视图解析器和内部视图解析器的示例。 另外,我派生了Spring VelocityLayoutView以便能够在2.0版中使用速度工具。 速度分解器的阶数为10,内部分解器的阶数为20,以便速度分解器有机会在始终分解的内部分解器之前解析其视图。 对于速度视图,输出强制为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>

这是修改后的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