Logic tag library of Struts

  • 2020-04-01 01:10:27
  • OfStack

1. The logic: the empty
Logic: the empty tag is used to determine if it is empty. If empty, the embedded content in the tag body is processed. The label is used for the following situations:
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 example shows the logic:empty tag to determine whether the collection of persons is empty:
< Logic :empty name="listForm" property =" persons">
< Div> Persons is empty! < / div>
< / logic: empty>

2. Logic: notEmpty
The application of this tag is the exact opposite of the logic:empty tag.

3. The logic: equal
The label is equal to the comparator.
Eg1. Compare whether the state attribute of the user is 1. If it is 1, output "enabled ";
Eg2. If the value in the above example is obtained dynamically, for example, it needs to be output via bean:write. Since struts does not support nested tags, EL can be used to solve this problem.
Logic: equal
Not just the logic:equal(=) tag, but a class of tags that perform comparison operations, including:
Logic: equal (=)
Logic: notEqual (! =)
Logic: greaterEqual (> =)
Logic: lessEqual (< =)
Logic: graterThan (>)
Logic: lessThan (<)
The use of this class of tags is similar, so we'll just cover the logic:equal tag, and leave the rest to you.
Logic :equal is equal. If equal, the content embedded in the tag body is processed. The label is used for the following situations:
Compare whether the value of the cookie specified by the cookie attribute of the tag is the same as the value attribute of the tag.
Compare whether the value of the header specified by the header attribute of that tag is the same as the value attribute of that tag.
Compare whether the JSP Bean specified by the tag's name property is equal to the value property value of the tag (the property property does not appear) or whether the property property value in the JSP Bean specified by the tag's name property is equal to the value property value of the tag.
Compare whether the parameter value specified by the tag's parameter attribute (in request) is the same as the value attribute value of the tag.

4. Logic: notEqual
Equal, equal, equal, equal, equal, equal, equal, equal, equal, equal, equal, equal, equal, equal

5. Logic: the forward
This tag is used to navigate the page, looking for the global forward of the configuration file. Global steering in struts-config.xml file
Eg. < Logic: the forward name = "redirect" / >

6. Logic: greaterEqual
Is greater than or equal to the comparator.
When a student's grade is greater than or equal to 60, the output "pass" :
< Logic: greaterEqual name = "student" property = "scores" value = "60" >
Pass the
< / logic: greaterEqual>

7. Logic: greaterThan
This is a greater than comparator. Use the same method as logic:greaterEqual;

8. Logic: lessEqual
This is the less than or equal comparator.

9. Logic: lessThan
This is the less than comparator, using the same method as logic:greaterEqual;

10. Logic: the match
This tag compares objects to be equal;
The logic:match tag is used to handle substring matching.
If the specified value matches the tag, the contents of its tag body are created. The label is used for the following situations:
Checks whether the cookie with the specified name matches the value of the tag.
Checks 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 property value in the JSP Bean with the specified name matches the value of the tag.
Check whether the parameter value of the name specified in the request matches the value of the tag.
The following code example illustrates the typical use of the logic:match tag:
Eg0.
 
<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 scope contains a "hello" string:
 
<logic:match name="name" scope="request" value="hello"> 
<bean:write name="name"/> There is 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"/> In order to" hello "As the starting string.  
</logic:match> 

Eg3.
 
<logic:match header="user-agent" value="Windows"> 
 You're running Windows system  
</logic:match> 

11. Logic: notMatch
The application of the logic:notMatch tag is the exact opposite of the logic:match tag.
This tag is used to compare whether objects are different, as opposed to the logic:match meaning, and is used similarly

12. Logic: messagePresent
This tag is used to determine whether ActionMessages/ActionErrors object exists;
Logic: the messagesPresent tag is used when:
There is an ActionMessages object in the request scope, and the property property of the tag corresponds to the property in ActionMessages.
There is an ActionErrors object in the request scope, and the property property of the tag corresponds to the property in ActionErrors.
There is a String object that is transformed (constructed) into an ActionMessage and then added to ActionMessages.
There is a String Array object that converts each String in the Array into an ActionMessage and then adds it to ActionMessages.
If the tag's message attribute value is true, the message will be found in the request scope with globals.message_key as the key. Otherwise, the value of name will be found as the key. If the name does not appear, the default value is globals.error_key.
The following code example illustrates the typical use of the logic:messagesPresent tag:
Eg1.
 
<logic:messagesPresent> 
<ul> 
<html:messages id="message"> 
<li><bean:write name="message"/></li> 
</html:messages> 
</ul> 
</logic:messagesPresent> 

Eg2.
If there is any error information, output all of it:
 
<logic:messagePresent property="error"> 
<html:messages property="error" id="err" > 
<bean:write name="err"/> 
</html:messages> 
</logic:messagePresent > 

13. Logic: messagesNotPresent
The application of the tag logic:messagesNotPresent is the exact opposite of the application of logic:messagesPresent.
This tag is used to determine whether ActionMessages/ActionErrors object does not exist, use method and logic: messagePresent similar

14. Logic: the present
This tag is used to determine whether the request object pass parameter exists.
If the specified value appears, the tag creates the contents of its tag body. The label is used for the following situations:
Checks if a cookie with the specified name appears.
Checks if a header with the specified name appears.
Checks whether the JSP Bean with the specified name appears or whether the property property in the JSP Bean with the specified name appears.
Checks if the parameter specified in the request name appears.
Checks whether the current authenticated user is associated with the specified security role.
Checks whether the current authenticated principal has the specified name.
When both the eg1.user object and its name attribute exist in the request, the corresponding string is output:
 
<logic:present name="user" property="name"> 
user Object and the object's name Properties are there  
</logic:present> 

Eg2. If there is a JavaBean named "hello", output the corresponding string:
< Logic: the present name = "hello" >
There is a JavaBean named "hello."
< / logic: present>
Eg3.
< Logic: the present header = "the user-agent" >
We got a user-agent header.
< / logic: present>
Eg4.
The following code example shows the logic:present tag checks whether the header with the specified name user-agent appears:
< Logic: the present header = "the user-agent" >
Your browser is < Bean: header id = "userAgent" name = "the user-agent" / >
< Bean: write name = "userAgent" / > . < Br / >
< / logic: present>
15. Logic: notPresent
This tag is used to determine whether the request object passing parameter does not exist.
Logic: the application of the notPresent tag is the exact opposite of the logic:present tag.
16. Logic: the iterator
Used to display the values of a List as a collection (List, ArrayList,HashMap, etc.).
The logic:iterate tag iterates over the collection, and you can specify the collection to iterate over for this tag in the following way:
Use a runtime expression whose value is a collection.
The name attribute is used to reference a JSP Bean that is itself a collection.
Using the name property to refer to a JSP Bean where one of the properties of the JSP Bean is a collection, you can specify the collection using a combination of properties.
The set mentioned above can be:
An Array of object types or atomic types.
Implementation of java.util.Collection, including ArrayList, Vector.
Implementation of java.util.Enumeration.
Implementation of java.util.Iterator.
Implementation of java.util.map, including HashMap, Hashtable, and TreeMap.
If your iterated collection contains null values, you need to take steps because logic:iterate does not create objects in the page scope. It is commonly used with < Logic: present> Label or < Logic: notPresent> Let's see the label.
Here are the object reference relationships and sections of code for the logic: iterated ArrayList example:
Figure 3. Logic :iterate object reference relationships
The list of persons in the figure is filled in the ListAction, where three Person objects are simply added. In a real application, 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(" For li fang "); 
Person person3 = new Person(); 
person3.setId("00003"); 
person3.setName(" wang "); 
persons.add(person1); 
persons.add(person2); 
persons.add(person3); 
listForm.setPersons(persons); 
return mapping.findForward("success"); 
} 

The output result of the label is:
00001 - > Zhao Chen
00002 - > For li fang
00003 - > wang
Eg1. Output the names of the users in the 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 with 1 from the list of users
< 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: nesting of iterator tags for example
< 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 tag is used for page redirection and can pass parameters.
Eg. < Logic: redirect href = "http://www.javaeye.com" / >
18. Logic :forward vs. logic:redirect
----------------------------------------------------------------------------------------------------------------
It is easier to understand the forward and redirect actions together than the introduction, and for this reason the tags logic:forward and logic:redirect are also introduced here.

Let's look at the difference between these two actions:
The forward is executed inside the servlet, and the browser won't notice the action at all, the original url won't change, and the browser's reload will simply repeat the original request.
Redirect is split into two steps: the first step is for the web application to tell the browser a second url, which the browser then sends a request to.
Redirect is slower than forward because the browser makes a second request. Also note that the beans in the first request scope are not visible to the second request.
Understanding the distinction described above will tell you when to use logic:forward tags and when to use logic:redirect tags.
Logic: the forward tag complete PageContext. Forward () or HttpServletResponse. SendRedirect (), how to choose is determined by the controller. Logic: redirect tag complete HttpServletResponse. SendRedirect ().
When using the logic:redirect tag, we can construct the baseurl and query parameters the same way we used the HTML :link. If you are interested, refer to the HTML :link tag.

Related articles: