所需要的Jar包
/SSH_ErXinLi/WebContent/WEB-INF/lib/antlr-2.7.7.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/aopalliance.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/asm-3.3.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/asm-commons-3.3.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/asm-tree-3.3.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/aspectjweaver.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/commons-email-1.3.1.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/commons-fileupload-1.3.1.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/commons-io-2.2.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/commons-lang3-3.2.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/commons-logging-1.1.3.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/dom4j-1.6.1.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/freemarker-2.3.22.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/hibernate-commons-annotations-4.0.5.Final.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/hibernate-core-4.3.11.Final.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/hibernate-jpa-2.1-api-1.0.0.Final.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/jandex-1.1.0.Final.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/javassist-3.18.1-GA.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/jboss-logging-3.1.3.GA.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/jboss-logging-annotations-1.2.0.Beta1.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/jboss-transaction-api_1.2_spec-1.0.0.Final.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/jstl-1.2.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/log4j-api-2.3.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/log4j-core-2.3.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/mail.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/mysql-connector-java-5.1.20-bin.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/ognl-3.0.14.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-aop-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-aspects-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-beans-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-context-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-context-support-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-core-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-expression-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-instrument-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-instrument-tomcat-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-jdbc-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-jms-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-messaging-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-orm-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-oxm-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-test-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-tx-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-web-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-webmvc-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-webmvc-portlet-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/spring-websocket-4.1.6.RELEASE.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/struts2-core-2.3.28.1.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/struts2-spring-plugin-2.3.28.1.jar
/SSH_ErXinLi/WebContent/WEB-INF/lib/xwork-core-2.3.28.1.jar
创建SessionFactory的工具包
public class HibernateUtils {
private static SessionFactory sessionFactory ;
static{
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry =
new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
}
public static Session getSession(){
return sessionFactory.getCurrentSession();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
Spring配置Hibernate
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "
>
<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 驱动类名,property类型是Class -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/two" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置Hibernate会话工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置链接参数,方言,hbm映射文件 -->
<!--配置链接参数,方言,自定义参数等.统称为Hibernate参数-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 配置hbm映射文件,spring配置文件支持通配符 -->
<property name="mappingLocations">
<array>
<value>classpath:entity/*.hbm.xml</value>
</array>
</property>
</bean>
</beans>
Dao
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<!-- 定义DAO对象 -->
<!-- <bean id="userDao" class="dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> -->
<context:component-scan base-package="dao"></context:component-scan>
</beans>
Service
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "
>
<!-- 配置Service对象 -->
<!-- <bean id="userService" class="service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean> -->
<context:component-scan base-package="service"></context:component-scan>
</beans>
事务控制
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
>
<!-- 定义事务管理器. 类似定义若干方法. 方法提供开启,提交,回滚事务的控制. -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务管理通知. 将定义好的事务管理类型,转化成事务管理通知.
通知内容包含. 为什么方法提供事务管理, 事务隔离级别是什么, 事务的传播特性是什么,
在发送什么类型异常的时候回滚. 是否是只读事务.
transaction-manager - 当前通知使用的事务管理器是什么bean对象.
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 事务管理的属性. 开始配置事务通知的特征 -->
<tx:attributes>
<!-- 在什么方法执行的时候,切入事务管理.
propagation - 事务传播特性. 在一个线程中,多个类型的多个方法合作完成的工作,其事务是否需要传播.
如 : service开启事务, dao是否使用这个事务.
REQUIRED - 必要事务, 线程有事务,实现现有的,没有事务,开启新的.
SUPPORTS - 支持事务, 线程有事务,实现现有的,没有事务,不开启新事务.
rollback-for - 发生什么类型的异常.回滚事务.
read-only - 只读事务. 代表方法只执行查询语法.
-->
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"
rollback-for="java.lang.Exception"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"
rollback-for="java.lang.Exception"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"
rollback-for="java.lang.Exception"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"
rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!-- 事务切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* service.*.*(..))"/>
</aop:config>
</beans>
Hibernate的XML模板
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Module" table="t_module" lazy="false">
<id name="id">
<generator class="native"></generator>
</id>
<property name="mName" column="m_name"/>
<set name="paper" lazy="false">
<!-- 不定义外键字段名, 一对多关系转换成共享主键一对一关系. 不符合要求. -->
<key column="m_id"></key>
<one-to-many class="entity.Paper"></one-to-many>
</set>
</class>
</hibernate-mapping>
Struts2XML模板
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="dis" extends="struts-default" namespace="/">
<action name="*" class="action.DispatcherAction" method="{1}">
<result>/WEB-INF/jsp/main.jsp</result>
<result name="initPassword">/WEB-INF/jsp/initPassword.jsp</result>
<result name="top">/WEB-INF/jsp/top.jsp</result>
<result name="center">/WEB-INF/jsp/table.jsp</result>
<result name="toModule">login.jsp</result>
<result name="toPerson">/WEB-INF/jsp/toPerson.jsp</result>
<result name="showUserInfo">/WEB-INF/jsp/modifyUser.jsp</result>
<result name="toModifyPassword">/WEB-INF/jsp/modifyPassword.jsp</result>
<result name="getPaperContent">/WEB-INF/jsp/PaperContext.jsp</result>
<result name="question">/WEB-INF/jsp/question.jsp</result>
</action>
</package>
<package name="user" extends="struts-default" namespace="/user">
<global-results>
<result name="login" type="redirect">/login.jsp</result>
<result name="register" type="redirect">/register.jsp</result>
</global-results>
<action name="*" class="action.UserAction" method="{1}">
<result name="toMain" type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">toMain</param>
</result>
<result name="toModule">login.jsp</result>
<result name="toSee" type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">showUserInfo</param>
</result>
<result name="toRePass" type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">showUserInfo</param>
</result>
<result name="input">${inputURL}</result>
</action>
</package>
<package name="modules" extends="struts-default" namespace="/modules">
<action name="*" class="action.ModulesAction" method="{1}">
</action>
</package>
<package name="question" extends="struts-default" namespace="/question">
<action name="*" class="action.QuestionAction" method="{1}">
<result type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">question</param>
</result>
<result name="error">/error.jsp</result>
<result name="result" type="redirect">result.jsp</result>
</action>
</package>
<package name="paper" extends="struts-default" namespace="/paper">
<action name="*" class="action.PaperAction" method="{1}">
<result type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">center</param>
</result>
<result name="getPaperContent" type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">getPaperContent</param>
</result>
<result name="result">/result.jsp</result>
</action>
</package>
</struts>