问题描述
我正在使用struts2和Hibernate集成应用程序在数据库中插入值.但是在从表单字段插入值后,只有空值保存在数据库中.这是我的表单jsp文件
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
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>
实体类 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是
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;
}
}
动作类别为
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;
}
}
推荐答案
如果您想先获得一些东西,则需要放置一些东西.该规则随处可见.在您的代码中,您需要将模型对象放入DAO.然后,DAO将使用通过拦截器在模型中填充的值将其保存到数据库中.例如
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
Empmodel queryresult = empdao.add(model);
为此,您需要更改方法签名以为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. Also make sure that the model is not empty before you start transaction.
DAO中的save
方法应检查是否创建了具有null
值的新对象.
The save
method in DAO should check if the new object is created that has null
value for id
.
if (model.getId() == null)
session.save(model);
else
session.update(model);
}
这篇关于使用Struts2和Hibernate在数据库中插入空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!