1

我是struts的新手,并试图使用struts和hibernate从jsp页面将一些值保存到DB(mysql)。

但是,应用程序每次都保存空值,并且自动增量 ID 正在增加。

我的数据库结构。:

Table Name | osdetail
-------------------------
   Columns | os_name, 
           | os_version,
           | id,
           | created,
           | notes.

index.jsp页面_

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>OS Manager - Struts2 Hibernate Example</title>
</head>
<body>
 
<h1>OS Manager</h1>
<s:actionerror/>
 
<s:form action="add" method="post">
    <s:textfield name="osdetail.OSname" label="name"/>
    <s:textfield name="osdetail.OSversion" label="version"/>
    <s:textfield name="osdetail.OSnotes" label="notes"/>
    <s:submit value="Add OS Details" align="center"/>

</s:form>
 
 
<h2>OS Details</h2>
<table>
<tr>
    <th>OS Name</th>
    <th>OS Version</th>
    <th>OS Notes</th>
</tr>
<s:iterator value="osdetails_list" var="osdetail">
    <tr>
        <td><s:property value="OSname"/></td>
        <td><s:property value="OSversion"/></td>
        <td><s:property value="OSnotes"/></td>
    </tr> 
</s:iterator>
</table>
</body>
</html>

我的观点:OSAction.java

 package net.ajeet.os.view;

     import java.util.List;

    import net.ajeet.os.controller.OSManager;
    import net.ajeet.os.model.OSDetail;

     import com.opensymphony.xwork2.ActionSupport;
     import com.opensymphony.xwork2.ModelDriven;


      public class OSAction extends ActionSupport implements ModelDriven<OSDetail> {

private static final long serialVersionUID = 9149826260758390091L;
private OSDetail osdetail= new OSDetail();
private List<OSDetail> osdetails_list;
private Long id;

private OSManager linkController= new OSManager();
@Override
public OSDetail getModel() {
    return osdetail;
}
public OSAction() {
    linkController = new OSManager();
}

public String execute() {
    this.osdetails_list = linkController.list();
    return SUCCESS;
}

public String add() {
    System.out.println("this is oS detail get ID"+osdetail.getId());
    try {
        //linkController.add(getOSDetail());
        linkController.add(osdetail);
        System.out.println("this is oS detail  after add "+getOSDetail());
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.osdetails_list = linkController.list();
    return SUCCESS;
}

public String delete() {
    linkController.delete(getId());
    return SUCCESS;
}

public OSDetail getOSDetail() {
    return osdetail;
}

public List<OSDetail> getOSDetail_list() {


    return osdetails_list;
}   

public void setOSDetail(OSDetail osdetail) {
    this.osdetail = osdetail;

}

public void setOSDetail_list(List<OSDetail> osdetails_list) {
    this.osdetails_list = osdetails_list;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

}

我的模型:OSDetail.java

 package net.ajeet.os.model;


      import java.io.Serializable;
     import java.sql.Date;


   import javax.persistence.Column;
     import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
   import javax.persistence.Table;

   @Entity
   @Table(name="osdetail")

     public class OSDetail implements Serializable{

private static final long serialVersionUID = -8767337896773261247L;

private Long OSid;

private String OSname;
private String OSversion;
private String OSnotes;

private Date OScreated;

@Id
@GeneratedValue
@Column(name="id")
public Long getId() {

    System.out.println("set os name is os id"+OSid);
    return OSid;

}
@Column(name="os_name")
public String getOS_name() {
    return OSname;
}
@Column(name="os_version")
public String getOS_version() {
    return OSversion;
}
@Column(name="notes")
public String getNotes() {
    return OSnotes;
}

@Column(name="created")
public Date getCreated() {
    return OScreated;
}
public void setId(Long OSid) {
    this.OSid = OSid;
}
public void setOS_name(String OSname) {
    this.OSname = OSname;

}
public void setOS_version(String OSversion) {
    this.OSversion = OSversion;
}
public void setNotes(String OSnotes) {
    this.OSnotes = OSnotes;
}
public void setCreated(Date OScreated) {
    this.OScreated = OScreated;

}

我的控制器:OSManager.java

     import java.util.List;

     import org.hibernate.HibernateException;
     import org.hibernate.classic.Session;

       import net.ajeet.os.model.OSDetail;
       import net.ajeet.os.util.HibernateUtil;

      public class OSManager extends HibernateUtil {

public OSDetail add(OSDetail osdetail) {

    System.out.println("value of the os in OSManager"+osdetail.getId());

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    session.save(osdetail);
    session.getTransaction().commit();
    return osdetail;
}

public OSDetail delete(Long id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    OSDetail osdetail = (OSDetail) session.load(OSDetail.class, id);
    if(null != osdetail) {
        session.delete(osdetail);
    }
    session.getTransaction().commit();
    return osdetail;
}
@SuppressWarnings("unchecked")
public List<OSDetail> list() {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List<OSDetail> osdetails_list = null;
    try {

        osdetails_list = (List<OSDetail>)session.createQuery("from OSDetail").list();

    } catch (HibernateException e) {
        e.printStackTrace();
        session.getTransaction().rollback();
    }
    session.getTransaction().commit();
    return osdetails_list;
}
}

保存在数据库中的值始终为空...除了 ID ..请帮助

更改了操作...更新了 getter/setter

package net.ajeet.os.view;

import java.util.List;

import net.ajeet.os.controller.OSManager;
import net.ajeet.os.model.OSDetail;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class OSAction extends ActionSupport  {

    private static final long serialVersionUID = 9149826260758390091L;
    public OSDetail osdetail= new OSDetail();
    private List<OSDetail> osdetails_list;
    public OSDetail getOsdetail() {
        return osdetail;
    }

    public void setOsdetail(OSDetail osdetail) {
        this.osdetail = osdetail;
    }

    private Long id;

    private OSManager linkController= new OSManager();
/*  @Override
    public OSDetail getModel() {
        return osdetail;
    }*/
    public OSAction() {
        linkController = new OSManager();
    }

    public String execute() {
        this.osdetails_list = linkController.list();
        return SUCCESS;
    }

    public String add() {

        try {

            linkController.add(getOsdetail());
            //linkController.add(osdetail);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.osdetails_list = linkController.list();
        return SUCCESS;
    }

    public String delete() {
        linkController.delete(getid());
        return SUCCESS;
    }

    public List<OSDetail> getOsdetails_list() {
        return osdetails_list;
    }

    public void setOsdetails_list(List<OSDetail> osdetails_list) {
        this.osdetails_list = osdetails_list;
    }

    public Long getid() {
        return id;
    }

    public void setid(Long id) {
        this.id = id;
    }




}

更正了 OSDetail.java,自动创建了 getter/setter。

     package net.ajeet.os.model;


import java.io.Serializable;
import java.sql.Date;



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="osdetail")

public class OSDetail implements Serializable{

    private static final long serialVersionUID = -8767337896773261247L;

    private Long OSid;

    private String OSname;
    private String OSversion;
    private String OSnotes;

    private Date OScreated;


    @Id
    @GeneratedValue
    @Column(name="id")
    public Long getOSid() {
        return OSid;
    }

    public void setOSid(Long oSid) {
        OSid = oSid;
    }

    @Column(name="os_name")
    public String getOSname() {
        return OSname;
    }

    public void setOSname(String oSname) {
        OSname = oSname;
    }

    @Column(name="os_version")
    public String getOSversion() {

        return OSversion;
    }

    public void setOSversion(String oSversion) {
        OSversion = oSversion;

        System.out.println("value of the os in OSversion in setter"+OSversion);
    }

    @Column(name="notes")
    public String getOSnotes() {
        return OSnotes;
    }

    public void setOSnotes(String oSnotes) {
        OSnotes = oSnotes;
    }

    @Column(name="created")
    public Date getOScreated() {
        return OScreated;
    }

    public void setOScreated(Date oScreated) {
        OScreated = oScreated;
    }



}

将 struts.xml 添加到

    <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation"
        value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" extends="struts-default" namespace="/">

        <action name="add"
            class="net.ajeet.os.view.OSAction" method="add">
            <result name="success" type="chain">index</result>
            <result name="input" type="chain">index</result>
        </action>



        <action name="index"
            class="net.ajeet.os.view.OSAction">
            <result name="success">index.jsp</result>
        </action>
    </package>
</struts>

和hibernate.cfg.xml

     <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/test
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">Asmita24</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping class="net.ajeet.os.model.OSDetail" />

    </session-factory>

</hibernate-configuration>
4

2 回答 2

1

您的 getter 函数被错误地命名为 variable osdetail。它应该getOsdetail()代替getOSDetail(). 这就是未设置表单中的值并且变量osdetail具有空白值的原因。尝试改变它。setter 方法也是如此,应该是setOsdetail(). 此外,为了防止在未来出现此类错误,您可以从 eclipse 中自动生成 getter 和 setter 函数,而不是手动创建它。

于 2013-07-19T06:43:26.043 回答
0

我在您OSAction的代码中看不到任何地方从jsp. 这就是为什么我认为这些值在数据库中以空值形式出现的原因。

我想如果您从 获取详细信息并在调用之前jsp将其设置为 ,它将起作用。osdetaillinkController.add(osdetail);

我不太精通,ActionSupport但我认为您可以从jspusinggetText()方法中读取值...

尝试在行之前添加这些linkController.add(osdetail);行...

osdetail.setOS_name(getText("osdetail.OSname"));
osdetail.setOS_version(getText("osdetail.OSversion"));
osdetail.setNotes(getText("osdetail.OSnotes"));
于 2013-07-19T06:10:46.650 回答