论坛首页 Java企业应用论坛

一个简单的主从表的增删改,采用 struts+hibernate

浏览 8147 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-12-24  
         网上有关主从表提交的资料非常的少,可怜我刚刚入门,一切都得自己想办法解决,经过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 代码
 
  1. public class CustomerAction extends DispatchAction {  
  2.     /** 
  3.      * 打开客户增加页面 
  4.      * @param mapping 
  5.      * @param form 
  6.      * @param request 
  7.      * @param response 
  8.      * @return 
  9.      * @throws Exception 
  10.      */  
  11.     public ActionForward openAddCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  
  12.         CustomerValueObject customer = new CustomerValueObject();  
  13.         request.setAttribute(Constants.customerBean, customer);  
  14.         return mapping.findForward("toAddCustomer");  
  15.     }  
  16.   
  17.     /** 
  18.      * 保存客户信息 
  19.      * @param mapping 
  20.      * @param form 
  21.      * @param request 
  22.      * @param response 
  23.      * @return 
  24.      * @throws Exception 
  25.      */  
  26.     public ActionForward saveCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  
  27.         CustomerForm customerForm = (CustomerForm)form;  
  28.         Customers customer = null;  
  29.         long customerId = customerForm.getCustomer().getId();  
  30.         //根据customers.id是否为空判断是插入还是更新  
  31.         if (customerForm.getCustomer().getId() == 0){  
  32.             customer = this.customerValueObject2Customers(customerForm.getCustomer());  
  33.         }else if (customerForm.getCustomer().getId() > 0){  
  34.             String hqlString = "select c from Customers c left join fetch c.orderses o where c.id = ?";  
  35.             customer = customerDao.getObject(hqlString, new Object[]{new Long(customerId)});  
  36.             //或许这里可以用BeanUtils.copyProperty来复制bean的属性,但我试过了,总是报错,索性自己赋值了  
  37.             customer.setName(customerForm.getCustomer().getName());  
  38.             customer.getOrderses().clear();  
  39.             for(Iterator it = customerForm.getCustomer().getOrderses().iterator(); it.hasNext();){  
  40.                 OrderValueObject orderVo = it.next();  
  41.                 Orders o = new Orders();  
  42.                 BeanUtils.copyProperties(o, orderVo);  
  43.                 customer.getOrderses().add(o);  
  44.                 o.setCustomers(customer);  
  45.             }  
  46.         }  
  47.         customerDao.saveObject(customer);  
  48.         return mapping.findForward("toList");  
  49.     }  
<ordervalueobject>

下面是用于接收客户信息的值对象的完整代码

</ordervalueobject>
java 代码
 
  1. package demo.struts.valueobjects;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.commons.collections.Factory;  
  7. import org.apache.commons.collections.ListUtils;  
  8. /** 
  9.  * 客户值对象,用于接收表单提交的客户和订单信息 
  10.  * @author Administrator 
  11.  * 
  12.  */  
  13. public class CustomerValueObject implements java.io.Serializable {  
  14.     private static final long serialVersionUID = -5142724257544817298L;  
  15.   
  16.     private long id;  
  17.   
  18.     private String name;  
  19.   
  20.     /** 
  21.      * 这是关键的一部分,也是和hibernate所用的pojo的唯一区别,这样做是为了解决在没有列表项目的时候,前端页面会报获取第0个元素失败的异常, 
  22.      * 这个工具类很好的解决了这个问题,它可以在你访问越界下标元素的时候动态的填充越界的部分,这样就不会报异常了。 
  23.      * 下面是这个LazyList的关键代码 
  24.      *     public Object get(int index) { 
  25.         int size = getList().size(); 
  26.         if (index < size) { 
  27.             // within bounds, get the object 
  28.             Object object = getList().get(index); 
  29.             if (object == null) { 
  30.                 // item is a place holder, create new one, set and return 
  31.                 object = factory.create(); 
  32.                 getList().set(index, object); 
  33.                 return object; 
  34.             } else { 
  35.                 // good and ready to go 
  36.                 return object; 
  37.             } 
  38.         } else { 
  39.             // we have to grow the list 
  40.             for (int i = size; i < index; i++) { 
  41.                 getList().add(null); 
  42.             } 
  43.             // create our last object, set and return 
  44.             Object object = factory.create(); 
  45.             getList().add(object); 
  46.             return object; 
  47.         } 
  48.     } 
  49.       */  
  50.     @SuppressWarnings("unchecked")  
  51.     private List orderses = (List)ListUtils.lazyList(new ArrayList(), new Factory(){  
  52.         public OrderValueObject create() {  
  53.             return new OrderValueObject();  
  54.         }  
  55.     });  
  56.   
  57.     public CustomerValueObject() {  
  58.     }  
  59.   
  60.     public long getId() {  
  61.         return this.id;  
  62.     }  
  63.   
  64.     public void setId(long id) {  
  65.         this.id = id;  
  66.     }  
  67.   
  68.     public String getName() {  
  69.         return this.name;  
  70.     }  
  71.   
  72.     public void setName(String name) {  
  73.         this.name = name;  
  74.     }  
  75.   
  76.     public List getOrderses() {  
  77.         return this.orderses;  
  78.     }  
  79.   
  80.     public void setOrderses(List orderses) {  
  81.         this.orderses = orderses;  
  82.     }  
  83. }  
<ordervalueobject><ordervalueobject>
下面是表单bean的完整代码,我不知道用动态bean能不能实现,如果你试成功了,请不要忘了告诉我一声。
</ordervalueobject></ordervalueobject>
java 代码
 
  1. package demo.struts.forms;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import org.apache.struts.action.*;  
  5. import demo.struts.valueobjects.*;  
  6.   
  7. /** 
  8.  * ActionForm的代码,里面负责维护一个客户的值对象 
  9.  */  
  10. public class CustomerForm extends ActionForm {  
  11.     private static final long serialVersionUID = -2128506741766449352L;  
  12.   
  13.     private CustomerValueObject customer;  
  14.   
  15.     public CustomerValueObject getCustomer() {  
  16.         return this.customer;  
  17.     }  
  18.   
  19.     public void setCustomer(CustomerValueObject customer) {  
  20.         this.customer = customer;  
  21.     }  
  22.   
  23.     /** 
  24.      * 必须在reset中创建客户对象 
  25.      */  
  26.     @Override  
  27.     public void reset(ActionMapping mapping, HttpServletRequest request) {  
  28.         customer = new CustomerValueObject();  
  29.         super.reset(mapping, request);  
  30.     }  
  31.   
  32.     @Override  
  33.     public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {  
  34.         return super.validate(mapping, request);  
  35.     }  
  36. }  


这是创建客户信息的jsp页面
xml 代码
 
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  3. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>  
  4. <%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>  
  5.   
  6. >  
  7. <html>  
  8.     <head>  
  9.         <html:base />  
  10.   
  11.         <title>新客户和订单<!---->title>  
  12.   
  13.         <meta http-equiv="pragma" content="no-cache">  
  14.         <meta http-equiv="cache-control" content="no-cache">  
  15.         <meta http-equiv="expires" content="0">  
  16.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.         <meta http-equiv="description" content="This is my page">  
  18.         <!---->  
  19.         <script type="text/javascript">  
  20.         function onAddOrder(url){  
  21.             frmCustomer.action = url;  
  22.             frmCustomer.target = "_self";  
  23.             frmCustomer.submit();  
  24.         }  
  25.   
  26.         function onDeleteOrder(url){  
  27.             frmCustomer.action = url;  
  28.             frmCustomer.submit();  
  29.         }  
  30.   
  31.         function onSaveCustomer(url){  
  32.             frmCustomer.action = url;  
  33.             frmCustomer.submit();  
  34.         }  
  35.     <!---->script>  
  36.     <!---->head>  
  37.     <body>  
  38.         <table>  
  39.             <form name="frmCustomer" method="post">  
  40.                 <input type="hidden" name="customer.id" value="${customer.id}" />  
  41.                 <table>  
  42.                     <tr>  
  43.                         <td>  
  44.                             客户名  
  45.                         <!---->td>  
  46.                         <td>  
  47.                             <input name="customer.name" value="${customer.name}" />  
  48.                         <!---->dh>  
  49.                     <!---->tr>  
  50.                     <tr>  
  51.                         <td colspan="2">  
  52.                             订单列表  
  53.                         <!---->td>  
  54.                     <!---->tr>  
  55.                     <tr>  
  56.                         <div id="orderList">  
  57.                             <table bgcolor="#C0C0C0" width="250px">  
  58.                                 <tr>  
  59.                                     <td>  
  60.                                            
  61.                                     <!---->td>  
  62.                                     <td>  
  63.                                         订单号  
  64.                                     <!---->td>  
  65.                                 <!---->tr>  
  66.                                     <c:forEach items="${customer.orderses}" var="order" varStatus="orderItems">  
  67.                                         <tr>  
  68.                                             <td>  
  69.                                                 <input type="hidden" name="customer.orderses[${orderItems.index}].id" value="${order.id}" />  
  70.                                             <!---->td>  
  71.                                             <td>  
  72.                                                 <input type="text" name="customer.orderses[${orderItems.index}].orderNumber" value="${order.orderNumber}" />  
  73.                                             <!---->td>  
  74.                                             <td>  
  75.                                                 <c:url var="deleteOrderUrl" value="/customers.do?method=deleteOrder">  
  76.                                                     <c:param name="index" value="${orderItems.index}" />  
  77.                                                     <c:param name="action" value="add" />  
  78.                                                 <!---->c:url>  
  79.                                                 <a href="javascript:onDeleteOrder('${deleteOrderUrl}');">删除订单<!---->a>  
  80.                                             <!---->td>  
  81.                                         <!---->tr>  
  82.                                     <!---->c:forEach>  
  83.                                 <tr>  
  84.                                     <td align="right" colspan="2">  
  85.                                         <c:url var="addOrderUrl" value="/customers.do?method=addOrder">  
  86.                                             <c:param name="action" value="add" />  
  87.                                         <!---->c:url>  
  88.                                         <a href="javascript:onAddOrder('${addOrderUrl}');">添加订单<!---->a>  
  89.                                     <!---->td>  
  90.                                 <!---->tr>  
  91.                             <!---->table>  
  92.                         <!---->div>  
  93.                     <!---->tr>  
  94.                 <!---->table>  
  95.             <!---->form>  
  96.             <tr>  
  97.                 <td colspan="2">  
  98.                     <c:url var="saveCustomerUrl"  
  99.                         value="/customers.do?method=saveCustomer" />  
  100.                     <html:link href="javascript:onSaveCustomer('${saveCustomerUrl}');">保存客户表单<!---->html:link>  
  101.                 <!---->td>  
  102.             <!---->tr>  
  103.         <!---->table>  
  104.     <!---->body>  
  105. <!---->html> 
<c:url var="saveCustomerUrl">
</c:url>
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics