struts2提供了从页面直接封装到List的功能,实现起来有两个办法
1.使用泛型定义集合中的对象类型
2.配合配置文件定义集合中的对象类型
User.java
package
HelloWorld;
public class User ... {
private String username;
private String password;
private String[] books;
public String[] getBooks() ...{
return books;
}
public void setBooks(String[] books) ...{
this.books = books;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
}
public class User ... {
private String username;
private String password;
private String[] books;
public String[] getBooks() ...{
return books;
}
public void setBooks(String[] books) ...{
this.books = books;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
}
LoginAction.java (不使用泛型,使用配置文件定义集合中对象的类型)
package
HelloWorld;
import java.util.List;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action ... {
private String tip;
private List users;
public List getUsers() ...{
return users;
}
public void setUsers(List users) ...{
this.users = users;
}
public String execute() throws Exception...{
//用第一个user做逻辑判断
if(((User)this.getUsers().get(0)).getUsername().equals("admin")&&((User)this.getUsers().get(0)).getPassword().equals("1234"))...{
ActionContext.getContext().getSession().put("user", ((User)this.getUsers().get(0)).getUsername());
BookService bs=new BookService();
((User)this.getUsers().get(0)).setBooks(bs.getBooks());
this.setTip("welcome welcome");
return SUCCESS;
}else...{
return ERROR;
}
}
public String getTip() ...{
return tip;
}
public void setTip(String tip) ...{
this.tip = tip;
}
}
import java.util.List;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action ... {
private String tip;
private List users;
public List getUsers() ...{
return users;
}
public void setUsers(List users) ...{
this.users = users;
}
public String execute() throws Exception...{
//用第一个user做逻辑判断
if(((User)this.getUsers().get(0)).getUsername().equals("admin")&&((User)this.getUsers().get(0)).getPassword().equals("1234"))...{
ActionContext.getContext().getSession().put("user", ((User)this.getUsers().get(0)).getUsername());
BookService bs=new BookService();
((User)this.getUsers().get(0)).setBooks(bs.getBooks());
this.setTip("welcome welcome");
return SUCCESS;
}else...{
return ERROR;
}
}
public String getTip() ...{
return tip;
}
public void setTip(String tip) ...{
this.tip = tip;
}
}
LoginAction_conversion.properties (放在和LoginAction同目录)
Element_users=HelloWorld.User
表示集合中的Element类型是User,如果是Map类型,则还需要定义Key_users=****
index.jsp
<%
...
@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312" %>
<% ... @ taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< title ></ title >
</ head >
< body >
< s:form action ="Login" >
< s:textfield name ="users[0].username" key ="username" ></ s:textfield >
< s:textfield name ="users[0].password" key ="password" ></ s:textfield >
< s:textfield name ="users[1].username" key ="username" ></ s:textfield >
< s:textfield name ="users[1].password" key ="password" ></ s:textfield >
< s:submit value ="login" ></ s:submit >
</ s:form >
</ body >
</ html >
pageEncoding="gb2312" %>
<% ... @ taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< title ></ title >
</ head >
< body >
< s:form action ="Login" >
< s:textfield name ="users[0].username" key ="username" ></ s:textfield >
< s:textfield name ="users[0].password" key ="password" ></ s:textfield >
< s:textfield name ="users[1].username" key ="username" ></ s:textfield >
< s:textfield name ="users[1].password" key ="password" ></ s:textfield >
< s:submit value ="login" ></ s:submit >
</ s:form >
</ body >
</ html >
如果使用泛型定义List users,则可以省略LoginAction-conversion.properties文件
private List<List> users