我是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;
}
}
DB中保存的值始终为null ...除了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>
答案 0 :(得分:1)
您的getter函数错误地命名为变量osdetail
。它应该是getOsdetail()
而不是getOSDetail()
。这就是您未设置表单中的值且变量osdetail
具有空值的原因。尝试更改它。同样适用于setter方法,它应该是setOsdetail()
。另外,为了防止在fututre中犯这样的错误,你可以从eclipse中generate your getter and setter functions automatically而不是手动创建它。
答案 1 :(得分:0)
我在OSAction
代码中没有看到任何内容来读取jsp
中的值。这就是我认为数据库中的值为空的原因。
我想如果您从jsp
获取详细信息并在调用osdetail
之前将其设置为linkController.add(osdetail);
,那么它将会有效。
我对ActionSupport
并不熟悉,但我认为您可以使用jsp
方法从getText()
中读取值...
尝试在linkController.add(osdetail);
行之前添加这些行...
osdetail.setOS_name(getText("osdetail.OSname"));
osdetail.setOS_version(getText("osdetail.OSversion"));
osdetail.setNotes(getText("osdetail.OSnotes"));