这个应该是属于Struts2类型转换的话题,本篇主要是讨论如何将页面上表单的值批量封装到一 个Set集合中去,因为项目中用的是Hibernate,基本上所有的集合类型用的都是Set类型。至于 如何批量封装数据到其它的集合类型如(List,Map)可以参看这篇文章,说的很详细了: http://blog.csdn.net/yangqicong/article/details/6911406。如何给这个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; } } |
Struts2:类型转换之批量封装Set集合类型的属性(一)
最新推荐文章于 2024-07-14 16:41:07 发布