论坛首页 入门技术论坛

数据保存问题

浏览 2698 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-06-19  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ include file="/common/taglibs.jsp" %>

<html>
<head>
<%@include file="/common/meta.jsp" %>
<title>供应商管理</title>
<%@include file="/common/ec.jsp" %>
</head>
<body>

<div align="center">
   <table width="99%" style="border:1px #0099ee solid; background-color: #efefef">
      <tr height="30">
          <td colspan="20" bgcolor="#0099ee" align="center">
             <font color="white"><b>供应商管理</b></font>
          </td>
      </tr>
      
      <tr height="40">
       <td align= "left">
        快速查找 <select name="select">
         <option>列出所有供应商</option>
         <option>所有被禁用的供应商</option>
         <option>所有推荐的供应商</option>
       </select>导航管理:<a href="${ctx}/merchant/listMerchant.do?method=list"><img src="${ctx}/images/icons/user.jpg"/>供应商管理首页</a>|
         <a href="${ctx}/merchant/createMerchant.do?method=create"><img src="${ctx}/images/icons/user_add.gif"/> 添加供应商</a>
        </td>
        </tr>
  
   </table>
</div>
<br>

<html:form action="/merchant/saveMerchant.do?method=save">
 <html:hidden property="merchantId"/>

    <div align="center">
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0" >
 <tr>
     <td class="simple">供应商名称:</td>
     <td class="simple"><html:text property="merchantName" size="30"></html:text>*</td>   
   </tr>
   <tr>
      <td class="simple">所属类别:</td>
     <td class="simple" colspan="6">
     <html:radio property="sort" value="生产厂家">生产厂家</html:radio>
     <html:radio property="sort" value="销售公司">销售公司</html:radio>
     <html:radio property="sort" value="其它">其它</html:radio></td>
    </tr>
    <tr>
     <td class="simple">助记码:</td>
     <td class="simple"><html:text property="memoryCode" size="30"></html:text>*</td>   
   </tr>
    <tr>
     <td class="simple">公司地址:</td>
     <td class="simple"><html:text property="address" size="30"></html:text></td>   
   </tr>
   <tr>
     <td class="simple">联系电话:</td>
     <td class="simple"><html:text property="phone" size="30"></html:text></td>   
   </tr>
   <tr>
     <td class="simple">传真号码:</td>
     <td class="simple"><html:text property="fax" size="30"></html:text></td>   
   </tr>
   <tr>
     <td class="simple">电子邮件:</td>
     <td class="simple"><html:text property="EMail" size="30"></html:text></td>   
   </tr>
    <tr>
     <td class="simple">公司简介:</td>
     <td class="simple"><html:textarea property="introduction" cols="50" rows="6" ></html:textarea></td>   
   </tr>
   <tr>   
    <td colspan="30" align="center" class="simple">
    <input type="submit" value=" 保存 " style="border:1px">  
    <input type="reset" value=" 清空 " style="border:1px">
    </td>
  </tr>
 </table></div>
</html:form>

</html>

这是一个页面数据

package com.systop.practice.model;

// Generated 2007-6-8 10:45:24 by Hibernate Tools 3.2.0.beta8

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

/**
 * Merchant generated by hbm2java
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "merchant")
public class Merchant implements java.io.Serializable {

 /**
  * 供应商编号
  **/ 

 private int merchantId;
 /**
  * 生产厂家
  **/   
 private String merchantName;
 /**
  * 供应商类型
  **/   
 private String sort;
 /**
  * 创建日期
  **/ 
 private Date buildDate;
 /**
  * 地址
  **/ 
 private String address;
 /**
  * 电话
  **/ 
 private Integer phone;
 /**
  * 传真
  **/ 
 private Integer fax;
 /**
  * 邮箱
  **/ 
 private String EMail;
 /**
  * 助记码
  **/ 
 private String memoryCode;
 /**
  * 简介
  **/  
 private String introduction;
 /**
  * 得到所有的品牌
  **/ 
 private Set<Trademark> trademarks = new HashSet<Trademark>(0);
 /**
  * 得到所有的采购单
  **/ 
 private Set<Stock> stocks = new HashSet<Stock>(0);

 // Constructors

 /** default constructor */
 public Merchant() {
 }

 /** minimal constructor */
 public Merchant(int merchantId) {
  this.merchantId = merchantId;
 }

 /** full constructor */
 public Merchant(int merchantId, String sort, Date buildDate,
   String address, Integer phone, Integer fax, String EMail,
   String memoryCode, Set<Trademark> trademarks, Set<Stock> stocks) {
  this.merchantId = merchantId;
  this.sort = sort;
  this.buildDate = buildDate;
  this.address = address;
  this.phone = phone;
  this.fax = fax;
  this.EMail = EMail;
  this.memoryCode = memoryCode;
  this.trademarks = trademarks;
  this.stocks = stocks;
 }

 // Property accessors
 @Id
 @GeneratedValue(generator = "inc")
 @GenericGenerator(name = "inc", strategy = "increment")
 @Column(name = "MERCHANT_ID", unique = true, nullable = true)
 public Integer getMerchantId() {
  return this.merchantId;
 }

 public void setMerchantId(Integer merchantId) {
  this.merchantId = merchantId;
 }

 @Column(name = "SORT", unique = false, nullable = true,
   insertable = true, updatable = true)
 public String getSort() {
  return this.sort;
 }

 public void setSort(String sort) {
  this.sort = sort;
 }

 @Temporal(TemporalType.DATE)
 @Column(name = "BUILD_DATE", unique = false, nullable = true,
   insertable = true, updatable = true)
 public Date getBuildDate() {
  return this.buildDate;
 }

 public void setBuildDate(Date buildDate) {
  this.buildDate = buildDate;
 }

 @Column(name = "ADDRESS", unique = false, nullable = true,
   insertable = true, updatable = true)
 public String getAddress() {
  return this.address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 @Column(name = "PHONE", unique = false, nullable = true,
   insertable = true, updatable = true)
 public Integer getPhone() {
  return this.phone;
 }

 public void setPhone(Integer phone) {
  this.phone = phone;
 }

 @Column(name = "FAX", unique = false, nullable = true,
   insertable = true, updatable = true)
 public Integer getFax() {
  return this.fax;
 }

 public void setFax(Integer fax) {
  this.fax = fax;
 }

 @Column(name = "E_MAIL", unique = false, nullable = true,
   insertable = true, updatable = true)
 public String getEMail() {
  return this.EMail;
 }

 public void setEMail(String EMail) {
  this.EMail = EMail;
 }

 @Column(name = "MEMORY_CODE", unique = false, nullable = true,
   insertable = true, updatable = true)
 public String getMemoryCode() {
  return this.memoryCode;
 }

 public void setMemoryCode(String memoryCode) {
  this.memoryCode = memoryCode;
 }

 @OneToMany(cascade = { CascadeType.ALL },
   fetch = FetchType.LAZY, mappedBy = "merchant")
 public Set<Trademark> getTrademarks() {
  return this.trademarks;
 }

 public void setTrademarks(Set<Trademark> trademarks) {
  this.trademarks = trademarks;
 }

 @OneToMany(cascade = { CascadeType.ALL },
   fetch = FetchType.LAZY, mappedBy = "merchant")
 public Set<Stock> getStocks() {
  return this.stocks;
 }

 public void setStocks(Set<Stock> stocks) {
  this.stocks = stocks;
 }
 @Column(name = "MERCHANT_NAME", unique = false,
   nullable = true, insertable = true, updatable = true)
 public String getMerchantName() {
  return merchantName;
 }

 public void setMerchantName(String merchantName) {
  this.merchantName = merchantName;
 }

 /**
  * @return the introduction
  */
 @Column(name = "INTRODUCTION", unique = false,
   nullable = true, insertable = true, updatable = true)
 public String getIntroduction() {
  return introduction;
 }

 /**
  * @param introduction the introduction to set
  */
 public void setIntroduction(String introduction) {
  this.introduction = introduction;
 }

 

}

这是一个实体

 

package com.systop.practice.merchant.webapp;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.DetachedCriteria;
import com.systop.practice.core.webapp.struts.StrutsAction;
import com.systop.practice.merchant.service.MerchantManager;
import com.systop.practice.model.Merchant;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
/**
 * Merchant的struts action,由于数据量小,所以直接使用StrutsAction,而不是
 * 使用ECPageQueryStrutsAction
 * @author ljy
 *
 */
public class MerchantAction extends StrutsAction {
 /**
    * 从spring中得到DeptManager
    */
   private MerchantManager getMerchantMgr() {
     return (MerchantManager) getBean("merchantManager");
   }
   /**
    * 列出所有生产厂家
    */
   public ActionForward list(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
   //创建一个离线条件查询
  DetachedCriteria criteria = DetachedCriteria.forClass(Merchant.class);
//  处理条件查询
     String merchantserch = request.getParameter("merchantSerch");
     String value = request.getParameter("matter");
     if (StringUtils.isNotBlank(merchantserch)) {
      criteria.add(Restrictions.like(merchantserch,
        value, MatchMode.ANYWHERE));
     }
  List merchants = getMerchantMgr().get(Merchant.class);
     request.setAttribute("merchants", merchants); //把生产厂家列表放到Request中
    
     return mapping.findForward("list");
    
   }
   /**
    * 添加生产厂家前,将页面定位到merchantForm.jsp
    * @return
    */
   public ActionForward create(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
  
     return mapping.findForward("edit");
   }
  
   /**
    * 编辑生产厂家前,将页面定位到merchantForm.jsp
    * @return
    */
   public ActionForward edit(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
     Merchant merchant = new Merchant();
     bindEntity(form, merchant);  
     log.debug(merchant.getMerchantId());
     if (merchant != null && merchant.getMerchantId() != null) {
      merchant = getMerchantMgr().get(Merchant.class,
        merchant.getMerchantId());
     }
     bindForm(form, merchant);
   
     return mapping.findForward("edit");
   }
  
   /**
    * 保存生产厂家信息,包括更新和添加
    * @return
    */
   public ActionForward save(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {

     Merchant merchant = new Merchant();

     this.bindEntity(form, merchant);

     //将页面数据提取为merchant对象
     getMerchantMgr().save(merchant);   
     return mapping.findForward("success");
   }
  
   /**
    * 删除生产厂家信息
    * @return
    */
   public ActionForward remove(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
     //获得页面数据
  String []merchantIds = request.getParameterValues("selectedItems");
      
     if (merchantIds != null && merchantIds.length > 0) {
       for (int i = 0; i < merchantIds.length; i++) {
         if (StringUtils.isNotBlank(merchantIds[i])) {
           Merchant merchant = getMerchantMgr().get(Merchant.class ,
             Integer.valueOf(merchantIds[i]));
             if (merchant != null) {
              getMerchantMgr().remove(merchant);
             log.debug("Remove Merchant" + merchant.getMerchantId());
           }
         }
       }
     }
     return mapping.findForward("success");
   }
}


这是我的Action

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
  "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
 <form-beans>  
  <form-bean name="merchantForm" type="org.apache.struts.validator.LazyValidatorForm"/>
 </form-beans>

 <action-mappings>
  
  <action path="/merchant/listMerchant" name="merchantForm" type="com.systop.practice.merchant.webapp.MerchantAction"
   parameter="method" scope="request" validate="false" >
   <forward name="list" path="/pages/merchant/listMerchants.jsp"/>   
  </action>
  <action path="/merchant/saveMerchant" name="merchantForm" type="com.systop.practice.merchant.webapp.MerchantAction"
   parameter="method" scope="request" validate="true" input="/pages/merchant/merchantForm.jsp" >
   <forward name="success" path="/merchant/listMerchant.do?method=list" redirect="true"/>
  </action>
  <action path="/merchant/editMerchant" name="merchantForm" type="com.systop.practice.merchant.webapp.MerchantAction"
   parameter="method" scope="request" validate="false" >
   <forward name="edit" path="/pages/merchant/merchantForm.jsp"/>   
  </action>
  <action path="/merchant/createMerchant" name="merchantForm" type="com.systop.practice.merchant.webapp.MerchantAction"
   parameter="method" scope="request" validate="false" >
   <forward name="edit" path="/pages/merchant/merchantForm.jsp"/>   
  </action>
  <action path="/merchant/removeMerchant" name="merchantForm" type="com.systop.practice.merchant.webapp.MerchantAction"
   parameter="method" scope="request" validate="false" >
   <forward name="success" path="/merchant/listMerchant.do?method=list" redirect="true"/>  
  </action>
 </action-mappings>

</struts-config>

这是STRUTS部分

问题:只能保存添加的内容。修改完以后就作为一条新记录添加进数据库,请帮助看一下问题也在什么

地方!!!!!

   发表时间:2007-06-19  
你持久层用的是hibernate吧,你的问题应该是出在,修改的时候你没有获取到实例对象主建值。
0 请登录后投票
论坛首页 入门技术版

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