struts hibernate 测试小例子 不报错 但数据库没反应 求分析项目在我的资源里面
本帖最后由 yjb8646226 于 2012-11-28 13:13:51 编辑 一个简单的JSP页面 账户 密码 等 存入 数据库对应表 (sqlserver)
求大神帮忙调试一下 找下哪里有问题
项目在我的资源里面 0积分下载
index.jsp
<form action="savePerson.action">
username:<input type="text" name="username" size="20"><br>
password:<input type="password" name="password" size="20"><br>
age:<input type="text" name="age" size="20">
<input type="submit" value="submit">
</form>
web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="hibernate" extends="struts-default">
<action name="savePerson" class="com.yjb.action.PersonAction">
<result>/welcome.jsp</result>
</action>
</package>
</struts>
Person
package com.yjb.model;
import java.sql.Date;
public class Person {
private Integer id;
private String username;
private String password;
private Integer age;
private Date registerDate;
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getRegisterDate() {
return registerDate;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
}
PersonAction
package com.yjb.action;
import com.opensymphony.xwork2.ActionSupport;
import com.yjb.model.Person;
import com.yjb.service.PersonService;
import com.yjb.service.impl.PersonServiceImpl;
public class PersonAction extends ActionSupport{
/**
*
*/
private String username;
private String password;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String excute ()throws Exception{
Person person =new Person();
person.setUsername(username);
person.setPassword(password);
person.setAge(age);
java.sql.Date registerDate=new java.sql.Date(new java.util.Date().getTime());
person.setRegisterDate(registerDate);
//把页面的值放到person里面
PersonService personService=new PersonServiceImpl();
personService.savePerson(person);
//通过PersonService借口实例化一个对象把person对象的值存入数据库
return SUCCESS;
}
}
HibernateUtil
package com.yjb.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Session openSession(){
Session session=sessionFactory.openSession();
return session;
}
public static void close(Session session){
if (null!=session) {
session.close();
}
}
}
PersonDao
package com.yjb.dao;
import com.yjb.model.Person;
public interface PersonDao {
public void savePerson(Person person);
}
PersonDaoImpl
package com.yjb.dao.impl;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.yjb.dao.PersonDao;
import com.yjb.model.Person;
import com.yjb.util.HibernateUtil;
public class PersonDaoImpl implements PersonDao {
@Override
public void savePerson(Person person) {
// TODO Auto-generated method stub
Session session = HibernateUtil.openSession();
Transaction tx=session.beginTransaction();
try {
session.save(person);
tx.commit();
} catch (Exception e) {
if(tx!=null){
tx.rollback();
}
}finally{
HibernateUtil.close(session);
}
}
}
Person.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yjb.model.Person" table="person">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>
<property name="username" column="username" type="string"></property>
<property name="password" column="password" type="string"></property>
<property name="age" column="age" type="int"></property>
<property name="registerDate" column="registerDate" type="date"></property>
</class>
</hibernate-mapping>
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:sqlserver://localhost:1433/hibernate</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
PersonService
package com.yjb.service;
import com.yjb.model.Person;
public interface PersonService {
public void savePerson(Person person);
}
PersonServiceImpl
package com.yjb.service.impl;
import com.yjb.dao.PersonDao;
import com.yjb.dao.impl.PersonDaoImpl;
import com.yjb.model.Person;
import com.yjb.service.PersonService;
public class PersonServiceImpl implements PersonService {
@Override
public void savePerson(Person person) {
// TODO Auto-generated method stub
PersonDao personDao=new PersonDaoImpl();
personDao.savePerson(person);
}
}
[最优解释]
LZ拼错了、Action默认执行方法execute
还有Hibernate的connection.url也写错了!
下面是各种数据库的URL格式:
http://blog.csdn.net/downloadsunlight2009/article/details/6431786