目前我的代码可以修改客户名字、客户年龄,但是无法修改客户订单。
客户类
public class Customer {
private Integer cid;
private String cname;
private Integer age;
private Set<Orders> orders = new HashSet<Orders>();
getter&setter
public String toString() {
return "Customer [cid=" + cid + ", cname=" + cname + ", age=" + age + ", orders=" + orders
+ "]";
}
}
订单类
public class Orders {
private Integer oid;
private String addr;
private Customer customer;
getter&setter
public String toString() {
return "Orders [oid=" + oid + ", addr=" + addr + "]";
}
}
action
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer();
public Customer getModel() {
return customer;
}
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public String upd() throws Exception{
customerService.upd(customer);
return SUCCESS;
}
}
jsp
<body>
<s:debug></s:debug>
<form action="${pageContext.request.contextPath }/customer_upd.action" method="post">
<table border="1">
<tr style="display: none">
<td>客户id</td>
<td><input type="text" name="cid" value="<s:property value="model.cid"/>"></td>
</tr>
<tr>
<td>客户名字</td>
<td><input type="text" name="cname" value="<s:property value="model.cname"/>"></td>
</tr>
<tr>
<td>客户年龄</td>
<td><input type="text" name="age" value="<s:property value="model.age"/>"></td>
</tr>
<tr>
<td>客户订单</td>
<td>
<table border="1">
<tr>
<td>订单id</td>
<td>订单地址</td>
<td>订单客户id</td>
</tr>
<s:iterator value="model.orders" id="orders" status="st">
<tr>
<td><s:textfield name="orders.makeNew[%{#st.index}].oid" value="%{#orders.oid}" readonly="true" theme="simple"/></td>
<td><s:textfield name="orders.makeNew[%{#st.index}].addr" value="%{#orders.addr}" theme="simple"/></td>
<td><s:textfield name="orders.makeNew[%{#st.index}].customer.cid" value="%{#orders.customer.cid}" theme="simple" readonly="true"/></td>
</tr>
</s:iterator>
</table>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="修改"></td>
</tr>
</table>
</form>
</body>
参考http://bbs.csdn.net/topics/390648778?page=1
添加了CustomerAction-conversion.properties(不清楚写对了没)
KeyProperty_orders = oid
Element_orders = cn.xx.vo.Orders
CreateIfNull_orders =true
运行报错
WARN OgnlValueStack:64 - Error setting expression 'orders.makeNew[0].addrcid
oid' with value '[Ljava.lang.String;@1d6d441'
ognl.OgnlException: Error getting property descriptor: null