zoukankan      html  css  js  c++  java
  • Velocity

    Added by Patrick Lightbody, last edited by Jeromy Evans on Apr 09, 2008  (view change) show comment

    Velocity is a templating language for Java.

    For more information on Velocity itself, please visit the Velocity website.

    Velocity is similar to FreeMarker, as both are template languages that can be used outside of a Servlet container. The framework uses FreeMarker internally since it has better error reporting. Developers may also like that FreeMarker supports JSP taglibs. However, both are good alternatives to JSP.

    Getting Started

    Getting started with Velocity is as simple as ensuring all the dependencies are included in your project's classpath. Other than that, struts-default.xml already configures the Velocity Result.

    struts.xml
    <action name="test" class="com.acme.TestAction">
        <result name="success" type="velocity">test-success.vm</result>
    </action>
    test-success.vm
    <html>
    <head>
        <title>Hello</title>
    </head>
    <body>
    
    Hello, ${name}
    
    </body>
    </html>

    Where name is a property on the Action class. That's it!

    There are few more details of interest, such as how templates are loaded and variables are resolved.

    Template Loading

    The framework looks for Velocity templates in two locations (in this order):

    1. Web application
    2. Class path

    The ordering is designed so that a default set of templates can be placed in a JAR (perhaps shared between applications). If a template needs to be overridden, a different version can be placed in the web application.

    Just the JARs, Ma'am

    Unlike JSPs, templates can be loaded from a JAR. Templates are a great way to support "plugins", since the entire module can be delivered in a single JAR, and the views easily customized by the host application.

    Variable Resolution

    In Velocity, there are three sources for variables, searched in a specific order.

    1. The value stack
    2. The action context
    3. Built-in variables

    Since the action context is resolved after the value stack, you can reference the variable without the typical preceding marker (#) that has to be used with the JSP s:property tag. Omitting the marker can be convenient, but it can also trip you up, if used carelessly.

    #surl "id=url" "value=http://www.yahoo.com"
    Click <a href="${url}">here</a>!

    The Stuts2-Velocity integration layer provides several implicit variables.

    Variable Description
    stackThe value stack itself, useful for calls like ${stack.findString('ognl expr')}
    actionThe action most recently executed
    responseThe HttpServletResponse
    resSame as response
    requestThe HttpServletRequest
    reqSame as request
    sessionThe HttpSession
    applicationThe ServletContext
    baseThe request's context path

    Configuring Velocity

    You can configure Velocity by placing configuration items in velocity.properties.

  • 相关阅读:
    mysql远程登录
    cmd中不能使用中文输入法解决方法
    dedecms sphinx 配置
    Docky需要混合窗口管理器才能工作,请启用混合窗口管理器后重新启动Docky.
    PHPCGI 进程 CPU 100% 与 file_get_contents 函数的关系
    MySQL MyISAM索引类型分析和优化
    ubuntu配置jdk7.0过程
    基于角色的用户权限设计的问题,大家探讨下
    工厂支持多数据库开发的三层结构模式随笔(一)
    分页利器 AspNetPager常用属性
  • 原文地址:https://www.cnblogs.com/lexus/p/2340577.html
Copyright © 2011-2022 走看看