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