struts2 与 velocity 整合 探究 编辑

我这边引出几个问题。

问题1,struts2 是怎么让 velocity 按照指定的 ResourceLoader 加载 vm 模板的?

 

首先,struts 默认的查找vm模板的路径有两种:

1,以 webapp 为相对路径下面去找

2,从 classpath 下面去找

那么看下面的代码 org.apache.struts2.views.velocity.VelocityManager:

Java代码   收藏代码
  1. private void applyDefaultConfiguration(ServletContext context, Properties p) {  
  2.         // ensure that caching isn't overly aggressive  
  3.   
  4.         /** 
  5.          * Load a default resource loader definition if there isn't one present. 
  6.          * Ben Hall (22/08/2003) 
  7.          */  
  8.         if (p.getProperty(Velocity.RESOURCE_LOADER) == null) {  
  9.             p.setProperty(Velocity.RESOURCE_LOADER, "strutsfile, strutsclass");  
  10.         }  

 

这里就指明了  velocity.RESOURCE_LOADER 有两个,一个是 stutsfile, 一个是 strutsclass;

 

然后分别指明了 这两个 resource.loader 的详细参数如下:

Java代码   收藏代码
  1. p.setProperty("strutsfile.resource.loader.description""Velocity File Resource Loader");  
  2.           p.setProperty("strutsfile.resource.loader.class""org.apache.velocity.runtime.resource.loader.FileResourceLoader");  
  3.           p.setProperty("strutsfile.resource.loader.path", context.getRealPath(""));  
  4.           p.setProperty("strutsfile.resource.loader.modificationCheckInterval""2");  
  5.           p.setProperty("strutsfile.resource.loader.cache""true");  

 

Java代码   收藏代码
  1. p.setProperty("strutsclass.resource.loader.description""Velocity Classpath Resource Loader");  
  2.       p.setProperty("strutsclass.resource.loader.class""org.apache.struts2.views.velocity.StrutsResourceLoader");  
  3.       p.setProperty("strutsclass.resource.loader.modificationCheckInterval""2");  
  4.       p.setProperty("strutsclass.resource.loader.cache""true");  

 

于是velocityEngine 引擎在初始化resource.loader 的时候就会初始化这2个loader 了。

 

那么,如果在开发阶段,不希望模板被cache ,能够修改完之后立马看到效果。可以在/WEB-INF/velocity.properties里面加上:

Java代码   收藏代码
  1. strutsclass.resource.loader.cache = false  
  2.   
  3. strutsfile.resource.loader.cache = false  

 

============================================================

问题2,struts2 怎么让 velocity 可以支持 layout ?

struts 2 自身带的velocityResult 是不支持 velocity 的layout的,它直接调用的是一个velocityEngine.如果需要它支持的话,需要仿照 velocity-tools中的

VelocityLayoutServlet 来重写 VelocityResult.

 

源代码请看附件


然后在struts.xml中进行配置

Java代码   收藏代码
  1. <package name="babystore" extends="struts-default" abstract="true">  
  2.     <result-types>  
  3.          <result-type name="velocity" class="com.yajun.babystore.common.struts.VelocityLayoutResult" default="true"/>  
  4.     </result-types>  
  5.    </package>  
  6.      
  7. <package name="default" extends="babystore">  
  8.     <action name="HelloWorld" class="helloWorldClass">  
  9.         <result name="success">/success.vm</result>  
  10.     </action>  
  11.   
  12. 。。。。。  

 

 

参考: http://www.ibm.com/developerworks/cn/java/j-lo-struts2-velocity/index.html

 

============================================================

 

问题3,struts2 有哪些提供的方便的 tag,如何运用?

struts2 的tag 分为两种:

第一种 Generic Tag:http://struts.apache.org/2.2.1.1/docs/generic-tag-reference.html

第二种 UI Tags :http://struts.apache.org/2.2.1.1/docs/ui-tags.html

 

这些Tag 都是Component的子类如图:


那么这些Component 怎么和velocity ,freemarke 或者其他模板结合的呢? 我这里介绍和velocity结合的方式:

 

先看下图:


 

可以看到在velocity 这个包里面对上面的 component 都有对应的 类支持。

这些类都继承自org.apache.velocity.runtime.directive.Directive 

这个类是用来实现类似velocity 中的 #set 这样的语法的(前面带个#号的那种)

注意到还有个基类:

Java代码   收藏代码
  1. public abstract class AbstractDirective extends Directive {  
  2.     public String getName() {  
  3.         return "s" + getBeanName();  
  4.     }  

 

发现在每个 beanName 前都加了个字符串s 。这就是为什么 velocity的模板中要用 #surl, #stext, #sform 的原因了。看到这段代码,也就不奇怪了。

 

那么知道了上面的东西之后,想看哪些tag 或者我叫做Component 的是常用的,或者怎么用,就看上面的源码吧。

 

 

问题4,struts2 如何支持 velocity-tools 的 toolbox ?

 

在struts.xml里配置

 

Java代码   收藏代码
  1. <constant name="struts.velocity.toolboxlocation" value="/WEB-INF/toolbox.xml"></constant>  

 

然后再WEB-INF/toolbox.xml 下面加上:toolbox.xml

Xml代码   收藏代码
  1. <?xml version="1.0"?>  
  2. <toolbox>  
  3.     <tool>  
  4.         <key>link</key>  
  5.         <scope>request</scope>  
  6.         <class>  
  7.             org.apache.velocity.tools.struts.StrutsLinkTool  
  8.      </class>  
  9.     </tool>  
  10.     <tool>  
  11.         <key>msg</key>  
  12.         <scope>request</scope>  
  13.         <class>  
  14.             org.apache.velocity.tools.struts.MessageTool  
  15.      </class>  
  16.     </tool>  
  17.     <tool>  
  18.         <key>errors</key>  
  19.         <scope>request</scope>  
  20.         <class>  
  21.             org.apache.velocity.tools.struts.ErrorsTool  
  22.      </class>  
  23.     </tool>  
  24.     <tool>  
  25.         <key>form</key>  
  26.         <scope>request</scope>  
  27.         <class>  
  28.             org.apache.velocity.tools.struts.FormTool  
  29.      </class>  
  30.     </tool>  
  31.     <tool>  
  32.         <key>tiles</key>  
  33.         <scope>request</scope>  
  34.         <class>  
  35.             org.apache.velocity.tools.struts.TilesTool  
  36.      </class>  
  37.     </tool>  
  38.     <tool>  
  39.         <key>validator</key>  
  40.         <scope>request</scope>  
  41.         <class>  
  42.             org.apache.velocity.tools.struts.ValidatorTool  
  43.      </class>  
  44.     </tool>  
  45. </toolbox>  

 

问题5,struts 与 velocity 整合以后,有哪些内置的变量可以直接用?

  • req - the current HttpServletRequest
  • res - the current HttpServletResponse
  • stack - the current OgnlValueStack
  • ognl - an instance of OgnlTool
  • ui - a (now deprecated) instance of a ui tag renderer

package com.yajun.babystore.common.struts;

import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.VelocityResult;
import org.apache.struts2.views.JspSupportServlet;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class VelocityLayoutResult extends VelocityResult {

    private static final Logger LOG                     = LoggerFactory.getLogger(VelocityResult.class);

    private VelocityManager     velocityManager;

    // location where to load the layout template
    protected String            layoutDir;
    // the default layout template to be loaded
    protected String            defaultLayout;

    /**
     * The default layout directory
     */
    public static final String  DEFAULT_LAYOUT_DIR      = "layout/";

    /**
     * The default filename for the servlet's default layout
     */
    public static final String  DEFAULT_DEFAULT_LAYOUT  = "Default.vm";

    /**
     * The velocity.properties key for specifying the relative directory holding layout templates.
     */
    public static final String  PROPERTY_LAYOUT_DIR     = "tools.view.servlet.layout.directory";

    /**
     * The velocity.properties key for specifying the servlet's default layout template's filename.
     */
    public static final String  PROPERTY_DEFAULT_LAYOUT = "tools.view.servlet.layout.default.template";

    public VelocityLayoutResult(){
        super();
    }

    /**
     * The context key that will hold the content of the screen. This key ($screen_content) must be present in the
     * layout template for the current screen to be rendered.
     */
    public static final String KEY_SCREEN_CONTENT = "screen_content";

    /**
     * The context/parameter key used to specify an alternate layout to be used for a request instead of the default
     * layout.
     */
    public static final String KEY_LAYOUT         = "layout";

    @Inject
    public void setVelocityManager(VelocityManager velocityManager) {
        this.velocityManager = velocityManager;
    }

    @Override
    public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        ValueStack stack = ActionContext.getContext().getValueStack();

        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        JspFactory jspFactory = null;
        ServletContext servletContext = ServletActionContext.getServletContext();
        Servlet servlet = JspSupportServlet.jspSupportServlet;

        velocityManager.init(servletContext);

        LOG.info("VelocityLayoutServlet: Layout directory is '" + layoutDir + "'");
        LOG.info("VelocityLayoutServlet: Default layout template is '" + defaultLayout + "'");

        boolean usedJspFactory = false;
        PageContext pageContext = (PageContext) ActionContext.getContext().get(ServletActionContext.PAGE_CONTEXT);

        if (pageContext == null && servlet != null) {
            jspFactory = JspFactory.getDefaultFactory();
            pageContext = jspFactory.getPageContext(servlet, request, response, null, true, 8192, true);
            ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT, pageContext);
            usedJspFactory = true;
        }

        try {
            // ------------- 1. render the screen template ------------
            String encoding = getEncoding(finalLocation);
            String contentType = getContentType(finalLocation);

            if (encoding != null) {
                contentType = contentType + ";charset=" + encoding;
            }
            Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);
            Context context = createContext(velocityManager, stack, request, response, finalLocation);
            Writer screenWriter = new StringWriter();
            response.setContentType(contentType);
            t.merge(context, screenWriter);
            context.put(KEY_SCREEN_CONTENT, screenWriter.toString());

            // ------------- 2. render the layout template -------------

            String layout = getLayoutTemplate(context);

            try {
                // load the layout template
                t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, layout, encoding);
            } catch (Exception e) {

                // if it was an alternate layout we couldn't get...
                if (!layout.equals(defaultLayout)) {
                    // try to get the default layout
                    // if this also fails, let the exception go
                    t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, defaultLayout, encoding);
                }
            }

            Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
            // Render the layout template into the response
            t.merge(context, writer);

            // always flush the writer (we used to only flush it if this was a jspWriter, but someone asked
            // to do it all the time (WW-829). Since Velocity support is being deprecated, we'll oblige :)
            writer.flush();
        } catch (Exception e) {
            LOG.error("Unable to render Velocity Template, '" + finalLocation + "'", e);
            throw e;
        } finally {
            if (usedJspFactory) {
                jspFactory.releasePageContext(pageContext);
            }
        }

        return;
    }

    private String getLayoutTemplate(Context context) {
        if (layoutDir == null) {
            // only for initialize the layoutDir
            layoutDir = (String) velocityManager.getVelocityEngine().getProperty(PROPERTY_LAYOUT_DIR);
            if (layoutDir == null || layoutDir.length() == 0) {
                layoutDir = DEFAULT_LAYOUT_DIR;
            }
        }

        if (defaultLayout == null) {
            // only for initialize the defaultLayout
            defaultLayout = (String) velocityManager.getVelocityEngine().getProperty(PROPERTY_DEFAULT_LAYOUT);
            if (defaultLayout == null || defaultLayout.length() == 0) {
                defaultLayout = DEFAULT_DEFAULT_LAYOUT;
            }
            defaultLayout = layoutDir + defaultLayout;
        }

        Object obj = context.get(KEY_LAYOUT);
        String layout = (obj == null) ? null : obj.toString();
        if (layout == null) {
            // no alternate, use default
            layout = defaultLayout;
        } else {
            // make it a full(er) path
            layout = layoutDir + layout;
        }
        return layout;
    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Spring Boot 中整合 Velocity 并使用自定义标签,可以按照以下步骤进行: 1. 导入相关依赖 在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency> ``` 2. 配置 Velocity 在 `application.properties` 文件中添加以下配置: ```properties spring.velocity.resource-loader-path=classpath:/templates/ spring.velocity.toolbox-config-location=velocity.toolbox.xml ``` 其中,`resource-loader-path` 配置 Velocity 模板所在的目录,`toolbox-config-location` 配置 Velocity 工具箱的配置文件路径。 3. 定义自定义标签 在 `velocity.toolbox.xml` 文件中定义自定义标签,例如: ```xml <tools> <toolbox scope="application"> <tool key="myTag" class="com.example.MyTag"/> </toolbox> </tools> ``` 其中,`key` 是标签名,`class` 是标签类的完整路径。 4. 实现自定义标签类 在项目中创建 `MyTag` 类,并实现 `org.apache.velocity.tools.generic.SafeConfig` 接口和 `org.apache.velocity.tools.generic.Tool` 接口,例如: ```java public class MyTag implements SafeConfig, Tool { private String name; @Override public void configure(Map<String, Object> map) { this.name = (String) map.get("name"); } public String execute() { return "Hello, " + name + "!"; } } ``` 其中,`configure` 方法用于获取配置信息,`execute` 方法用于执行标签的逻辑。 5. 在模板中使用自定义标签 在模板中使用自定义标签,例如: ``` <myTag name="world"/> ``` 这样就可以在模板中使用自定义标签了。 以上就是 Spring Boot 整合 Velocity 并使用自定义标签的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值