网上有关主从表提交的资料非常的少,可怜我刚刚入门,一切都得自己想办法解决,经过N天的努力,总算是搞定了。程序写的有点徒省事,所以请别见笑。
本来想用struts-nested标签来实现的,折腾了7个小时都搞不定,最后还是用jstl的方式搞定,最然笨了点,但是看着比较容易懂。
下面的代码是主要部分,完整代码请送附件下载(注意编码为utf-8)
用了以下包 dom4j,hibernate3,struts1.2,junit,几个常用apache-commons包,cglib,ehcache,mysql-connector,附件代码里面没有把这些库打包,如果要运行请自己网上下载吧,反正都是开源的
参考文章
www.learntechnology.net/struts-nested.do
这是CustomerAction的部分代码
java 代码
- public class CustomerAction extends DispatchAction {
-
-
-
-
-
-
-
-
-
- public ActionForward openAddCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- CustomerValueObject customer = new CustomerValueObject();
- request.setAttribute(Constants.customerBean, customer);
- return mapping.findForward("toAddCustomer");
- }
-
-
-
-
-
-
-
-
-
-
- public ActionForward saveCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- CustomerForm customerForm = (CustomerForm)form;
- Customers customer = null;
- long customerId = customerForm.getCustomer().getId();
-
- if (customerForm.getCustomer().getId() == 0){
- customer = this.customerValueObject2Customers(customerForm.getCustomer());
- }else if (customerForm.getCustomer().getId() > 0){
- String hqlString = "select c from Customers c left join fetch c.orderses o where c.id = ?";
- customer = customerDao.getObject(hqlString, new Object[]{new Long(customerId)});
-
- customer.setName(customerForm.getCustomer().getName());
- customer.getOrderses().clear();
- for(Iterator it = customerForm.getCustomer().getOrderses().iterator(); it.hasNext();){
- OrderValueObject orderVo = it.next();
- Orders o = new Orders();
- BeanUtils.copyProperties(o, orderVo);
- customer.getOrderses().add(o);
- o.setCustomers(customer);
- }
- }
- customerDao.saveObject(customer);
- return mapping.findForward("toList");
- }
<ordervalueobject>
下面是用于接收客户信息的值对象的完整代码
</ordervalueobject>
java 代码
- package demo.struts.valueobjects;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.commons.collections.Factory;
- import org.apache.commons.collections.ListUtils;
-
-
-
-
-
- public class CustomerValueObject implements java.io.Serializable {
- private static final long serialVersionUID = -5142724257544817298L;
-
- private long id;
-
- private String name;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @SuppressWarnings("unchecked")
- private List orderses = (List)ListUtils.lazyList(new ArrayList(), new Factory(){
- public OrderValueObject create() {
- return new OrderValueObject();
- }
- });
-
- public CustomerValueObject() {
- }
-
- public long getId() {
- return this.id;
- }
-
- public void setId(long id) {
- this.id = id;
- }
-
- public String getName() {
- return this.name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public List getOrderses() {
- return this.orderses;
- }
-
- public void setOrderses(List orderses) {
- this.orderses = orderses;
- }
- }
<ordervalueobject><ordervalueobject>
下面是表单bean的完整代码,我不知道用动态bean能不能实现,如果你试成功了,请不要忘了告诉我一声。
</ordervalueobject></ordervalueobject>
java 代码
- package demo.struts.forms;
-
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts.action.*;
- import demo.struts.valueobjects.*;
-
-
-
-
- public class CustomerForm extends ActionForm {
- private static final long serialVersionUID = -2128506741766449352L;
-
- private CustomerValueObject customer;
-
- public CustomerValueObject getCustomer() {
- return this.customer;
- }
-
- public void setCustomer(CustomerValueObject customer) {
- this.customer = customer;
- }
-
-
-
-
- @Override
- public void reset(ActionMapping mapping, HttpServletRequest request) {
- customer = new CustomerValueObject();
- super.reset(mapping, request);
- }
-
- @Override
- public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
- return super.validate(mapping, request);
- }
- }
这是创建客户信息的jsp页面
xml 代码
<c:url var="saveCustomerUrl">
</c:url>