我正在使用struts2 hibernate,我有一个问题,当我添加一个新联系人时,表联系人中有一个新行,但所有值都为空。
这是动作类
public class ContactAction extends ActionSupport implements ModelDriven<Contact> {
private static final long serialVersionUID = 1L;
private Contact contact = new Contact();
private List<Contact> contactList = new ArrayList<Contact>();
private ContactDAO contactDAO = new ContactDAOImpl();
public Contact getModel() {
return contact;
}
public String add()
{
contactDAO.saveContact(contact);
return SUCCESS;
}
public String list()
{
contactList = contactDAO.listContact();
return SUCCESS;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public List<Contact> getUserList() {
return contactList;
}
public void setUserList(List<Contact> contactList) {
this.contactList = contactList;
}
这是DAO课程
public class ContactDAOImpl implements ContactDAO{
@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;
@SuppressWarnings("unchecked")
public List<Contact> listContact() {
List<Contact> courses = null;
try {
courses = session.createQuery("from Contact").list();
} catch (Exception e) {
e.printStackTrace();
}
return courses;
}
public void saveContact(Contact contact) {
try {
session.beginTransaction();
session.save(contact);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}
这就是hibernate配置文件
<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/example1</property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<mapping class="com.example.model.User" />
<mapping class="com.example.model.Contact" />
</session-factory>
</hibernate-configuration>
我为User使用了完全相同的代码,没有任何问题。
这是Contact.java类
@Entity
@Table(name="CONTACTS")
public class Contact {
private Long contact_id;
private String contact_firstname;
private String contact_laststname;
private String contact_title;
private String contact_email;
private String contact_company;
private int contact_phone;
@Id
@GeneratedValue
@Column(name="CONTACT_ID")
public Long getId() {
return contact_id;
}
public void setId(Long contact_id) {
this.contact_id = contact_id;
}
@Column(name="CONTACT_FIRSTNAME")
public String getFirstName() {
return contact_firstname;
}
public void setFirstName(String contact_firstname) {
this.contact_firstname = contact_firstname;
}
@Column(name="CONTACT_LASTNAME")
public String getLastName() {
return contact_laststname;
}
public void setlastName(String contact_laststname) {
this.contact_laststname = contact_laststname;
}
@Column(name="CONTACT_TITLE")
public String getTitle() {
return contact_title;
}
public void setTitle(String contact_title) {
this.contact_title = contact_title;
}
@Column(name="CONTACT_COMPANY")
public String getCompany() {
return contact_company;
}
public void setCompany(String contact_company) {
this.contact_company = contact_company;
}
@Column(name="CONTACT_PHONE")
public int getPhone() {
return contact_phone;
}
public void setPhone(int contact_phone) {
this.contact_phone = contact_phone;
}
@Column(name="CONTACT_EMAIL")
public String getEmail() {
return contact_email;
}
public void setEmail(String contact_email) {
this.contact_email = contact_email;
}
}
这是jsp页面
[<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add contact</title>
<s:head />
<style type="text/css">
@import url(style.css);
</style>
</head>
<body>
<s:form action="addContact">
<s:textfield name="contact_firstname" label="First name" />
<s:textfield name="contact_laststname" label="Last name" />
<s:textfield name="contact_title" label="Title" />
<s:textfield name="contact_email" label="Email address" />
<s:textfield name="contact_company" label="Related company" />
<s:textfield name="contact_phone" label="Phone number" />
<s:submit />
</s:form>
</body>
</html>][1]