Struts2的Collection和Map支持
来源:互联网 发布:共射放大电路实验数据 编辑:程序博客网 时间:2024/05/22 00:40
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
- Struts2的Collection和Map支持
- Struts2的Collection和Map支持
- Struts2的Collection和Map支持
- java的Collection和Map
- Collection和Map的使用
- Map和Collection的区别
- Collection和map的区别
- Collection和Map的总结
- El表达式 (对Collection Map集合的支持)
- java的Collection和Map详解
- java的Collection和Map详解
- java的Collection和Map详解
- java的Collection和Map详解
- java的Collection和Map详解
- java的Collection和Map详解
- java的Collection和Map详解
- java里面的集合Collection和Map;
- java集合:Collection和Map的介绍
- linux图形编程前的基本操作
- 目标
- 批处理,创建新文件
- JMail发送邮件系统
- RichFaces ICEFACES 简单介绍
- Struts2的Collection和Map支持
- 解决在url中传递js变量到java程序中出现乱码问题(转自http://www.blogjava.net/fine)
- 打开QQ对话
- onmouseover, onmouseout的触发顺序问题
- 怎么成为优秀的软件模型设计者?
- JAVA2核心技术卷I:基础知识(原书第7版) -- 第5章. 继承
- IOC容器的经典解释
- 笑侃人生
- asp可用下列函数截取字符串