Struts2 tag Library
Including:
OGNL
Struts2 label Classification
Control labels: (if, elseif, else, iterator, append, merge, generator, subset, sort)
DATA tag: (bean, param, date, debug, include, set, url, push)
OGNL
The abbreviation of Object-Graph Navigation Language, which is a powerful expression Language (EL). Through its simple and consistent expression syntax, attackers can access any property of an object, call the object method, traverse the structure of the entire object, and convert the field type.
The default expression language of Struts2 is OGNL.
OGNL symbol --#:
1. Access OGNL context and Action context. # It is equivalent to ActionContext. getContext ().
2. Construct a Map
Used for filtering and projecting Sets
? -- All elements matching the selection Logic
^ -- Extract only the first element that meets the selection Logic
$ -- Extract only the last element that matches the selection Logic
|
Name |
Function |
Example: |
|
Parameters |
Read Request Parameters |
<S: property value ="# Parameters. id"/> |
|
Request |
Read the attributes in the request: |
<S: property value ="# Request. r"/> |
|
Session |
Read the attributes in the session: |
<S: property value ="# Session. s"/> |
|
Application |
Read attributes in application: |
<S: property value ="# Application."/> |
|
Attr |
Request> session> application: |
<S: property value ="# Attr. pc"/> |
Example:
<S: iterator value = "# {'China': 'China', 'usa': 'USA ', 'E': 'Russia'}" var ="C">
<S: property value ="# C. key"/> -- <S: propertyvalue ="# C. value"/> <Br/>
</S: iterator>
Example:
<S: property value ="# Attr. list. {sname} [0]"/>
<Value ="# Attr. list. {$ # this. sid = 3}. {sname} [0]"/>
Supports object method calls:
<S: property value ="# Attr. p. doSomeThing ('Li si ')"/>
Call doSomeThing () of the Student object stored in pageContext ()
[N] Syntax:
Value starting from position N of ValueStack
Top keywords:
Retrieve the top object
Example ::
<S: push value ="'United Arab Emirates '">
<S: property value ="[0]. top"/>
</S: push>
The top object is retrieved from the 0th position in ValueStack and the return value is"United Arab Emirates"
Struts2 label Classification
1. Category:
Struts2 tag Library: User Interface tag Library (Form/non-form tag Library), non-user interface tag Library (control tag library, data access tag Library), ajax supports tag Library
2. Import taglib
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
Control label
1. Condition Tag if... Elseif... Else
Main attributes: test conditions
Example:
<%! Random rnd = newRandom (); %>
<% Int n = rnd. nextInt (200); pageContext. setAttribute ("N", N); %>
<S: if test ="# Attr. n % 7 = 0"> Congratulations, you won the first prize! </S: if>
<S: elseif test ="# Attr. n % 5 = 0"> Congratulations, you won the second prize! </S: elseif>
<S: else> welcome! </S: else>
2. Iteration label iterator
Used for loop array, set, and Map
Attribute:
Value: array, set, or Map
Var: Current element
Status: the status index, count, even, odd, first, and last of the current element.
Example:
Traverse List
<S: iterator value ="{'People 'S republic of china ', 'United America', 'United Arab Emirates '}"Var ="Cur"Status ="St">
<S: property value ="# St. count"/>
<S: property value ="# Cur"/> <Br/>
</S: iterator>
Traverse Map
<S: iterator value = "# {1: 'China', 2: 'u.s., 3: 'Japan '}" var ="Cur">
<S: property value ="# Cur. key"/>
<S: property value ="# Cur. value"/> <Br/>
</S: iterator>
3. merge the set tags append and merge
Merge multiple sets into one set
Property: var-name of the New Set
Example:
<S: appendvar ="NewCollection">
<S: param value ="{'China', 'u.s., 'Japan '}"> </S: param>
<S: param value ="{'German ', 'thai', 'singaporean '}"> </S: param>
</S: append>
<S: iterator value ="# NewCollection"Var ="Cur">
<S: property value ="# Cur"/> <Br/>
</S: iterator>
Note: The merge usage is the same as append, and the results are organized differently.
4. string segmentation label generator
Splits a string into a string array according to the specified delimiter
Attribute ::
Separator: separator
Val (value): String
Count: number of elements in the string data group
Example:
<S: generator separator =","Val ="'Middle, Chinese, People's Republic, guo '"Var ="Arr"/>
<S: iterator value ="# Arr"Var ="Cur">
<S: property value ="# Cur"/> <Br/>
</S: iterator>
5. subset
Extracts a subset from the collection,Note: The subset is placed at the top of the value stack. After the subset label is executed, the subset is automatically removed.
Attribute ::
Source: Set
Start: start Index
Count: number of elements in a subset
Example:
<S: subset source ="{'China', 'America ', 'Japan', 'Germany ', 'thailand', 'Singapore '}"Start ="1"Count ="3">
<S: iterator var ="Cur">
<S: property value ="# Cur"/>
</S: iterator>
</S: subset>
6. Sorting tag sort
Sort the elements in the set according to the defined sorting rules. The new set generated after sorting is placed at the top of the value stack. The label is automatically deleted after it is finished.
Attribute ::
Comparator: class for implementing Comparator
Source: Set
Example:
<S: bean name ="Com. aptech. sorted. LengthSorted"Var ="Ls"/>
<S: sort comparator ="# Ls"Source ="{'Medium 1', '88w', '000000', '000000 '}">
<S: iterator>
<S: property/> <br/>
</S: iterator>
</S: sort>
DATA tag
1. set the variable tag set
It is used to set a new variable and put the new variable into the specified scope.
Attribute:
Var: Reference of New Variables
Value: Value
Example:
<S: set var ="V1"Value ="'People's Republic of China '"Scope ="Page"> </S: set>
<S: property value ="# Attr. v1"/>
Note: If this parameter is not specified, it is stored in the Stack Context by default.
2. Stack pin value tag push
Place a value on the top of the value stack. After the Tag ends, it is removed from the value stack.
Attribute:
Value: the value.
Example:
<S: push value ="'People's Republic of China '">
<S: property/>
</S: push>
3. Create a tag bean for javabean
Creates a javabean instance. If the var value is set, the instance is stored in the Stack Context. Otherwise, the instance is automatically removed after the Tag ends.
Attribute:
Name: name of the javabean class (with a package)
Var: Name
Example:
<S: bean name ="Com. aptech. entity. Student"Var ="Stu">
<S: param name ="Sid"> 1 </s: param>
<S: param name ="Sname"> Zhang sanhao </s: param>
<S: param name ="Sbirthday"> 2009-9-9 </s: param>
</S: bean>
<S: property value ="# Stu. sbirthday"/>
4. format the date tag
Format and output the date
Attribute:
Name: Date
Format: format the string.
Example:
<% PageContext. setAttribute ("D", New Date (); %>
<S: date name ="# Attr. d"Format ="Yyyy MM mm dd hh mm mm ss seconds"/>
5. debug labels
Data stored in Value Stack and stackcontext in the current environment
6. Resource inclusion label include
Used to include an html, jsp, or servlet to the current page. param is used to pass parameters.
Attribute:
Value: contains the Page name.
Example:
<S: include value ="MyHtml.html"> </S: include>
7. Generate tag URLs from URLs
Generate a URL address. You can use the param tag to pass the request parameters. If the parameter is Chinese, it is automatically encoded.
Attribute:
Value: url value
Action: action name, automatically added. action
Example:
<S: url value ="Control. jsp"Var ="Url">
<S: param name ="Id"> People's Republic of China </s: param>
</S: url>
<A href ="<S: propertyvalue ="# Url"/>"> Link </a>
<S: url action ="Test"Var ="Url2"> </S: url>
<S: property value ="# Url2"/>
8. Data Output tag property
Used to output data in the value Stack and Stack Context.
Attribute:
Value: the value to be output.
If the value in the output value stack is not used #
If the value in Stack Context is output, use #
If the value is omitted, the output of the stack top object of the value stack is retrieved.
Summary
OGNL is the basis of tags. To understand the basic usage of OGNL, OGNL must work closely with tags in struts2.
Control tags include a set of condition judgment tags and a set of traversal and operation tags.
A data tag contains a set of tags related to data access.