类2
import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.kucheTV.dao.DAOFactory;
import com.kucheTV.dao.MaterialDAO;
import com.kucheTV.domain.Material;
import com.kucheTV.struts.form.MaterialForm;;
public class AddMaterial extends Action {
private MaterialDAO materDao; public MaterialDAO getMaterialDAO() {
return DAOFactory.getInstance().createMaterialDAO();
} public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// ActionMessages errors = new ActionMessages(); MaterialForm materForm = (MaterialForm) form; materDao = this.getMaterialDAO();

Material mater =new Material();
try {
mater.setMaid( new String(materForm.getMaid().getBytes("ISO-8859-1"),"GB2312") );
mater.setMaterial( new String(materForm.getMaterial().getBytes("ISO-8859-1"),"GB2312"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} if (materDao.saveMaterial(mater)) {

return (mapping.findForward("success"));
}else{

return (mapping.getInputForward());

}
}}

解决方案 »

  1.   

    用到的JSP页面<%@ page language="java" pageEncoding="GB2312"%>
    <%@ page import="java.sql.*,com.kucheTV.service.*"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html lang="true">
      <head>
        <html:base />
        
        <title>增加材料</title> <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        <p>
    <%
    /*
    String admin_id = (String) session.getAttribute("id");
    if (admin_id == null) {
    response.sendRedirect("login.jsp");
    }
                */
    %>
    </p>
    <p align="center"> 
    新增材料
    </p>
    <form method="post" action="/kucheTV/addMaterial.do">
    <table width="49%" height="50" border="1" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td width="48%">
    材料编号
    </td>
    <td width="52%">
    <html:text property="maid" value="" />
    </td>
    <tr>
    <td width="48%">
    材料名称
    </td>
    <td width="52%">
    <html:text property="material" value="" />
    </td>
    </tr>
    <tr>
    <td width="48%">
    材料数量
    </td>
    <td width="52%">
    <html:text property="manumber" value="" />
    </td>
    </tr>


    </table>
    <p align="center">
    <input type="submit" name="Submit" value="提交">
    </p>
    </form>
    <a href="getMaterial.jsp">&lt;&lt;Back </a>
      </body>
    </html:html>
      

  2.   

    //保存信息 
    public boolean saveMaterial(Material mater) { 
    // TODO Auto-generated method stub 
    try { 
    Session s = HibernateUtil.currentSession(); 
    HibernateUtil.beginTransaction(); 
    s.saveOrUpdate(mater); 
    //用save()试试..
    System.out.println("save Material maid =" + mater.getMaid()); 
    HibernateUtil.commitTransaction(); 
    HibernateUtil.closeSession(); 
    return true; 
    } catch (HibernateException e) { 
    log.fatal(e); 

    return false; } } 因为saveOrUpdate 方法会根据 unsave-value 去判断这个对象是更新还是保存 如果 unsave-value不是你的期望值就要手动设置.
      

  3.   


    恩,看你hbm.xml里有没设置unsavevalue