1. logic: empty
Logic: the empty label is used to determine whether it is null. If it is null, the content embedded in the label body will be processed. This label is used in the following scenarios:
When the Java object is null
When the String object is ""
When isEmpty () in the java. util. Collection object returns true
When isEmpty () in the java. util. Map object returns true
The following code demonstrates the logic: empty tag to determine whether the set persons is empty:
<Logic: empty name = "listForm" property = "persons"> <div> the set persons is empty! </Div> </logic: empty>
2. logic: notEmpty
The application of this tag is exactly the opposite of the logic: empty tag.
3. logic: equal
The label is equal to the comparison operator.
Eg1. compare whether the user's status attribute is 1. If it is 1, the output is "enabled ";
Eg2. if the value in the previous example is dynamically obtained, for example, the bean: write output is required. Because struts does not support label nesting, EL can be used to solve this problem.
Logic: equal
Here, we will not only introduce logic: equal (=) tags, but also a class of tags that complete comparative operations, including:
Logic: equal (=)
Logic: notEqual (! =)
Logic: greaterEqual (> =)
Logic: lessEqual (<=)
Logic: graterThan (>)
Logic: lessThan (<)
The usage of such labels is similar. We only introduce logic: equal labels, and the rest are similar.
Logic: equal is used to determine whether it is equal. If they are equal, the content embedded in the label body will be processed. This label is used in the following scenarios:
Compare whether the cookie value specified by the cookie attribute of the tag is equal to the value attribute value of the tag.
Compare whether the header value specified by the header attribute of the tag is equal to the value attribute value of the tag.
Compare whether the JSP Bean specified by the name attribute of the tag is equal to the value attribute value of the tag (the property attribute does not appear) or compare whether the property value in the JSP Bean specified by the name attribute of the tag is equal to the value attribute value of the tag.
Compare whether the parameter value specified by the parameter attribute of the tag (in request) is equal to the value attribute value of the tag.
4. logic: notEqual
This label indicates the opposite of logic: equal. It is used in a similar way, slightly.
5. logic: forward
This label is used to implement page orientation and find the global forward of the configuration file. Global redirection in struts-config.xml files
<logic:forward name="redirect"/>
6. logic: greaterEqual
Is a comparison character greater than or equal.
Eg. When a student's score is greater than or equal to 60, "pass" is output ":
<Logic: greaterEqual name = "student" property = "scores" value = "60"> pass </logic: greaterEqual>
7. logic: greaterThan
This is a greater than the comparison character. The method is the same as logic: greaterEqual;
8. logic: lessEqual
This is a comparison character which is less than or equal to the same logic: greaterEqual;
9. logic: lessThan
This is less than the comparison character. The method is the same as logic: greaterEqual;
10. logic: match
This label compares whether the objects are equal;
Logic: the match tag is used to handle the substring matching problem.
If the specified value matches the tag, the content in the TAG body is created. This label is used in the following scenarios:
Check whether the cookie with the specified name matches the value of the tag.
Check whether the header with the specified name matches the value of the tag.
Check whether the JSP Bean with the specified name matches the value of the tag or whether the property attribute value of the JSP Bean with the specified name matches the value of the tag.
Check whether the parameter value of the specified name in the request matches the value of the tag.
The following code demonstrates the typical usage of logic: match tags:
<logic:present header="User-Agent"> <logic:match header="User-Agent" value="MSIE 6.0"> MS IE 6.0 </logic:match></logic:present>
Eg1. check whether the name attribute in the request range contains a "hello" string:
<Logic: match name = "name" scope = "request" value = "hello"> <bean: write name = "name"/> contains a "hello" string. </Logic: match>
Eg2. check whether the name attribute in the request range has "hello" as the starting string:
<Logic: match name = "name" scope = "request" value = "hello" location = "start"> <bean: write name = "name"/> uses "hello" as the starting string. </Logic: match>
Eg3.
<Logic: match header = "user-agent" value = "Windows"> you are running Windows </logic: match>
11. logic: notMatch
The application of the logic: notMatch tag is exactly the opposite of the logic: match tag.
This label is used to compare whether the object is different. It is opposite to logic: match and is used in a similar way.
12. logic: messagePresent
This label is used to determine whether an ActionMessages/ActionErrors object exists;
Logic: the messagesPresent label is used in the following situations:
There is an ActionMessages object in the request scope. The property of the tag corresponds to the property in ActionMessages.
There is an ActionErrors object in the request scope. The property attribute of the tag corresponds to the property in ActionErrors.
There is a String object, which is converted (constructed) into ActionMessage and then added to ActionMessages.
There is a String Array object that converts every String in the Array into an ActionMessage and adds it to ActionMessages.
When the message attribute value of the tag is true. MESSAGE_KEY is the key used to find the Message in the request scope. In other cases, the value of name is used as the key for searching. If the name does not appear, the default value is Globals. ERROR_KEY.
The following code demonstrates the typical usage of the logic: messagesPresent Tag:
Eg1.
<logic:messagesPresent><ul>
Eg2.
If there is an error message, output all of the information:
<logic:messagePresent property="error">
13. logic: messagesNotPresent
The application of the label logic: messagesNotPresent is exactly the opposite of the Application of logic: messagesPresent.
This label is used to determine whether the ActionMessages/ActionErrors object does not exist. Its usage is similar to that of logic: messagePresent.
14. logic: present
This tag is used to determine whether a request object passes a parameter.
If the specified value appears, the content in the TAG body is created. This label is used in the following scenarios:
Check whether a cookie with the specified name appears.
Check whether the header with the specified name appears.
Check whether the JSP Bean with the specified name appears or whether the property attribute in the JSP Bean with the specified name appears.
Check whether the specified parameter in the request appears.
Check whether the current authenticated user is associated with the specified security role.
Check whether the current authenticated subject has a specified name.
Eg1.
When both the user object and its name attribute exist in the request, the corresponding string is output:
<Logic: present name = "user" property = "name"> both the user object and the name attribute of this object exist. </logic: present>
Eg2.
If there is a JavaBean named "hello", the corresponding string is output:
<Logic: present name = "hello"> There Is A JavaBean named "hello. </Logic: present>
Eg3.
<logic:present header="user-agent">we got auser-agent header.</logic:present>
Eg4.
The following code uses the logic: present label to check whether headers with the specified name "User-Agent" appear:
<Logic: present header = "User-Agent"> your browser is <bean: header id = "userAgent" name = "User-Agent"/> <bean: writen ame = "userAgent"/>. <Br/> </logic: present>
15. logic: notPresent
This label is used to determine whether the request object passes a parameter does not exist. It is opposite to logic: present and is used in a similar way.
The application of the logic: notPresent tag is exactly the opposite of the logic: present tag.
16. logic: iterator
Lists the collection values (List, ArrayList, HashMap, and so on ).
Logic: the iterate label is used to iterate the set. You can specify the set for which the label is to be stacked as follows:
Use a runtime expression. The value of this expression is a set.
Reference A JSPBean using the name attribute. the JSP Bean itself is a set.
Reference A JSPBean with the name attribute. One attribute of this JSP Bean is a set. In this case, you can use property to specify this set.
The Set mentioned above can be:
Array of the object type or atomic type ).
Java. util. Collection implementation, including ArrayList and Vector.
Java. util. Enumeration implementation.
Java. util. Iterator implementation.
Java. util. Map implementation, including HashMap, Hashtable and TreeMap.
If the stacked collection contains null values, you must take some measures, because logic: iterate will not create objects in the page scope. Generally, you can use the <logic: present> tag or <logic: notPresent> tag to determine whether the tag is used.
The persons list is filled in ListAction. Here, we simply add three Person objects. In actual applications, the data should be taken from the database. The specific code is as follows:
Public ActionForward execute (ActionMapping mapping, ActionForm. form, HttpServletRequest request, HttpServletResponse response) {ListForm. listForm. = (ListForm) form; List <Person> persons = new ArrayList <Person> (); Person person1 = new Person (); person1.setId ("00001 "); person1.setName ("ZHAO Chen"); Person person2 = new Person (); person2.setId ("00002"); person2.setName ("Li Wei Fang"); Person person3 = new Person (); person3.setId ("00003"); person3.setName ("Wang Wei"); persons. add (person1); persons. add (person2); persons. add (person3); listForm. setPersons (persons); return mapping. findForward ("success ");}
The tag output result is:
00001 --> ZHAO Chen
00002 --> Li xiangfang
00003 --> Wang Wei
Eg1. output the user name in the user list (empList) one by one:
<logic:iterate id="emp" name="empList"> <bean:write name="emp"property="name"/><br></logic:iterate>
Eg2. output the names of two users starting from 1 from the user list
<logic:iterate id="emp" name="empList" indexId="index"offset="1" length="2"> <bean:write name="index"/>.<bean:write name="emp"property="name"/><br></logic:iterate>
Eg3.logic: Nested example of iterator labels
<logic:iterate id="emp" indexId="index" name="empList"> <bean:write name="index"/>. <bean:write name="emp"property="name"/><br> <logic:iterate id="address" name="emp" property="addressList"length="3" offset="1"> <bean:write name="address"/><br> </logic:iterate></logic:iterate>
17. logic: redirect
This label is used to implement page redirection and can pass parameters.
Eg.
<logic:redirect href="http://www.javaeye.com"/>
18. Comparison of logic: forward and logic: redirect
Putting the forward and redirect actions together makes it easier to understand the introduction. For this reason, we will also introduce the logic: forward and logic: redirect tags here.
Let's take a look at the differences between the two actions:
Forward is executed within the servlet. the browser will not perceive this action at all, and the original url will not change. If the browser reloads, it will simply repeat the original request.
Redirect is divided into two steps: the first step is that the web application tells the browser the second url, and then the browser sends a request to the second url.
Redirect is slower than forward because the browser requires a second request. Note that the bean in the first request scope is invisible to the second request.
After understanding the differences described above, you will know when to choose the logic: forward label and when to choose the logic: redirect label.
Logic: the forward label completes PageContext. forward () or HttpServletResponse. sendRedirect (). The Controller determines how to choose. Logic: the redirect label completes HttpServletResponse. sendRedirect ().
When using the logic: redirect tag, we can construct baseurl and query parameters like html: link. If you are interested, refer to html: link tag.