ksplatform学习笔记

1.viewResolver配置中的:

<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm" />
<property name="exposeRequestAttributes" value="true" />
<property name="requestContextAttribute" value="request" />
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="exposeSessionAttributes" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="toolboxConfigLocation" value="WEB-INF/toolbox.xml"/>
<property name="viewClass" value="cn.com.ksplatform.core.expand.spring.velocity.VelocityToolbox20View"/>
</bean>

VelocityToolbox20View代码如下:

public class VelocityToolbox20View extends VelocityToolboxView {
    @Override
    protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {// Create a
                                                            // ChainedContext
                                                            // instance.
        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;
    }
}

  

标红部分的作用是:说白了就是velocity在模板解析过程中想使用tool.xml文件中配置的类

3、配置velocity-tool

在实际使用中,需要配置一些velocity-tool

在spring对最新的velocity tool2.0 的支持不是太好,所以需要扩展viewClass类,才能读取velocitytool.Xml文件。

在没有使用layout的velocity中,需要可以直接继承VelocityToolboxView类,然后重写createVelocityContext方法,但是在VelocityLayoutViewResolver中却无法加载继承VelocityToolboxView的类,而是需要继承VelocityLayoutView,去查找源文件的类,发现VelocityLayoutView继承一样VelocityToolboxView类,所以直接继承VelocityLayoutView然后重写createVelocityContext方法,如下:

 

[java] view plain copy
 
  1. package com.cy.wxs.velocity;  
  2.    
  3. import java.util.Map;  
  4.    
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.    
  8. import org.apache.velocity.context.Context;  
  9. import org.apache.velocity.tools.Scope;  
  10. import org.apache.velocity.tools.ToolManager;  
  11. import org.apache.velocity.tools.view.ViewToolContext;  
  12. import org.springframework.web.servlet.view.velocity.VelocityLayoutView;  
  13.    
  14.    
  15. public classVelocityToolbox2View extends VelocityLayoutView{  
  16.      @Override  
  17.         protected ContextcreateVelocityContext(Map<String, Object> model,  
  18.                 HttpServletRequest request,HttpServletResponse response)  
  19.                 throws Exception {// Create a  
  20.                                     // ChainedContext  
  21.                                     // instance.  
  22.             ViewToolContext ctx;  
  23.    
  24.             ctx = new ViewToolContext(getVelocityEngine(),request, response,  
  25.                     getServletContext());  
  26.    
  27.             ctx.putAll(model);  
  28.    
  29.             if (this.getToolboxConfigLocation() != null) {  
  30.                 ToolManager tm = new ToolManager();  
  31.                tm.setVelocityEngine(getVelocityEngine());  
  32.                tm.configure(getServletContext().getRealPath(  
  33.                        getToolboxConfigLocation()));  
  34.                 if (tm.getToolboxFactory().hasTools(Scope.REQUEST)) {  
  35.                    ctx.addToolbox(tm.getToolboxFactory().createToolbox(  
  36.                             Scope.REQUEST));  
  37.                 }  
  38.                 if (tm.getToolboxFactory().hasTools(Scope.APPLICATION)) {  
  39.                    ctx.addToolbox(tm.getToolboxFactory().createToolbox(  
  40.                             Scope.APPLICATION));  
  41.                 }  
  42.                 if (tm.getToolboxFactory().hasTools(Scope.SESSION)) {  
  43.                    ctx.addToolbox(tm.getToolboxFactory().createToolbox(  
  44.                             Scope.SESSION));  
  45.                 }  
  46.             }  
  47.             return ctx;  
  48.         }  
  49.    
  50. }  



4、关于velocity tool2.0

Velocity的配置文件较与之前的配置的版本,在配置上有一些改变,具体的配置方法在velocity-tool2.0.jar包的位置:org.apache.velocity.tools.generic中

原文地址:https://www.cnblogs.com/zhangshitong/p/5798427.html