您的位置:首页 > 编程语言 > Java开发

Struts2:类型转换之批量封装Set集合类型的属性(一)

2011-10-27 17:08 453 查看
这个应该是属于Struts2类型转换的话题,本篇主要是讨论如何将页面上表单的值批量封装到一
个Set集合中去,因为项目中用的是Hibernate,基本上所有的集合类型用的都是Set类型。至于
如何批量封装数据到其它的集合类型如(List,Map)可以参看这篇文章,说的很详细了:
/article/10764524.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;
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: