Struts programming learning-label logic: iterate
【Author】: Unknown <size: 1 k <Release Date: <browse: 4639 〗
Iterate is mainly used to process the output set class on the page. The set is generally one of the following:
1. array of Java objects
2. arraylist, vector, and hashmap
For details about the usage, refer to the struts document.
Now define a class, which is compiled into user. Class by user. java.
Package example;
Import java. Io. serializable;
Public final class user implements serializable {
Private string name = NULL;
Private string Password = NULL;
Public String getname (){
Return (this. Name );
}
Public void setname (string name ){
This. Name = Name;
}
Public String GetPassword (){
Return (this. Password );
}
Public void setpassword (string password ){
This. Password = password;
}
}
Create a JSP in a struts webapplication, such as iterate. jsp.
<% @ Page Language = "Java" %>
<% @ Page import = "example. *" %>
<% @ Taglib uri = "/WEB-INF/struts-bean.tld" prefix = "Bean" %>
<% @ Taglib uri = "/WEB-INF/struts-logic.tld" prefix = "logic" %>
<%
Java. util. arraylist list = new java. util. arraylist ();
User usera = new user ();
Usera. setname ("white ");
Usera. setpassword ("ABCD ");
List. Add (usera );
User userb = new user ();
Userb. setname ("Mary ");
Userb. setpassword ("hijk ");
List. Add (userb );
Session. setattribute ("list", list );
%>
& Lt; HTML & gt; & lt; Body & gt; & lt; table width = "100%" & gt;
<Logic: iterate id = "A" name = "list" type = "example. User">
& Lt; tr & gt; & lt; TD width = "50%" & gt;
Name: <Bean: write name = "A" property = "name"/>
& Lt; TD/& gt; & lt; TD width = "50%" & gt;
Password: <Bean: write name = "A" property = "password"/>
</TD> </tr>
</Logic: iterate>
</Table> </body>
Put user. Class, iterate. jsp to the corresponding directory. Run iterate. jsp and you will see the iterate effect.
Iterate flag
ID: the name of the script variable, which stores the handle of the current element in the set.
Name indicates the set to be stacked, from the attributes of the session or request.
Type is the type of the Collection class element.
The write tag of bean is used to output attributes. Name is used to match the iterate ID, and property is used to match the attributes of the corresponding class.