Struts 2 tag Library (2) -- control tag

Source: Internet
Author: User
Tags new set

Struts 2 control labels

1) if/elseif/else

<% @ Page contenttype = "text/html; charset = GBK "Language =" Java "errorpage =" "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

2) iterator

Iterator mainly iterates on the set. The set here can be a list, set, or array, or a map set.

The <s: iterator.../> tag has the following three attributes:

Value ---- specifies the set to be iterated, usually determined by an ognl expression. If no value is specified, the set at the top of the valuestack stack is used.

Id ----- ID of the element in the Set

Status ---- determine the attributes of the current iteration Element

<S: iterator value = "{'struts 2 authoritative Guide', 'lightweight Java EE Enterprise Application practices ', 'Crazy Ajax handout '} "id =" name "> <tr> <TD> <s: property value =" # St. count "/> <s: property value =" name "/> </TD> </tr> </S: iterator>

The status instance includes the following methods:

  • Int getCount (): returns several elements of the current iteration.
  • Int getIndex (): generalized index of the current iteration
  • Boolean isEven ()/bolean isOdd (): returns whether the elements of the current iteration are even (ODD)
  • Boolean isFirst ()/boolean isLast (): returns whether the current iteration element is the first (last) element.
<S: iterator value = "{'struts 2 authoritative Guide', 'lightweight Java EE Enterprise Application practices ', 'Crazy Ajax handout '} "id =" name "status =" St "> <tr <s: If test =" # St. odd "> style =" background-color: # bbbbbb "</S: If >>< TD> <s: property value = "name"/> </TD> </tr> </S: iterator> </table>

 

3) append tag

The append tag is used to splice multiple set objects to form a new set. The <s: append/> label requires an ID attribute, which determines the name of the new set generated after splicing.

<S: append/> multiple <s: Param/> can be accepted, and each tag is a set.

<S: append id = "newlist"> <s: Param value = "{'struts 2 authoritative Guide', 'lightweight Java EE Enterprise application ', 'Crazy Ajax handout '} "/> <s: Param value =" {'HTTP: // www.leegang.org', 'HTTP: // www.crazyit.org '} "/> </s: append> <Table border = "1" width = "260"> <! -- Use iterator to iterate the newlist set --> <s: iterator value = "# newlist" status = "St" id = "ele"> <tr> <TD> <s: property value = "# St. count "/> </TD> <s: property value =" ele "/> </TD> </tr> </S: iterator>

In the above example, the list is spliced, or map can be spliced. The following example shows the splicing of MAP and list:

<S: append id = "newlist"> <s: Param value = "# {'crazy Java handout ': 'Li gang', 'lightweight Java EE Enterprise applications ': 'Li gang', 'Crazy Ajax handout ': 'Li gang'} "/> <s: Param value =" # {'HTTP: // www.leegang.org', 'HTTP: // www.crazyit.org '} "/> </S: append> <Table border =" 1 "width =" 280 "> <! -- Use iterator to iterate the newlist set --> <s: iterator value = "# newlist" status = "St"> <tr <s: If test = "# St. odd "> style =" background-color: # bbbbbb "</S: If >>< TD> <s: property value = "key"/> </TD> <s: property value = "value"/> </TD> </tr> </S: iterator>

Here, only the <s: Property> output, value = key and value are used, so that the output can be made. Why?

After checking the information, we know that the map key and value in Java correspond one to one.
 

4) generator tag

The generator tag converts a string into a list set. In the TAG body, the entire temporarily generated set is at the top of the valuestack, but once the Tag ends, the set will be removed from the valuestack.

Has the following attributes:

  • Count: number of elements in the set.
  • Separator: (required) specifies the delimiter used to parse strings
  • Val: (required) specifies the parsed string.
  • Converter: parses a string into an object set.
  • Var: puts the generated Iterator object into the StackContext (Stack Context (map): stack Context, which contains some column objects, including request/session/attr/application map) and request range. You can also replace it with id.
<Table border = "1" width = "240"> <! -- Use the generator tag to parse the specified string into a list set in the generator tag, and the obtained list set is located at the top of valuestack --> <s: generator val = "'struts 2 authoritative guide, lightweight Java EE Enterprise Application Practice, crazy Ajax handout '"separator =", "> <! -- No specific set for iteration, directly iterate the set at the top of valuestack --> <s: iterator status = "St"> <tr <s: If test = "# St. odd "> style =" background-color: # bbbbbb "</S: If >>< TD> <s: property/> </TD> </tr> </S: iterator> </S: generator> </table>

Here, only the <s: Property> output is used, and no ID is specified. Why? The reason is S: Property: Get the attribute of 'value'. If the value is not provided, it is the element at the top of the stack by default.

If the Count attribute is specified, the set can contain up to count elements. If the VaR attribute is specified, the generated set can be placed in the stack context of struts 2.

<Body> <! -- Use generator to parse a string into a set, specifying the ID and count Attributes --> <s: generator val = "'struts 2 authoritative guide, lightweight Java EE Enterprise Application Practice, crazy Ajax handout '"separator =", "Var =" books "Count =" 2 "/> <Table border =" 1 "width =" 300 "> <! -- Iteratively outputs the books set in stack congtext --> <s: iterator value = "# books"> <tr> <TD> <s: property/> </TD> </tr> </S: iterator> </table >$ {requestscope. books} </body>

 

5) Merge tag

Merge also concatenates multiple components, but merge and append are different:

Suppose there are three sets: A = {a, B, c}; B = {a, B, c}; C = {1, 2, 3}

The append result is: {A, B, C, A, B, C, 1, 2, 3}

The result of merge is: {A, A, 1, B, B, 2, C, C, 3}

Merge is cross-merged.

 

6) subset tag

Attributes:

  • Count: number of elements in the subset. If this value is not set, all elements in the source set are obtained by default.
  • Source: Specifies the source set. If not specified, the set at the top of the stack is obtained by default (ValueStack is the value in the value stack storage form ).
  • Start: Specifies the elements of the source set to be intercepted. The default value is the first one (starting from 0)
  • Decider: specifies that the developer selects this element.
  • Var: Set the generated Iterator to the property of the page range.
<Table border = "1" width = "300"> <! -- Use the subset tag to capture the four elements in the target set, starting from the first element --> <s: subset source = "{'crazy Java handout ', 'struts 2 authoritative Guide', 'lightweight Java EE Enterprise application', 'Crazy Ajax output ', 'Crazy XML handout '} "start =" 1 "count =" 4 "> <! -- Use the iterator label to iterate the target set. Because the value attribute value is not specified, the set at the top of the ValueStack stack is iterated --> <s: iterator status = "st"> <! -- Determines whether to use CSS styles based on whether the index of the current iteration element is an odd number --> <tr <s: if test = "# st. odd "> style =" background-color: # bbbbbb "</s: if >>< td> <s: property/> </td> </tr> </s: iterator> </s: subset> </table>

Struts 2 also allows developers to determine the interception standards. If developers need to implement their own interception standards, they need to implement a Decider class, And the Decider class needs to implement SubsetIteratorFilter. decider interface. To implement this class, you must implement a boolean decide (Object element) method. If the returned value is true, the elements are selected as subsets.

Package lee; import org. apache. struts2.util. subsetIteratorFilter; // User-Defined Decider class, implements SubsetIteratorFilter. decider interface public class MyDecider implements SubsetIteratorFilter. decider {// The decide method that must be implemented to implement the Decider interface. // This method determines whether the elements in the set are selected as the subset public boolean decide (Object element) throws Exception {String str = (String) element; // if the collection element (String) contains a Java substring, return str is selected as the subset. indexOf ("Java")> 0 ;}}

Jsp call

<Body> <! -- Define a decider Bean --> <s: bean Var = "mydecider" name = "Lee. mydecider"/> <! -- Use a custom decider instance to intercept the target set, generate a subset, specify the VaR attribute, and put the generated itertor into the stack context --> <s: subset source = "{'crazy Java output', 'struts 2 authoritative Guide', 'lightweight Java EE Enterprise Application practically ', 'Crazy Ajax output ', 'Crazy XML handout '} "decider =" # mydecider "Var =" newlist "/> directly output the newlist attribute of the page range: <br/>$ {pagination. newlist} <Table border = "1" width = "300"> <! -- Iterate the newlist attribute within the page range --> <s: iterator status = "St" value = "# ATTR. newlist "> <tr <s: If test =" # St. odd "> style =" background-color: # bbbbbb "</S: If >>< TD> <s: property/> </TD> </tr> </S: iterator> </table> </body>

7) Sort label

The sort label is used to sort specified set elements. However, you must provide your own sorting rules, that is, you must implement your own comparator. Your own comparator must implement the java. util. comparator interface.

The attributes of the sort tag include:

  • Comparator: A required attribute. It specifies the Comparator instance to be sorted.
  • Source: Specifies the source set. If not specified, the set at the top of the stack (ValueStack is the value in the value stack storage form) is obtained by default (must be in the source TAG body)
  • Var: Set the generated Iterator to the property of the page range.

Package lee; import java. util. comparator; public class MyComparator implements Comparator {// method for determining the size of two elements public int compare (Object element1, Object element2) {// return element1.toString () is determined based on the length of the element string (). length ()-element2.toString (). length ();}}

Jsp application:

<Body> <! -- Define a Comparator instance --> <s: bean var = "mycomparator" name = "lee. MyComparator"/> <! -- Use custom sorting rules to sort the target set --> <s: sort source = "{'lightweight Java EE Enterprise applications', 'Crazy Ajax output ', 'struts 2 authorization', 'Crazy XML handout '} "comparator =" # mycomparator "var =" sortedList "/> output the sortedList attribute in the page range: <br/> $ {pagination. sortedList} <table border = "1" width = "300"> <! -- Iterate the sortedList attribute within the page range --> <s: iterator status = "st" value = "# attr. sortedList "> <tr <s: if test =" # st. odd "> style =" background-color: # bbbbbb "</s: if >>< td> <s: property/> </td> </tr> </s: iterator> </table> </body>

 

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.