jar包:
hibernate-distribution-3.5.0-CR-1/lib/required/antlr-2.7.6.jar
hibernate-distribution-3.5.0-CR-1/lib/required/commons-collections-3.1.jar
hibernate-distribution-3.5.0-CR-1/lib/required/dom4j-1.6.1.jar
hibernate-distribution-3.5.0-CR-1/lib/required/hibernate3.jar
hibernate-distribution-3.5.0-CR-1/lib/required/javassist-3.9.0.GA.jar
hibernate-distribution-3.5.0-CR-1/lib/required/jta-1.1.jar
hibernate-distribution-3.5.0-CR-1/lib/required/mysql-connector-java-5.1.12-bin.jar
hibernate-distribution-3.5.0-CR-1/lib/required/slf4j-api-1.5.8.jar
hibernate-distribution-3.5.0-CR-1/lib/required/slf4j-log4j12-1.5.2.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.aop-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.asm-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.beans-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.context-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.core-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.expression-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.jdbc-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.orm-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.transaction-3.0.1.RELEASE-A.jar
spring-framework-3.0.1.RELEASE-A/dist/org.springframework.web-3.0.1.RELEASE-A.jar
struts-2.1.8.1/lib/commons-fileupload-1.2.1.jar
struts-2.1.8.1/lib/freemarker-2.3.15.jar
struts-2.1.8.1/lib/ognl-2.7.3.jar
struts-2.1.8.1/lib/struts2-core-2.1.8.1.jar
struts-2.1.8.1/lib/struts2-spring-plugin-2.1.8.1.jar
struts-2.1.8.1/lib/xwork-core-2.1.6.jar
struts-2.1.8.1/lib/commons-logging-1.0.4.jar
struts-2.1.8.1/lib/struts2-convention-plugin-2.1.8.1.jar
log4j-1.2.15/log4j-1.2.15.jar
aopalliance/aopalliance-1.0.jar
aspectj/lib/aspectjweaver.jar
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>*.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
其中,如果没有struts2的公共参数要设置,则struts.xml可为空;hibernate.properties按一般情况设置。
以下是application.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="mappingResources"> <list> <value>com/jetsum/common/entity/Person.hbm.xml</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="saveOrUpdate" propagation="REQUIRED" /> <tx:method name="loadEntity" propagation="REQUIRED" /> <tx:method name="getEntity" propagation="REQUIRED" /> <tx:method name="delete" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置哪些类哪些方法使用事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.jetsum.webtest.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> </aop:config> <context:annotation-config/> <context:component-scan base-package="com.jetsum.webtest.dao.impl"></context:component-scan> <context:component-scan base-package="com.jetsum.webtest.service.impl"></context:component-scan> </beans>
必不可少的类:
package com.jetsum.webtest.common; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class BaseHibernateDaoSupport extends HibernateDaoSupport{ @Resource(name="sessionFactory") //为父类HibernateDaoSupport注入sessionFactory的值 public void setSuperSessionFactory(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } }
<property name="sessionFactory" ref="sessionFactory"></property>
package com.jetsum.common.entity; import java.util.Date; public class Person { private int id; private String name; private Date birthday; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
@Repository("personDao") public class PersonDaoImpl extends BaseHibernateDaoSupport implements PersonDao{
@Repository("personService") public class PersonServiceImpl implements PersonService {
@Resource(name="personDao") private PersonDao personDao; (不再需要getter和setter)
@Resource(name="personService") private PersonService personService; (不再需要getter和setter)
@Controller @Scope("prototype") @Results({ @Result(name="save",location="/success.jsp") }) public class PersonAction extends ActionSupport