Struts2 control label
The control label of Struts2 is mainly used to control the process and control the ValueStack. The control label can control the output process, such as loop and branch operations, you can also merge and sort a set.
1. Common Control labels include if, elseif, else, iterator, append, merge, generator, subset, and sort.
The following describes these control labels and how to use them:
(1). if, elseif, and else labels: if labels are used to control basic conditional processing flows. They are usually used with else or elseif labels.
Its Parameters and attributes are:
Name |
Required? |
Default Value |
Can I use an expression? |
Type |
Description |
Id |
No |
|
Yes |
String |
Used to represent this element. For the UI and Form labels, it is directly converted to the HTML id attribute. |
Test |
Yes |
|
Yes |
Boolean |
The expression used to determine whether to display the internal content of a tag. |
Example: first, create a Struts2 project named BiaoQian and create an if. jsp file in the project. The Code is as follows:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@ taglib uri=/struts-tags prefix=s%><%String path = request.getContextPath();String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%>
> I am the if control label! I am the elseif control label! I am an else control label!
Deploy the project on the Tomcat server and enable the Tomcat server. After running the project, the effect is as follows:
Example resolution: This example uses the if, elseif, and else control labels. It is very similar to if, else if, and else in Java syntax. In the above example, use the test attribute of the if control label to determine whether it is true. if it is true, the content of the if control label is executed. if it is false, the test attribute of the elseif control label is determined, if this parameter is set to true, elseif is executed to control the label content. If it is set to false, the content in the else control label is directly executed. This parameter is set to true, so the displayed content is in the elseif control label!
Note: You can change the property value of test and try it yourself. Here is a brief introduction!
(2). iterator label: Used to iterate a set, which can be Collection, Map, Enumeration, Iterator, array. During iteration, each object is temporarily pushed into the value stack, so that the attributes and methods of the object can be directly accessed inside the tag. After the tag is complete, the value stack content is deleted.
Its parameters have the following attributes:
Name |
Required? |
Default Value |
Can I use an expression? |
Type |
Description |
Id |
No |
|
Yes |
String |
Id, used to represent this element |
Status |
No |
No |
Yes |
Boolean |
If specified, an IteratorStatus variable will be retained during the loop process. This variable is used to query the status of the current iteration. |
Value |
No |
|
Yes |
String |
Iterated object |
The IteratorStatus instance includes the following methods:
Int getConut () returns several elements of the current iteration.
Int geIndex () returns the index of the current element.
Boolean isEven () returns whether the index of the current iteration is an even number.
Boolean isOdd () returns whether the index of the current iteration is an odd number.
Boolean isFirst () returns whether the index of the current iteration is the first element.
Boolean isLast () returns whether the index of the current iteration is the last element.
Example: Create an iterator. jsp file under the above BiaoQian project. The Code is as follows:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@ taglib uri=/struts-tags prefix=s %><%String path = request.getContextPath();String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%>
> Style = background-color: # FFCCFF>
The running effect is as follows:
Example: iterator. on the jsp page, we use the iterator control tag to iterate a set. The value of this tag is the set we iterate over. The tag id is specified as name, it can be referenced by other labels below and the status is specified as st, so that an IteratorStatus variable is retained during the loop process. This variable is used to query the status of the current iteration, then an if control label is used. The test value of the if label is # st. odd, because we use the iterator label for iteration, put the IteratorStatus variable st in the value stack and use # st. odd is used to specify the test attribute of the if tag. odd is also the IteratorStatus method and returns whether the index of the current iteration set is odd, that is, if the index in the iteration set is odd, change the background color of the content style corresponding to the index to pink. To obtain several elements of the current iteration. To obtain the set of the specified iterator tag, which is obtained through the id attribute value of the iterator tag.
Note: you can modify the code as needed!
I want to extend it here. Some students like to write the content of some collections into a single letter. For example, I have a student who often defines the project name and class name as a single letter. I said it is not standard, he has never changed, but he is very good. The winner of the first prize of the Blue Bridge Cup in Our Province (the schoolmaster in the class, the master in mind) won't talk nonsense here, this problem was made by a good friend of mine. To avoid this error, let's expand it. In fact, we can see the error message to know how to solve it:
We will change the main code of the above iterator. jsp file to the following. Here, only the Code marked by the body is attached:
Style = background-color: # FFCCFF>
Then we run it again and find an error on the interface, as shown in:
An error occurred in line 31, but we still missed it:
An error is also reported in the console, similar to the error message above. What is the error? Java. lang. classCastException exception. The message is displayed as this exception. The detailed information is java. lang. the Character type cannot be converted to java. lang. string type, that is, the character type cannot be converted to the String type. Therefore, this error is reported because the 'A', 'B ', 'C' and other letters will regard these letters as a series of characters, so they cannot be converted to the String type. You can add one more letter, that is, not a single letter, in this way, the system will not recognize multiple letters as characters, but recognize them as string types, as shown in the following figure:
(3). append Tag: Used to splice multiple set objects to form a new set. Through this concatenation, The tag completes iteration of multiple sets.
Inside the append tag, use the param tag to specify the set. Usually used with the iterator label!
The parameter property is
Name |
Required? |
Default Value |
Can I use an expression? |
Type |
Description |
Id |
No |
|
Yes |
String |
Used to save the results. The iterator Object Name in valuecontext |
The append label specifies an id attribute. If this attribute is specified, the combined iterator is saved to OgnlContext. You can reference the combined iterator through the value of this attribute.
Example: Create an append. jsp file in the BiaoQian project. The Code is as follows:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@ taglib uri=/struts-tags prefix=s%>
Style = background-color: # FFCC55>
The running effect is as follows:
An example is attached, which is to receive data from the action and then splice it with the append tag. The iterator tag is used to iterate the data from the action class through the iterator tag ??, First, create an AppendIteratorTagAction class. The Code is as follows:
package com.gk;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.ActionSupport;public class AppendIteratorTagAction extends ActionSupport {private List myList1;private List myList2;private List myList3;public List getMyList1() {return myList1;}public void setMyList1(List myList1) {this.myList1 = myList1;}public List getMyList2() {return myList2;}public void setMyList2(List myList2) {this.myList2 = myList2;}public List getMyList3() {return myList3;}public void setMyList3(List myList3) {this.myList3 = myList3;}public String execute(){myList1=new ArrayList();myList1.add(xg);myList1.add(Lc);myList2=new ArrayList();myList2.add(Hello);myList2.add(World);myList3=new ArrayList();myList3.add(HeHe);myList3.add(HaHa);return done;}}
Configure the struts. xml configuration file and AppendIteratorTagAction. The Code is as follows:
/index.jsp
Finally, open the index. jsp file. The Code is as follows:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@ taglib uri=/struts-tags prefix=s %>
The running effect is as follows:
(4). merge labels: similar to append labels, they all splice multiple sets. The difference between them is that the order of elements in the assembled sets is different.
Suppose there are two sets, the difference is as follows:
Append splicing. The order of the new elements is: (1 ). the first element in the first set; (2 ). the second element in the first set; (3 ). the first element in the second set; (4 ). the second element in the second set;
The order of the new elements is: (1 ). the first element in the first set; (2 ). the first element in the second set; (3 ). the first element in the first set; (4 ). the second element in the second set;
We create a new merge under the BiaoQian project. in the jsp file, the action class is still AppendIteratorTagAction. You only need to configure one more action in the configuration file. Here, struts is attached. xml file and merge. jsp file code: struts. the xml file code is as follows:
/index.jsp
/merge.jsp
The code of the merge. jsp file is as follows:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@ taglib uri=/struts-tags prefix=s %>
Enter http: // localhost: 8083/BiaoQian/merge in the address bar. The running effect is as follows:
Then you can compare the running effect of the append label to know their differences.
(5). generator Tag: This tag can split a specified string into multiple strings by the specified separator. You can use the iterator label to generate multiple strings for iterative output. It can be understood that the generator tag converts a string into a List set. In the TAG body, the entire temporarily generated set is placed at the top of the ValueStack, but once the Tag ends, the generated set is removed from the ValueStack.
The generator tag has the following attributes:
1. count attribute: Optional attribute, which specifies the total number of elements in the generated set.
2. val attribute: A required attribute that specifies the parsed string.
3. separator attribute: this is a mandatory attribute that specifies the separator Used to separate strings.
4. converter attribute: an optional attribute that specifies a converter. The converter is responsible for converting each string in the generated collection into an object. Through this converter, a string containing separators can be parsed into a collection of objects. The converter must be an object that inherits org. apache. struts. util. IteratorGenerator. Cobverter.
5. var property: Optional. If this attribute is specified, the generated set is saved in the Stack Context. If this attribute is not specified, the generated set is placed at the top of the ValueStack. Once the Tag ends, the generated set is removed. This attribute can also be replaced with an id.
Example: Create a generator. jsp file under the BiaoQian project. The Code is as follows:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@ taglib uri=/struts-tags prefix=s %>
Style = background-color: blue>
Enter the address: and the effect is as follows:
The code in the preceding example shows that a string of Java * Android * Struts2 * Linux splits it into four strings using the * Separator, based on the iterator iteration tag, the split content is displayed. If the index is an odd number, the background is blue.
(6). subset Tag: used to obtain the subset of the set. The underlying layer of the tag is implemented through the org. apache. struts2.util. SubsetIteratorFilte class.
Its attributes include:
1. count: Optional attribute, indicating the number of elements in the subset. If not specified, it indicates all elements in the source set.
2. source: an optional attribute that specifies the source set. If not specified, it indicates the top stack set of ValueStack.
3. start: Optional attribute, Which is truncated from the nth element.
4. decider: Optional attribute, specifying whether to select this element by the developer. Note: In the subset label, the subset generated by the subset label is located at the top of the ValueStack stack. If the Tag ends, the subset generated by the tag will be removed from the ValueStack stack.
Example: first, create a SubsetDecider class under the BiaoQian project to implement the Decider interface. The Code is as follows:
package com.gk;import org.apache.struts2.util.SubsetIteratorFilter.Decider;public class SubsetDecider implements Decider {@Overridepublic boolean decide(Object arg0) throws Exception {return arg0.toString().length()>4;}}
Create a new subset. jsp file. The Code is as follows:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@ taglib uri=/struts-tags prefix=s %>
Decider attribute not set:
Set decider attributes:
In the address bar, enter
Address. The running effect is as follows:
Example resolution: Check the code of the subset. jsp file. This code is truncated from the second element without specifying the decider attribute, because my set is 'java', 'android', 'linux ', 'C ', so we get the value after the second element. Where This Code sets the decider attribute, so it calls the class specified by the bean tag, that is, the SubsetDecider class, the class we can see that the return value of the decider method is the object length and is greater than 4, so the subset tag will find the set element with a length greater than 4 from the set and find 2, so Android and Linux are displayed.
(7 ). sort label: used to sort elements in a specified set. When sorting, you must provide your own sorting rules, that is, implement your own Comparator. Your own Comparator must implement java. util. comparator interface.
Its attributes include:
1. comparator: Required attribute. It specifies the instance of the comparator to be sorted.
2. source: an optional attribute that specifies the sorting set. If not specified, it indicates the stack top set of ValueStack. (Note: In the sort tag, the subset generated by the subset tag is located at the top of the ValueStack stack. If the Tag ends, the subset generated by the tag will be removed from the ValueStack stack .)
Example: In the BiaoQian project, create a Comparator class SortComparator to implement Comparator. The Code is as follows:
package com.gk; import java.util.Comparator; public class SortComparator implements Comparator
{ @Override public int compare(String s1, String s2) { return s2.length()-s1.length();
}
Create a new sort file with the following code:
<%@ page language=java import=java.util.* pageEncoding=utf-8%><%@taglib uri=/struts-tags prefix=s %>
Input address:
The running effect is as follows: In this line of code, we can see from the Attribute source that the set of the specified sorting is aad, cccc, and bc. The comparator can know that the Comparator instance of the specified sorting is the SortComparator class specified by the bean label, this class is sorted by comparing two lengths and the return value is the length of the second number minus the length of the first number, so it is arranged from the length.
2. For control tags, you can write more examples to understand them! The above content is for your reference only. It is not well written. Please forgive me. If there is any error, please point it out. Thank you!