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>

 

posted on 2016-08-12 12:21  mmdsnb  阅读(4201)  评论(0编辑  收藏  举报

导航