Struts the logic label detailed

Source: Internet
Author: User
Tags constant tld
The Logic tab of struts can determine the content of a Web page based on specific logical conditions, or iterate through the collection elements, and the Html,bean tag is the most commonly used three tags for struts applications. Its function is mainly to compare operations, string matching, to determine whether the specified content exists, Loop through the collection for request forwarding and redirection, the following specify the purpose of the label: <logic:equal> variable equals the specified constant <logic:notEqual> variable is not equal to the specified constant <logic:lessthan > variable is less than the specified constant <logic:lessEqual> variable is less than or equal to the specified constant <logic:geraterThan> variable is greater than the specified constant <logic:geraterEqual> variable is greater than or equal to the specified constant these tags are similar, have common properties, cookie properties Specify the value of the cookie property, and then compare with the constants set by value. Header property sets the value of the header request header. It is also compared by the value set by the Value property. The Parameter property sets a request parameter and is then compared by the value set by the Value property. The Name property sets a variable and then compares it with value. If both the name and property properties are set, At this point, the Name property specifies that the Javabean,property property that already exists specifies the Bean's properties. This label feels very simple and uses very simply, and here's a look at the label that matches the string and the label that determines the specific content. <logic:match> tag is to determine whether the variable species contains a constant string,<logic:netmatch> also determines whether the string is not included, using the Name property to define a string variable, The Value property is then used to determine whether the variable contains the string or does not contain the string, and the judgement returns true successfully. The <logic:empty> and <logic:netEmpty> tags can determine whether the specified variable is empty, and you can use the Name property to determine whether a string is null. For example, a string that requests a parameter. The <logic:present> and <logic:notPresent> labels can determine whether the specified object is empty, This tag has a lot of properties. The cookie property determines whether the cookie exists. The Header property determines whether the HTPP request header exists. Role property is used to determine the current permissionIs the specified security role. The user property is a decision to determine whether the currently authenticated users have the specified user name. The parameter property is the ability to determine whether the requested parameter exists. The Name property is used to determine whether the specified bean exists. Setting the name and property properties at the same time determines the specific bean Whether the property exists. <logic:messagesPresent> and <logic:messagesNotPresent> tags are used to determine whether a particular actionmessages or subclass Actionerrors object is in the request scope. The Name property is the key used to retrieve the Actionmessages object. property specifies that a message key, or a specific Actionmessage object, is retrieved from the Actionmessages collection object. Looping through the logic tag is the most complex tag in the logic tag library, and is the most versatile label that can traverse an array in a loop, collection,enumeration,irerator or all elements in a map 1)                traversing the tags of the collection <logic:iterate> The Name property specifies the collection object that needs to be traversed, each time retrieving an element from the collection, then storing it in the page range, and naming the element with the id attribute, preferably nested inside a <bean:write> tag, The string output that is specified by the first ID just traversed is then looped in the output. The number of elements that need to be traversed by the length property, and if no Length property is set, all the elements in the collection are traversed. The Offset property specifies the location in which to start the traversal, The default value is the 0.indexId property defines a serial number that represents the currently traversed element, which is stored in page range and can be <bean:write> The label access output outputs a number of int. such as 1.2.3.4. Series number of elements that can be output by user. 2)                traversal map is through the <logic:iterate> tag, the Name property specifies a hashmap, stored in theRequest scope, and then assign the variable to the ID, INDEXID can specify the index number and then output it through the <bean:write> tab. Each element in the HashMap is a collection object, so it can be nested. That means < Logic:inerate> This tag can also be used to make a map of collection elements. So it's powerful. There is also a collection property that can set an expression that returns the result of the expression as <logic:inerate> tag output collection or map. This property is similar to the Name property. Finally, the logic label,<logic:forward> tag for request forwarding and redirection is used to request forwarding, and its Name property specifies the target to forward, and the <global-forwards> in the Struts configuration file element and the child elements <forward> elements match. The simple thing is to define the value of the <logic:forward> tag's name attribute, to find the <golbal-forwards> child element <forward > The matching Name property of the element, which is then forwarded through the path specified by path. Redirection uses the <logic:redierct> label its forward,href and page properties to specify the target for redirection, these properties and   
So commonly used tags, the source code of course need to take out the study, the following list of several research results:
  
1, the label internal use Collection to represent a given container, all the given container objects (such as ArrayList, MAP, etc.) will be converted into Collection, Collection is actually the Map and List of the parent class.
  
2, the label itself to maintain a circular index, without the programmer to manage the index
  
3, the label common several properties are as follows: Name, property, scope, ID
  
The APIs given by Struts are described below:
  
Name: Contains the names of the beans that are to traverse the JSP page of the Collection (if the property is not defined), or the names of the beans in the JSP that obtained the attribute through the getter method, and these getter methods return the Colle Ction (if the property is defined).
  
Property: The name of the attribute defined in the Jsp bean named in name, returned by the getter method to a Collection
  
Scope: Indicates where to look for beans with name names, if the default is ' any scope ' is not defined
  
ID: If Collection is not empty, the name of each element in the Collection at each traversal.
  
In addition to the ID each element is RT expr, here RT expr means Run time Expression. It is clear that if you specify the <rtexprvalue> of an attribute as true, you can use the <%=%> in such attributes. This configuration file is in the TLD.
  
Only the ID is required to be described.
  
Instructions on the API description:
  
The ID is only a temporary identity, and the name attribute appearing in the <bean:write below will be consistent with the ID to print out the <bean:write property, which is the attribute in iterator.
  
Give an example to explain
  
The following code generates a ladder-like table
  
System Resource Operations
  
Soft3
Res3
Opt3
Soft12
Res12
opt1211
Soft11
Res11
opt1111
  
Before this came a request.getattribute ("Userpurview"), so there was a userpurview in the first logic, which was looking for userpurview in this request
  
   return is a List  
<table width= "border=" 0 ">
<tr><td> system </td>
<td> Resources </td>
<td> Operations </td>
</tr>
<!---the value in the first-level iteration-->//request is "Userpurview" in the request, the ID named Targetsys
<logic:iterate id= "Targetsys" name= "Userpurview" scope= "request" >//This ID can be arbitrarily named, but note the consistency of the following use
<tr bgcolor= "#cccccc" ><td height= "class=" "Unnamed2" >
<bean:write name= "Targetsys" Property= "cn"/>//Here name is consistent with the above ID, property is the element in the first list
</td>
&LT;TD height= "class=" Unnamed2 > </td>
&LT;TD height= "class=" Unnamed3 > </td>
</tr> <!---second-level iterative-->
<logic:iterate id= "Targetres" name= "Targetsys" property= "Purviewreslist" >
&LT;TR&GT;&LT;TD height= "class=" Unnamed2 "> </td><td height=" "class=" >
<bean:write name= "Targetres" Property= "cn"/>
</td>
&LT;TD height= "class=" Unnamed6 > </td>
</tr>
<!---third-level iterative-->
<logic:iterate id= "Targetopr" name= "Targetres" property= "Purviewoprlist" >
&LT;TR&GT;&LT;TD height= "class=" unnamed4 "> </td><td height=" class= "" Unnamed4 "> </td>
&LT;TD height= "class=" "Redzi" >
<bean:write name= "TARGETOPR" Property= "cn"/></td>
</tr>
</logic:iterate>
  
</logic:iterate>
  
</logic:iterate>
</table>
  
   Conclusions
  
Multi-level iterations and a single layer, the only attention is the ID and <bean:write in the corresponding name, the upper logic ID and subordinate logic's name corresponds, and take out the Collection,name and ID does not necessarily actually need To this bean, it's all virtual.

Struts ' logic label

(1) Empty label
Class Name: Org.apache.struts.taglib.logic.EmptyTag
Label Body: bodycontent=jsp
Referencing Logic:empty
Attribute Attribute:name,property,scope
Function: To determine whether an object's value is empty
(2) Equal
Class Name: Org.apache.struts.taglib.logic.EqualTag
Label Body: bodycontent=jsp
Referencing logic:equal
Attribute Attribute:cookie,header,name,parameter,property,scope,value
function: equals comparison character

(3) Forward
Org.apache.struts.taglib.logic.ForwardTag
Label Body: bodycontent=empty
Referencing Logic:forward
Attribute Attribute:name
Features: page orientation, find the global forward of the configuration file

(4) Greaterequal
Class Name: Org.apache.struts.taglib.logic.GreaterEqualTag
Label Body: bodycontent=jsp
Referencing logic:greaterequal
Attribute Attribute:cookie,header,name,parameter,property,scope,value
Function: is greater than or equal to the comparison character

(5) GreaterThan
Class Name: Org.apache.struts.taglib.logic.GreaterThanTag
Label Body: bodycontent=jsp
Referencing Logic:greaterthan
Attribute Attribute:cookie,header,name,parameter,property,scope,value
Function: greater than comparison character

(6) Iterator
Class Name: Org.apache.struts.taglib.logic.IterateTag
Label Body: bodycontent=jsp
Referencing Logic:iterator
Attribute Attribute:collection,id,indexid,length,name,offset,property,scope,type
Function: Display list of collection values (lists, ARRAYLIST,HASHMAP, etc.)

(7) Lessequal
Class name Org.apache.struts.taglib.logic.LessEqualTag
Label Body: bodycontent=jsp
Logic:lessequal
Attribute Attribute:cookie,header,name,parameter,property,scope,value
Function: Less than or equal to comparison character

(8) LessThan
Class Name: Org.apache.struts.taglib.logic.LessThanTag
Label Body: bodycontent=jsp
Logic:lessthan
Attribute Attribute:cookie,header,name,parameter,property,scope,value
Function: less than comparison character

(9) match
Class Name: Org.apache.struts.taglib.logic.MatchTag
Label Body: bodycontent=jsp
Referencing Logic:match
Attribute Attribute:cookie,header,location,name,parameter,property,scope,value
Features: Comparing objects

(a) Messagesnotpresent
Class Name: Org.apache.struts.taglib.logic.MessagesNotPresentTag
Tags: bodycontent=jsp
Referencing logic:messagesnotpresent
Attribute Attribute:name,property,message
Function: Actionmessages/actionerrors object does not exist

(one) messagepresent
Class Name: Org.apache.struts.taglib.logic.MessagesPresentTag
Tags: bodycontent=jsp
Referencing logic:messagepresent
Attribute Attribute:name,property,message
Function: Actionmessages/actionerrors object does not exist

(Notempty)
Class Name: Org.apache.struts.taglib.logic.NotEmptyTag
Tags: bodycontent=jsp
Referencing Logic:notempty
Attribute Attribute:name,property,scope
Function: Compare objects to not empty

(notequal)
Class Name: Org.apache.struts.taglib.logic.NotEqualTag
Tags: bodycontent=jsp
Referencing logic:notequal
Attribute Attribute:cookie,header,name,parameter,property,scope,value

(Notmatch)
Class Name: Org.apache.struts.taglib.logic.NotMatchTag
Tags: bodycontent=jsp
Referencing Logic:notmatch
Attribute Attribute:cookie,header,location,name,parameter,property,scope,value
Function: Compare objects for unequal

(notpresent)
Class Name: Org.apache.struts.taglib.logic.NotPresentTag
Tags: bodycontent=jsp
Referencing logic:notpresent
Attribute Attribute:cookie,header,name,parameter,property,role,scope,user
Function: Request object pass parameter does not exist

(present)
Class Name: Org.apache.struts.taglib.logic.PresentTag
Tags: bodycontent=jsp
Referencing logic:present
Attribute Attribute:cookie,header,name,parameter,property,role,scope,user
Function: Request object pass parameter exists

(redirect)
Class Name: Org.apache.struts.taglib.logic.RedirectTag
Tags: bodycontent=jsp
Referencing Logic:redirect
Property Attribute:anchor,forward,href,name,page,paramid,paramname,paramproperty,paramscope,property,scope, Transaction
function; page steering, transitive parameters

Example:

1, <logic:iterate id= "row" name= "Formbean" property= "list" indexid= "index" >
<logic:equal name= "Row" property= "unit" value= "DefaultValue" >
...
</logic:equal>
</logic:iterate>

<logic:notempty name= "Info" >
<logic:iterate id= "List" name= "info" >
<bean:define id= "Curid" name= "list" property= "id"/>
<logic:equal parameter= "id" value= "<%= curid.tostring ()%>" >
Abcde
</logic:equal>
</logic:iterate>
</logic:notEmpty>
Parameter the name of the HTTP request parameter to be compared
Name if the bean or bean's properties are to be compared, this property represents the bean's name
Name of the Bean property to be compared

Logic Use


With the <logic:iterate> tags in struts, you can accomplish some complex loops.
Here's a small example to illustrate this point:
  
Iteratebean.java:
  
Package beantest;
  
Import Org.apache.struts.action.ActionForm;
  
public class Iteratebean extends Actionform {
Private string[] Name_coll = null;
public void Setname_coll (string[] name_coll) {
This.name_coll = Name_coll;
}
Public string[] Getname_coll () {
return this.name_coll;
}
  
Private string[] name_new = null;
public void Setname_new (string[] name_new) {
System.out.println ("Iteratebean:setname_new (string[])");
This.name_new = name_new;
}
Public string[] Getname_new () {
return this.name_new;
}
}
  
Iterateaction.java
  
Package beantest;
  
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
  
Import org.apache.struts.action.Action;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
  
public class Iterateaction extends Action {
Public Actionforward Execute (
Actionmapping Mapping,
Actionform form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Iteratebean Ibean = (iteratebean) Form;
string[] temp = {"Bitan", "Helen"};
Ibean.setname_coll (temp);
temp = new string[] {"AAAAA", "bbbbb"};
Ibean.setname_new (temp);
Return Mapping.findforward ("Iterate-view");
}
}
  
ITERATE-VIEW.JSP:
  
<%@ page contenttype= "text/html;charset=gb2312"%>
  
<%@ taglib uri= "/web-inf/struts-logic.tld" prefix= "logic"%>
<%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%>
<logic:iterate id= "Iterate" name= "Ibean" property= "Name_coll" scope= "request" offset= "0" >
  
<logic:iterate id= "iterate_new" name= "Ibean" property= "name_new" scope= "request" offset= "0" >
Name: <font color=red><%=iterate%></font>; Code: <font color=red><%=iterate_new%></font><br>
</logic:iterate>
  
★〓〓〓〓〓〓〓〓★<br>
<bean:include id= "Lower" page= "/test/lower.jsp"/>
<bean:write name= "Lower" filter= "false"/><br>
</logic:iterate>
  
LOWER.JSP:
  
<font Color=blue>hello, iterate!</font>
  
Struts-config.xml:
  
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Struts-config Public
"-//apache Software foundation//dtd Struts Configuration 1.1//en"
"Http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >
<struts-config>
<form-beans>
<form-bean type= "Beantest. Iteratebean "name=" Ibean "/>
</form-beans>
<action-mappings>
<action path= "/iterate-test" type= "Beantest". Iterateaction "Name=" Ibean "scope=" Request ">
<forward path= "/test/iterate-view.jsp" name= "Iterate-view"/>
</action>
</action-mappings>
</struts-config>
  
Xml:
  
<?xml version= "1.0"?>
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD ">
<web-app>
  
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/eye/*</url-pattern>
</servlet-mapping>
  
</web-app>
  
Output effect:
  
Name:bitan; Code:aaaaa
Name:bitan; code:bbbbb
★〓〓〓〓〓〓〓〓★
Hello, iterate!.
Name:helen; Code:aaaaa
Name:helen; code:bbbbb
★〓〓〓〓〓〓〓〓★
Hello, iterate!.
  
As shown above, it is not possible to output multiple variables directly at the same time in a loop, but nesting loops in loops can achieve a complex effect.
  
To achieve simultaneous output of multiple variables in a loop, you can reference the <bean:include>, <bean:write> tag.

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.