码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc velocity toolbox 配置

时间:2016-08-12 13:26:37      阅读:684      评论:0      收藏:0      [点我收藏+]

标签:

1.springmvc

<!-- 设置视图解析工具 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <property name="suffix" value=".vm"/>
        <!-- 避免乱码 -->
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <!--<property name="toolboxConfigLocation" value="/WEB-INF/views/layout/toolbox.xml" />-->
        <property name="dateToolAttribute" value="dateTool"/>
        <property name="numberToolAttribute" value="numberTool"/>
        <property name="exposeRequestAttributes" value="true"/>
        <property name="exposeSessionAttributes" value="true"/>
        <property name="layoutUrl" value="layout/layout.vm"/>
        <property name="order" value="0"/>
        <property name="toolboxConfigLocation" value="/WEB-INF/views/layout/toolbox.xml" />
        <property name="viewClass" value="com.tanbao.common.VelocityToolbox20View" />
    </bean>

2.VelocityToolbox20View

public class VelocityToolbox20View extends VelocityLayoutView {

    @Override
    protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ViewToolContext ctx;

        ctx = new ViewToolContext(getVelocityEngine(), request, response,
                getServletContext());

        ctx.putAll(model);

        if (this.getToolboxConfigLocation() != null) {
            ToolManager tm = new ToolManager();
            tm.setVelocityEngine(getVelocityEngine());
            tm.configure(getServletContext().getRealPath(
                    getToolboxConfigLocation()));
            if (tm.getToolboxFactory().hasTools(Scope.REQUEST)) {
                ctx.addToolbox(tm.getToolboxFactory().createToolbox(
                        Scope.REQUEST));
            }
            if (tm.getToolboxFactory().hasTools(Scope.APPLICATION)) {
                ctx.addToolbox(tm.getToolboxFactory().createToolbox(
                        Scope.APPLICATION));
            }
            if (tm.getToolboxFactory().hasTools(Scope.SESSION)) {
                ctx.addToolbox(tm.getToolboxFactory().createToolbox(
                        Scope.SESSION));
            }
        }
        return ctx;
    }
}

3.toolbox.xml

<?xml version="1.0" encoding="UTF-8"?>
<tools>
<toolbox scope="application">
    <!-- <tool key="map" class="java.util.HashMap"/> -->
    <tool key="env" class="com.tanbao.common.EnvContext"/>
</toolbox>
</tools>

 

springmvc velocity toolbox 配置

标签:

原文地址:http://www.cnblogs.com/mmdsnb/p/5764481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!