我是用STRUTS ,做了一个JSP页面,提交后没有存入数据库。
JSP代码如下:
aa.jsp代码
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ 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"%>
<%@ page import="java.sql.*"%>
<html> 
<head>
<title>项目总体登记录入</title>
</head>
 <a href="/xmgl/successmain.jsp">返回主菜单</a>
<br><center><a>项目总体录入</a></center><br>
<body bgcolor=#dcdcdc>&nbsp; 
     
     <table  cellspacing="1" cellpadding="3" align="center" border="0" style="border:#dcdcdc 1px solid;background:#dcdcdc;">
          <html:form action="xmdj">
项目编号        : <html:text property="ht_no"/><html:errors property="ht_no"/>
            是否签订合同:<select name="ht_yn">
                               <option value="Y">是</option>
                              <option value="N">否</option>  
                             </select>
<br/>
                                合同日期        : <html:text property="ht_date"/><html:errors property="ht_date"/> 格式:yyyy-MM-dd<br/>                                甲方名称        : <select name="jf_name" style="width:200pt">
                         <%!
public String getStr(String str)
    {
           try{
                   String temp_p=str;
                       byte[] temp_t=temp_p.getBytes("ISO8859-1");
                       String temp=new String(temp_t);
                       return temp;
               }catch(Exception exx){}
            return null;
    }
%>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();  
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xmgl";  
//pubs为你的数据库的  
String user="sa";  
String password="";  
Connection conn= DriverManager.getConnection(url,user,password);  
Statement stmt=conn.createStatement();
String PCName, name, sex, education, email, resume;
String username1=getStr(request.getParameter("PCName"));
String sql="select P_C_Name from Provider_Consumer";//where  P_C_Name like '%"+username1+"%'";
ResultSet rs = stmt.executeQuery(sql);
%>


  <%
         
        while(rs.next()){
                
                %>
                        <option value=<%=rs.getString("P_C_Name")%>><%=rs.getString("P_C_Name")%></option>

        
                <%
                        }
                %>
        
              
                               
                       </select>
<br/>
 
                                项目名称        : <html:text property="xm_name" style="width:200pt"/><html:errors property="xm_name"/><br/>
                                甲方负责人   : <html:text property="jf_main"/><html:errors property="jf_main"/>
联系方式        : <html:text property="lx_way"/><html:errors property="lx_way"/><br/>
                               甲方联系人    :  <html:text property="jf_lxr"/><html:errors property="jf_lxr"/>
                                联系方式        : <html:text property="jf_lxr_way"/><html:errors property="jf_lxr_way"/><br/>
                                业务负责人   : <html:text property="yw_main"/><html:errors property="yw_main"/><br/>
项目联系人   : <html:text property="xm_lxr"/><html:errors property="xm_lxr"/><br/>
                                合同标的        :  <html:text property="ht_bd" style="width:200pt"/><html:errors property="ht_bd"/><br/>
                                项目金额        : <html:text property="ht_total"/><html:errors property="ht_total"/>
                                万元;       是否含税:
                               <select name="hs_yn">
                               <option value="Y">是</option>
                              <option value="N">否</option>  
                             </select>
                             <br/>                        金额说明        : <html:text property="je_sm" style="width:200pt"/><html:errors property="je_sm"/><br/>
                                合同说明        :  <html:text property="ht_sm" style="width:200pt"/><html:errors property="ht_sm"/><br/>
                                项目类型        : <html:text property="xm_lx"/><html:errors property="xm_lx"/><br/>
项目级别        : <html:text property="xm_jb"/><html:errors property="xm_jb"/><br/>
         
                                文件路径        : <html:text property="ht_file_lj" style="width:200pt"/><html:errors property="ht_file_lj"/><br/>
            
<br><center><a> </a></center><br>
<html:submit value=" 提  交 "/><html:cancel value = " 取  消 "/>
</html:form>
      </table>
</body>
</html>

解决方案 »

  1.   

    aaactive.java代码/*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.xmgl.struts.action;import java.io.UnsupportedEncodingException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    //import java.util.Iterator;
    //import java.util.Iterator;//import javax.servlet.ServletRequest;
    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 org.hibernate.Query;
    //import org.hibernate.Query;
    //import org.hibernate.Query;
    import org.hibernate.Session;import com.xmgl.HibernateUtil;
    import com.xmgl.ProviderConsumer;
    //import com.xmgl.SysUser;import com.xmgl.XmDj;
    import com.xmgl.struts.form.XmdjForm;/** 
     * MyEclipse Struts
     * Creation date: 09-10-2009
     * 
     * XDoclet definition:
     * @struts.action path="/xmdj" name="xmdjForm" input="/form/xmdj.jsp" scope="request" validate="true"
     */
    public class XmdjAction extends Action {
    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @param u1 
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response, ProviderConsumer u1) {
    XmdjForm XmdjForm = (XmdjForm) form;// TODO Auto-generated method stub
    String htNo = XmdjForm.getHt_no();
    String jfName;
    jfName = "null";
    try {
    jfName = new String ((XmdjForm.getJf_name()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    String xmName;
    xmName =null;
    try {
    xmName = new String((XmdjForm.getXm_name()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String jfMain;
    jfMain = null;
    try {
    jfMain = new String((XmdjForm.getJf_main()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String lxWay;
    lxWay = null;
    try {
    lxWay = new String((XmdjForm.getLx_way()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String jfLxr;
    jfLxr = null;
    try {
    jfLxr = new String((XmdjForm.getJf_lxr()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String jfLxrWay;
    jfLxrWay = null;
    try {
    jfLxrWay = new String((XmdjForm.getJf_lxr_way()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String ywMain;
    ywMain = null;
    try {
    ywMain = new String((XmdjForm.getYw_main()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String xmLxr;
    xmLxr = null;
    try {
    xmLxr = new String((XmdjForm.getXm_lxr()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String htBd;
    htBd = null;
    try {
    htBd = new String((XmdjForm.getHt_bd()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    Double htTotal = XmdjForm.getHt_total(); //t数值型,暂时注释
    String hsYn = XmdjForm.getHs_yn();
    String jeSm;
    jeSm = null;
    try {
    jeSm = new String((XmdjForm.getJe_sm()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String htSm;
    htSm = null;
    try {
    htSm = new String((XmdjForm.getHt_sm()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    String xmLx;
    xmLx = null;
    try {
    xmLx = new String((XmdjForm.getXm_lx()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String xmJb;
    xmJb = null;
    try {
    xmJb = new String((XmdjForm.getXm_jb()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String htFileLj;
    htFileLj = null;
    try {
    htFileLj = new String((XmdjForm.getHt_file_lj()).getBytes("iso8859-1"),"GB2312");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    //Date createDate = XmdjForm.getCreate_date(); 
    Date createDate = new java.util.Date();
    String htDate = XmdjForm.getHt_date();
    String htYn = XmdjForm.getHt_yn();
    SimpleDateFormat htDate1 = new SimpleDateFormat("yyyy-MM-dd");
    Date htDate2;
    htDate2 = null;
    // String htDate3;
    // htDate3 = "2009-1-1";
    try {
     htDate2 = htDate1.parse(htDate);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    Session session=HibernateUtil.Session();
    //创建事务
    org.hibernate.Transaction tx=session.beginTransaction();
    XmDj xd= new XmDj();
    xd.setHtNo(htNo);
    xd.setXmName(xmName);
    xd.setJfName(jfName);
    xd.setJfMain(jfMain);
    xd.setLxWay(lxWay);
    xd.setJfLxr(jfLxr);
    xd.setJfLxrWay(jfLxrWay);
    xd.setYmMain(ywMain);
    xd.setXmLxr(xmLxr);
    xd.setHtBd(htBd);
    xd.setHsYn(hsYn);
    xd.setJeSm(jeSm);
    xd.setHtSm(htSm);
    xd.setPostYn("N");
    xd.setXmLx(xmLx);
    xd.setXmJb(xmJb);
    xd.setTjYn("N");
    xd.setFpYn("N");
    xd.setDjYn("N");
    xd.setDaNo("webinput");
    xd.setHtFileLj(htFileLj);
    xd.setHtTotal(htTotal);
    xd.setCreateDate(createDate);
    xd.setHtYn(htYn);
    xd.setHtDate(htDate2);
    session.save(xd);
    tx.commit();
    session.close();
    //request.setAttribute("OK","提交成功!");
    return  mapping.findForward("okxmdj");