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


这个应该是属于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;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值