I am using struts2 and Hibernate integration application to insert values in database. But after inserting values from form filed only null value is saving in database; this is my form jsp file
我使用struts2和Hibernate集成应用程序在数据库中插入值。但是从表单字段中插入值后,只有空值保存在数据库中;这是我的表单jsp文件
employee.jsp
<%@ taglib uri ="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="employee" method="post">
<s:textfield name ="name" label="ENTER your name"/>
<s:textfield name="address" label="Enter Address"/>
<s:submit label="submit">submit</s:submit>
</s:form>
</body>
</html>
entity class Empmodel.java
实体类Empmodel.java
package model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Empmodel {
@Id @GeneratedValue
private int serialno;
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Connectionfactory.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class ConnectionFactory {
private static SessionFactory sessionfactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
Configuration config =null;
SessionFactory sf =null;
try{
config= new Configuration().configure();
ServiceRegistry servicereg = (ServiceRegistry) new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sf= config.buildSessionFactory(servicereg);
}
catch(Throwable e){
System.out.println("Initial session factory creation failed"+ e);
throw new ExceptionInInitializerError(e);
}
return sf;
}
public static SessionFactory getSessionFactory(){
return sessionfactory;
}}
Empdao is
import model.Empmodel;
import org.hibernate.Session;
public class Empdao {
public Empmodel add(){
Empmodel model = new Empmodel();
Session session=ConnectionFactory.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(model);
session.getTransaction().commit();
return model;
}
}
Action class is
动作类是
import model.Empmodel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import dao.Empdao;
public class Empaction extends ActionSupport implements ModelDriven<Empmodel> {
private static final long serialVersionUID = 1L;
private Empmodel model;
public String execute() throws Exception{
Empdao empdao = new Empdao();
Empmodel quereyresult = empdao.add();
if(quereyresult!= null){
return SUCCESS;
}
else
return ERROR;
}
@Override
public Empmodel getModel() {
// TODO Auto-generated method stub
return model;
}
}
1 个解决方案
#1
2
if you want to get something first you need to put something. This rule works everywhere. In your code you need to put the model object to DAO. Then DAO will save it to the db using the values populated in the model via the interceptor. For example
如果你想先得到一些你需要放东西的东西。这条规则无处不在。在您的代码中,您需要将模型对象放到DAO中。然后DAO将使用模型中通过拦截器填充的值将其保存到数据库。例如
Empmodel quereyresult = empdao.add(model);
For this to work you need to change the method signature to add a parameter for model
.
为此,您需要更改方法签名以为模型添加参数。
The thing seems trivial but you need to remove the statement that recreate the model
in the method implementation, it is not working fine. Also make sure that the model is not empty before you start transaction.
事情似乎微不足道,但你需要删除在方法实现中重新创建模型的语句,它不能正常工作。在开始事务之前,还要确保模型不为空。
The save
method in DAO should check if the new object is created that has null
value for id
.
DAO中的save方法应检查是否创建了具有null值的新对象。
if (model.getId() == null)
session.save(model);
else
session.update(model);
}
How to implement a ModelDriven
with integration hibernate example
Struts2 hibernate integration via s2hibernate plugin
如何通过s2hibernate插件实现与集成hibernate示例Struts2 hibernate集成的ModelDriven
#1
2
if you want to get something first you need to put something. This rule works everywhere. In your code you need to put the model object to DAO. Then DAO will save it to the db using the values populated in the model via the interceptor. For example
如果你想先得到一些你需要放东西的东西。这条规则无处不在。在您的代码中,您需要将模型对象放到DAO中。然后DAO将使用模型中通过拦截器填充的值将其保存到数据库。例如
Empmodel quereyresult = empdao.add(model);
For this to work you need to change the method signature to add a parameter for model
.
为此,您需要更改方法签名以为模型添加参数。
The thing seems trivial but you need to remove the statement that recreate the model
in the method implementation, it is not working fine. Also make sure that the model is not empty before you start transaction.
事情似乎微不足道,但你需要删除在方法实现中重新创建模型的语句,它不能正常工作。在开始事务之前,还要确保模型不为空。
The save
method in DAO should check if the new object is created that has null
value for id
.
DAO中的save方法应检查是否创建了具有null值的新对象。
if (model.getId() == null)
session.save(model);
else
session.update(model);
}
How to implement a ModelDriven
with integration hibernate example
Struts2 hibernate integration via s2hibernate plugin
如何通过s2hibernate插件实现与集成hibernate示例Struts2 hibernate集成的ModelDriven