springmvc velocity toolbox 配置


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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM