精华内容
下载资源
问答
  • Velocity

    2019-10-06 01:29:23
    Velocity http://velocity.apache.org/The Apache Velocity ProjectVelocity is a project of the Apache Software Foundation, ...
    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.

    posted on 2012-02-06 20:55 lexus 阅读(...) 评论(...) 编辑 收藏

    转载于:https://www.cnblogs.com/lexus/archive/2012/02/06/2340577.html

    展开全文
  • velocity

    2017-09-11 22:50:49
    velocity,是一个基于Java的模板引擎,它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象。 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点。这是很...

    velocity,是一个基于Java的模板引擎,它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象。 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点。这是很好的一个地方,可以提高很多效率。下面来说下velocity的语法吧。

    1.获取:

    $!(var):如果有值则输出,如果为空,则不显示;
    
    ${var}:如果有值则输出,如果不存在,则将该代码原样输出;

    2.设置:

    #set{$var=5}:设置var的值为5
    1. 判断:
    #if{! $var}判断var为空
    
    #elseif{$var}判断var不为空
    
    #else{}
    #end

    4.循环:

    循环数组或List时:

    #foreach($var in list)
    
    ##输出当前迭代的次数
    
    $velocityCount
    
    ${var}
    
    #end

    5.判断是否为null

    1)使用 #ifnull() 或 #ifnotnull() 
       #ifnull ($foo) 
       要使用这个特性必须在velocity.properties文件中加入: 
    userdirective = org.apache.velocity.tools.generic.directive.Ifnull 
    userdirective = org.apache.velocity.tools.generic.directive.Ifnotnull 
    2) 使用null工具判断 
       #if($null.isNull($foo)) 

    ${}过滤输出
    输出表达式的计算结果,并进行过滤,比如:过滤变量中的HTML标签。

    示例:
    ${user.name}

    注:HTTL缺省开启了EscapeXmlFilter,以防止HTML注入攻击,参见:安全示例。如果你需要更强的过滤,请自行实现Filter,并配置到value.filters。此处为运行时热点,请注意性能。

    如果输出变量的类型为Template,则缺省不过滤,比如:${include(“foo.httl”)}

    $!{}不过滤输出
    原样输出表达式的计算结果,不进行任何过滤,通常用于输出HTML片段。

    示例:
    $!{body}

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 19,017
精华内容 7,606
关键字:

velocity