今天用到了struts 封装set,找了很多资料,也没有找到一个说的特别明白的,今天自己连猜带试准备成功了,现在将我的成果跟大家分享一下,
action中需要封装一个set,代码如下
public class CustomerInserterAction extends ActionSupport {
//将数据封装到orders中
private Set<Order> orders=new HashSet();
//要加上get和set方法
public Set getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
}
order是一个javabean,有一个id为其主键
封装set的重点是配置CustomerInserterAction-conversion.properties文件,我的内容如下
//配置order对象的主键 KeyProperty_orders=id //Element_集合对象名称=封装类的全程 Element_orders=test.persistence.Order //如果集合对象为空,就创建一个新的 CreateIfNull_orders=true
将此配置文件和action放在同一个目录下
在页面上这样写:
<input name="orders.makeNew[0].orderNumber" type="text" size="10">
必须要使用makeNew运算符,这样ognl才能帮我创建新的对象,否则orders会为空。当时如果集合类型是List,就不必这样,直接name="orders[0].orderNumber"就可以了。
这样就可以把页面的值封装到set中了。
本文介绍如何在Struts框架中使用set进行数据封装的方法。重点在于配置CustomerInserterAction-conversion.properties文件,通过特定的配置使得页面的数据能够正确地封装到JavaBean的set属性中。
285

被折叠的 条评论
为什么被折叠?



