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

来源:互联网 发布:淘宝网英雄联盟代练 编辑:程序博客网 时间:2024/06/05 13:34

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

 

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;

 


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();


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">男&nbsp;<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,它里面对类型转换配置文件做了详细的介绍。

原创粉丝点击