4.2, using Velocity in Webwork
Use Velocity as a view, there are two ways:
l Use the velocity result type to present a Velocity Template
l Sign up in Web.xml, request the Velocity template file directly to render; this method To add a servlet mapping for WebWorkVelocityServlet in Web.xml, as follows:
servlet>
servlet-maping>
Use the Velocity result type means that the Velocity template is rendered through action. If you access the .vm file, this file will not be rendered, but returns a text file. Therefore, the Velocity Template file should be placed in a web-inflicity to allow it to be directly accessed.
Using WebWorkVelocityServlet means rendering the Velocity template by requesting the .vm file, this may need security checks in the template.
Regardless of which method is used, all features of Velocity are valid when writing templates, and some of WebWork can be used. Here, you are familiar with Velocity, focus on the specific functionality of WebWork.
(1) Specific functions of Webwork
Webwork provides some accessible objects in Value Stack, including:
l Current HTTPSERVLETREQUEST
l Current httpservletResponse
l Current OgnwaStack
l Ognltool instance
l All properties of the current Action class
To access objects in Value Stack, you need to use Velocity reference correctly in the template:
l $ req = httpservletRequest
L $ res = httpservletResponse
L $ stack = ignlvaluestack
L $ OGNL = Ognltool
l $ name-of-protety = Properties of the current Action class
(2) Type of Velocity results
The following example is the use of the Velocity template as a result, to implement the previous Hello example, Note
Xwork.xml:
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
action>
package>
xwork>
HelloAction.java: (as in the example)
EX01-index.jsp: (with the example in the example)
EX01-Success.VM:
hEAD>
Hello, $ Person
body>
html>
(3) Using WebWork tags in Velocity
Use the Velocity template to replace the JSP tag, will lose the ability to use JSP tags. However, Webwork's Velocity Servlet provides a method for using JSP tags in a Velocity template: using # tag, # bodytag and #param velocimaos. Below is a general syntax:
#tag (Name-of-tag list-of-attributes)
or:
#bodytag (Name-of-tag List-of-attributes)
#param (key value)
#param (key value)
...
#end
The following example uses the Velocity implementation of the Demonstration UI tag usage in Section 4.1.1:
Xwork.xml:
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
action>
action>
package>
xwork>
EX02-INDEX.VM:
{color: red;
style>
hEAD>
ui form tags example usufactage using velocity: p>
#bodytag (form "action = 'formProcessingVelocity.Action'" "Method = 'POST'")
#tag (CheckBox "name = 'checkbox'" "Label = 'a checkbox'" "FieldValue = 'Checkbox_Value')
#tag (file "name = 'file'" "label = 'a file field') # Tag (hidden" name = 'hidden' "" value = 'hidden_value')
#TAG (label "label = 'a label')
#tag (Password "name = 'password'" "label = 'a password field')
#tag (Radio "Name = 'Radio'" "" LABEL = 'Radio Buttons' "" List = {'One', 'Two', 'Three'} ")
#tag (select "name = 'select'" "" "LABEL = 'a select list'" "" list = {'one', 'two', 'three'} "
"EMPTYOPTION = True")
#tag (Textarea "Name = 'Textarea'" "Label = 'A Text Area'" "Rows = '3'" "COLS = '40 '")
#tag (textfield "name = 'textfield'" "Label = 'a text field')
#tag (Submit "Value = 'Send Form'))
#end
body>
html>
EX02-Success.VM:
hEAD>
ui form tags example result sale using velocity: p>
ul>
body>
html>
FormProcessingAction.java: (Examples of Section 4.1.1)
FormProcessingAction-Validation.xml: (Example of 4.1.1) The following example uses Velocity implementation of custom components in Section 4.1.1, pay attention to #PARAM usage:
EX03.VM:
hEAD>
Custom Component Example: P>
#bodytag (Component "Template = / files / templates / components / datefield.vm")
#param ("Label" "Date")
#param ("name" "mydatefield")
#param ("Size" "3")
#end
p>
body>
html>
/files/templates/components/datefield.vm: (Examples of 4.1.1)
4.3, use FreeMaker in Webwork