Velocity & Freemarker

Source: Internet
Author: User

Velocityvelocity is a Java-based template engine. It allows anyone to refer to objects defined by Java code simply by using a simple template language (language). When velocity is applied to web development, interface designers can synchronize with Java program developers to develop a Web site that adheres to the MVC architecture, which means that page designers can focus only on the display of the page, while the Java program developer focuses on the business logic encoding. Velocity separates the Java code from the Web page, which facilitates long-term maintenance of the Web site, and provides an optional alternative to JSP and PHP.

1. Variable definition

#Set($name = "velocity")

The string Velocity engine after the equals sign will be re-parsed, for example, when a string starting with $ is made, the variable will be replaced.

#Set($hello = "Hello $name")

The above equation will give $hello a value of "Hello Velocity".

${name} can also be written as: $name. Promote the use of the preceding wording. It should be written in a canonical format: Jack is a ${vice}maniac 2. Cycle
#foreach in are $element $velocityCount #end

The Velocity engine will loop the values in the list to the element variable, creating a $velocitycount variable as a count, starting with 1 and adding 1 for Each loop.

#foreachin, Value: $list. Get ($key) <br>#end

Tip: Velocity is case sensitive.

3. Judging
#if (condition) ... #elseif (condition) ... #else ... #end
4. Notes
 is a single line comment. Multiline comment: #*
5.Velocity also has a logical and, or, and not operator.
#if ($foo && $bar) #end

6. Escape character ' \ ' used: If reference is defined, two ' \ ' means output a ' \ ', if not defined, just as is output.

7.Velocity built in some objects, in the VM template can be directly called, listed as follows: $request, $response, $session Two, Freemarkerfreemarker is a Java language written template engine, It generates text output based on a template. Freemarker is not related to the Web container, that is, it does not know the servlet or HTTP while the Web is running. It can be used not only as an implementation technique for the presentation layer, but also for generating xml,jsp or Java.
Freemarker template files are not more complex than HTML pages, Freemarker template files consist of the following 4 parts:
1, Text: Part of the direct output
2, note: <#--...--> format section, does not output
3, interpolation: ${...} Or #{...} The part of the format that will use the partial substitution output 4,FTL directive in the data model: Freemarker specified, similar to HTML tags, with the name before adding # to differentiate, does not output the following is an example of a freemarker template, containing the above mentioned 4 parts
We have these animals:      <u1>     <#--using FTL instruction--as      being> for         ${being.price} Euros <    < #list >    
1. FTL instruction Rules
In Freemarker, using the FTL tag to use instructions, Freemarker has 3 FTL tags, which are exactly like the HTML tags.
1, start tag:< #directivename parameter>
2, end tag: </#directivename > 3, empty label:< #directivename parameter/>
< #assign age=> <#if (age>60) > Seniors < #elseif (age>)>  < #elseif (age>) ><#else> Young people </#if

The output is: young people

Disadvantage: 1, freemarker variables must have values, not be assigned to the variable will throw an exception, that yellow freemarker error page, really let people look too sad. 2, Freemarker the map limit key must be string, other data types can not operate 3, freemarker for programming convenience to the non-serializable things into the session inside!

Velocity & Freemarker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.