这个应该是属于Struts2类型转换的话题,本篇主要是讨论如何将页面上表单的值批量封装到一个Set集合中去,因为项目中用的是Hibernate,基本上所有的集合类型用的都是Set类型。至于如何批量封装数据到其它的集合类型如(List,Map)可以参看这篇文章,说的很详细了:http://hi.baidu.com/wava/blog/item/01d58926c42dac158b82a134.html。如何给这个Set属性赋值花费了我相当的时间,下面直接贴例子吧。
CustomerInserterAction.java:处理请求,含有一个Set类型的属性orders
package test.web;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import test.persistence.Customer;
import test.persistence.Order;
import test.service.ICustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
/**
* @author zhukai 2007-8-31
*
*/
public class CustomerInserterAction extends ActionSupport {
private Customer customer;
private Set<Order> orders=new HashSet();
private ICustomerService customerService;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
//customer.setOrders(orders);
this.customer = customer;
}
public String execute() {
customer.setOrders(orders);
for(Order order:orders)
order.setCustomer(customer);
customerService.addCustomer(customer);
return SUCCESS;
}
public Set getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
public ICustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(ICustomerService customerService) {
this.customerService = customerService;
}
}
Customer.java和Order.java:两个实体类,分别代表一个消费者和一个订单,并且一个消费
者可以有多个订单,而一个订单只属于一个消费者。
package test.persistence;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* @author zhukai
*/
public class Customer implements Serializable {
private Long id;
private String name;
private String email;
private String password;
private String phone;
private boolean married;
private String address;
private char sex;
private String description;
private Date birthday;
private Date registeredTime;
private Set orders = new HashSet();
/**
* hibernate要求持久化类必须提供一个不带参数的默认构造方法
*/
public Customer() {
// TODO Auto-generated constructor stub
}
public Customer(String name) {
super();
this.name = name;
}
public Customer(String name, Set orders) {
super();
this.name = name;
this.orders = orders;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;//
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getRegisteredTime() {
return registeredTime;
}
public void setRegisteredTime(Date registeredTime) {
this.registeredTime = registeredTime;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public Set getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
}
reg.jsp:前台页面,用于输入数据
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<div style="color:red">
<s:fielderror />
</div>
<form action="register.action" method="post">
<table width="389" border="1" align="center">
<tr>
<th height="30" colspan="4" scope="col"></th>
</tr>
<tr>
<td width="65" height="30">姓名</td>
<td width="68"><input name="customer.name" type="text" id="customer.name" size="10"></td>
<td width="75">电子邮箱</td>
<td width="153"><input name="customer.email" type="text" id="customer.email" size="10"></td>
</tr>
<tr>
<td height="30">地址</td>
<td><input name="customer.address" type="text" id="customer.address" size="10"></td>
<td>电话</td>
<td><input name="customer.phone" type="text" id="customer.phone" size="10"></td>
</tr>
<tr>
<td height="30">密码</td>
<td><input name="customer.password" type="text" id="customer.address" size="10"></td>
<td>性别</td>
<td><input name="customer.sex" type="radio" id="customer.sex" size="10" value="M" checked="checked">男 <input name="customer.sex" type="radio" id="customer.sex" size="10" value="F">女</td>
</tr>
<tr>
<td height="30">出生日期</td>
<td><input name="customer.birthday" type="text" id="customer.address" size="10"></td>
<td>注册日期</td>
<td><input name="customer.registerTime" type="text" id="customer.phone" size="10"></td>
</tr>
<tr>
<td height="30" colspan="4">订单一</td>
</tr>
<tr>
<td height="30">订单号</td>
<td><input name="orders.makeNew[0].orderNumber" type="text" size="10"></td>
<td>配送地址</td>
<td><input name="orders.makeNew[0].address" type="text" size="10"></td>
</tr>
<tr>
<td height="30" colspan="4">订单二</td>
</tr>
<tr>
<td height="30">订单号</td>
<td><input name="orders.makeNew[1].orderNumber" type="text" size="10"></td>
<td>配送地址</td>
<td><input name="orders.makeNew[1].address" type="text" size="10"></td>
</tr>
<tr>
<td height="30" colspan="4">订单三</td>
</tr>
<tr>
<td height="30">订单号</td>
<td><input name="orders.makeNew[2].orderNumber" type="text" size="10"></td>
<td>配送地址</td>
<td><input name="orders.makeNew[2].address" type="text" size="10"></td>
</tr>
<tr>
<td height="30" colspan="4"><input type="submit" value="提交"><input type="reset" value="重置"> </td>
</tr>
<tr>
</table>
</form>
</body>
</html>
CustomerInserterAction-conversion.properties:类型转换的配置文件,我的内容如下
KeyProperty_orders=id
Element_orders=test.persistence.Order
CreateIfNull_orders=true
有几个要说明的地方,这时封装Set集合时必须要注意的:
1.CustomerInserterAction.java中对orders必须要初始化,如下所示:
private Set<Order> orders=new HashSet();
2.在jsp页面中给变量命名时必须要按照如下的格式:
<input name="orders.makeNew[0].orderNumber" type="text" size="10">
必须要使用makeNew运算符,这样ognl才能帮我创建新的对象,否则orders会为空。当时如果集合类型是List,就不必这样,直接name="orders[0].orderNumber"就可以了。
3.在Class-conversion.properties文件中加上KeyProperty_orders=id,这是封装Set时必须的。这个主要是用来设置检索集合中元素时的索引,具体含义可以参看http://wiki.javascud.org/display/ww2cndoc/Type%20Conversion,它里面对类型转换配置文件做了详细的介绍。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1799836
分享到:
相关推荐
struts2中的文件上传和下载: 1.首先导入commons-fileupload-...3.再次,struts2中文件上传将底层高度封装了。在使用时只需要在ACTION中添加三个字段(其中 List<File> files为必填项),并为其设置get set 属性。
书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。...
4. 为Bean配置集合(List,Set,Map) 266 5. 调用构造方法创建Bean 271 6. 指定Bean引用与自动装配 276 (二) AOP 278 1. AOP简介 278 (三) Spring 3.X 新特性(JavaConfig,SpEL)简介 285 1. JavaConfig 285 2. SpEL...
需要指定的是Spring 1.2.5提供了两套Hibernate的支持包,其中Hibernate 2相关的封装类位于org.springframework.orm.hibernate2.*包中,而Hibernate 3.0的封装类位于org.springframework.orm.hibernate3.*包中,...
Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design. This definition implies that Struts is a framework, rather than a library, but Struts also contains...
,注意load()方法自动将相关参数set到formbean中去不需要用户再调用request.getParameter()。 4.send到szdw.jsp即用户看到的师资队伍。 还有一种情况就是用户所需要接收的参数不在数据库表中,那么调用load()是得不...
Struts2只是起到一个数据接收和转接的功能,就是Controller控制器,而传来数据的页面叫view显示层,Struts2将数据提交给进行处理的类叫Model模型层,专门进行数据处理和数据库的连接。 heap和stack有什么区别? 答:...
集合框架:List、Set、Map的使用及其实现原理。 Java I/O:文件操作、流处理。 多线程编程:线程的创建、同步机制、并发工具。 网络编程:Socket编程、URL和URLConnection。 数据库连接:JDBC的使用和数据库交互。 ...
Collection是集合类的上级接口,继承与他的接口主要有Set 和List. Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。 13、&和&&的区别。 &是位运算符...
Collection是集合类的上级接口,继承与他的接口主要有Set 和List. Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。 10、&和&&的区别。 &是位运算符...
数据结构,如何遍历List中的元素? 如果要按照键值保存或者访问数据,使用什么数据结构? 要掌握Collection相关的接口和类的使用 56.使用StringBuffer类与String类进行字符串连接时有何区别? 57.调用Thread类的...