[英]Null values are insering in database using Struts 2 and Hibernate
I am using Struts 2 and Hibernate integration application to insert values in database.我正在使用 Struts 2 和 Hibernate 集成应用程序在数据库中插入值。 But after inserting values from a form filed only null
value is saving in database.但是从提交的表单中插入值后,只有null
值保存在数据库中。
This is my form JSP file:这是我的表单 JSP 文件:
employee.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
: 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: Empdao
是:
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: Action
类是:
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 queryresult = empdao.add();
if(queryresult != null) {
return SUCCESS;
}
else
return ERROR;
}
@Override
public Empmodel getModel() {
// TODO Auto-generated method stub
return model;
}
}
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.在您的代码中,您需要将模型对象放入 DAO。 Then DAO will save it to the db using the values populated in the model via the interceptor.然后 DAO 将使用通过拦截器填充到模型中的值将其保存到数据库中。 For example例如
Empmodel queryresult = empdao.add(model);
For this to work you need to change the method signature to add a parameter for model
.为此,您需要更改方法签名以添加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.这件事似乎微不足道,但您需要删除在方法实现中重新创建model
的语句,它无法正常工作。 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
方法应该检查是否创建了id
为null
值的新对象。
if (model.getId() == null)
session.save(model);
else
session.update(model);
}
How to implement a ModelDriven
with integration hibernate example 如何使用集成休眠示例实现ModelDriven
Struts2 hibernate integration via s2hibernate plugin 通过 s2hibernate 插件进行 Struts2 休眠集成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.