One of the most common applications for JSP/servlet development is to display a list of entries (such as the user list,ArticleList, item list, and so on), and then the user selects to operate a certain entry based on browsing. Now let's talk about how to use struts to implement this function.
In general, applications developed with Struts should not directly access the. jsp file, but forward requests by action. jsp only displays data transmitted by action. Therefore, even a simple application requires an action.
1. Use the easy struts wizard, select File> New> other from the menu, and then select easy action. In this case, a wizard window is opened. The Wizard has only two steps, which is less than the step for creating form bean in learning diary 1.
2. We can name the action to be created as listitems, that is, enter listitems in the User case box. Modify the package name as needed. Click Next to continue.
3. Add a forward named success with path/form/listitems. jsp. Click Finish to complete the wizard.
4. Edit the generated listitemsaction. Java file and modify the execute file as follows:
Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {
User user = new user ();
List items = new arraylist ();
Items. Add (new item ("001", "medicine "));
Items. Add (new item ("002", "ticket "));
Items. Add (new item ("003", "Clothes "));
User. setitems (items );
Request. getsession (). setattribute ("user", user );
Return (mapping. findforward ("success "));
}
Here, both user and item are classes written by ourselves. A user can have multiple items, and item has two attributes: ID and name. DetailsCodeSee later.
5. Create a file named listitems. jsp under/form. The content is as follows:
<% @ taglib uri = "/WEB-INF/struts-bean.tld" prefix = "Bean" %>
<% @ taglib uri = "/WEB-INF/struts-html.tld" prefix =" html "%>
<% @ taglib uri ="/WEB-INF/struts-logic.tld "prefix =" logic "%>
itemlist
The key here is the <logic: iterate> label. In this example, it is used to traverse the items attribute of the object where the key in the session or request is the user, you can use item to reference each object (<Bean: write name = "item">) during traversal ). <Bean: Write> used to output different attributes of each object. Filter = "true" indicates converting special characters, for example, <to & lt.
6. In this way, you can view an entry list through http: // localhost: 8080/Struts-test/listitems. Do, as shown below:
| 001 |
Medicine |
| 002 |
Ticket |
| 003 |
Clothes |
7, we can modify the struts-config.xml file, let the user login successful direct to the entry list page. You only need to change the forward path of the original logon action named success from/form/main.htm to/listitems. Do.
Appendix:
User. Java code:
Public class user {
Private list items;
Public user (){
Items = new arraylist ();
}
Public list getitems (){
Return items;
}
Public void setitems (list ){
Items = List;
}
}
Item. Java code:
Public class item {
Private string ID;
Private string name;
Public item (string arg0, string arg1 ){
Id = arg0;
Name = arg1;
}
Public String GETID (){
Return ID;
}
Public String getname (){
Return name;
}
Public void setid (string ){
Id = string;
}
Public void setname (string ){
Name = string;
}
}