Struts2 and velocity templates

Source: Internet
Author: User

Velocity is a Java-based template engine. developers can quickly develop the display layer using a simple template language, which separates the display layer from program code. In earlier versions of webwork, all UI labels are generated using the velocity template engine. You can find the template folder in the Struts-core-2.0.8.jar, where the archive folder stores the original VL file!

In struts2, find the VM file first, find the web application, and then find the class path. If you want to use VM as the display layer, you only need to specify the type attribute of result in the configuration file as velocity, then, specify the location attribute of <param> to a resource ending with a VM!

 

1. struts2Velocity inHow to handle Chinese Garbled text:

Because Org. apache. velocity. runtime. the velocity. input. encoding = ISO8859-1, output. encoding = ISO8859-1, it is usually better to use % {gettext ('')} In the VM file to call the contents of the internationalized resource file without generating garbled characters.

2. VelocityVMThe file must go through the actionIn order to be parsed by the program. You cannot directly access the VM.File.

3.In the VMUse struts2 in the fileYou only need to add # s before the original tag.You can use "" between parameters ""Separated!

(1) For example, the following is a form definition:

# Sform ("Action = student" namespace = "/ilkj/student ")

# Stextfield ("label =%{ gettext ('student. Label. name')}" "name = student. Name ")

# Spassword ("label =%{ gettext ('student. Label. password')}" name = student. Age ")

# SSubmit ("value = gettext ('globale. Label. Submit ')")

# End

(2) The following is the value of a parameter in the session.

# Sproperty ("value = # session. Information" "default = novalue ")

(3) The following are the attributes of the access action.

$ {Student. name}, of course, you can also simply write it in the form of $ student. Name.

4. VelocityBuilt-in variables:

Stack --- valuestack, which can be set to $ {stack. findstring ('ognl expression ')}

Action --- the recently executed action, such as $ {action. getstudent (). getname ()}, or you can directly write $ {action. Student. name}

Response --- httpservletrequest

Request --- httpservletresponse

Session --- httpsession

Application --- servletcontext

Base --- generate the application context, equivalent to request. getcontextpath ()

5.Annotation syntax

(1) single line comment :##

(2) multi-line comment :#*......*#

6.Pause execution

# Stop --- stop the template content after execution

7.Escape characters

Velocity uses/escape characters, such as email is defined as andrew830314@163.com (# Set ($ email = 'andrew830314 @ 163.com '), you also want to output $ email without being parsed by velocity, then you can use/$ email.

8.Introduce other files

# Include (""). If it is a JSP or VM file, the content will be displayed. If it is another text file, the text will be displayed, and the binary will be garbled. Multiple files will be introduced for use at intervals, # parse ("") introduces a local VM file. Unlike # include (""), # parse ("") can only introduce one file, and # parse ("") you can access the variables in the parent template.

9.Template code reuse

# Macro (code segment name $ parameter 1 ......)

......

# End

You only need to use the # code snippet name ($ parameter 1 ......) to use this code. Of course, you can also use no parameters.

10.Judgment statement

# If (logical expression)

Note: In VelocityUsing =Determine whether two variables are equal, instead of using equals ("")Method.

# Else

# End

From the above, we can also conclude that the velocity template engine uses # To represent a command. For example, # Set sets a value for a variable, uses $ to reference a variable, and uses () to represent a parameter, use {} to indicate the scope of action.

In addition, velocity only parses face changing variables according to gettxxx (), such as student. name actually finds student. getname () method. You can try to write a variable in action instead of using the getxxx () method to see if the value will be obtained in VM. The answer is that the variable will be output as is.

Velocity uses all objects, even if you define # Set ($ COUNT = 1), it will be packaged as an integer.

In addition, you need to restart the server to modify the VM file. If you are in the development stage, you can set struts. devmode = true in struts. properties to avoid restarting the server each time.

Finally, let's think about whether the $2.95 currency will be directly output by the velocity engine or the variable 2.95 will be searched? If you are thinking about this problem, it means that your Java basics are not enough! Because the variables in Java cannot start with numbers! Therefore, you do not have to worry that the currency $2.95 will be parsed as a variable! In addition, if no variable is found in the velocity engine, it is output as is, for example, $ {student. name} is output directly if not found, which is why a large number of $ {} is output when we directly access JSP. If the variable does not exist and you want it not to be output, you can write $! {}

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.