Struts2.3.4.1 + Spring3.1.2 + Hibernate4.1.6整合

时间:2022-06-03 13:47:47
1. Jar包 
2. web.xml配置 
3. struts.xml配置 
4. hibernate.cfg.xml配置 
5. applicationContext.xml配置 
6. log4j.properties配置 
7. Dao层 
8. Service层 
9. Action层

1. Jar包 
1) Struts 2.3.4.1 
下载地址:http://struts.apache.org/download

  1. /lib/commons-fileupload-1.2.2.jar
  2. /lib/commons-io-2.0.1.jar
  3. /lib/commons-lang3-3.1.jar
  4. /lib/freemarker-2.3.19.jar
  5. /lib/javassist-3.11.0.GA.jar
  6. /lib/ognl-3.0.5.jar
  7. /lib/struts2-core-2.3.4.1.jar
  8. /lib/struts2-spring-plugin-2.3.4.1.jar
  9. /lib/xwork-core-2.3.4.1.jar

2) Spring 3.1.2 
下载地址:http://www.springsource.org/download

  1. /dist/*

3) Hibernate 4.1.6 
下载地址:http://sourceforge.net/projects/hibernate/files/hibernate4

  1. /lib/required/*
  2. /lib/envers/hibernate-envers-4.1.6.Final.jar
  3. /lib/jpa/hibernate-entitymanager-4.1.6.Final.jar
  4. /lib/optional/c3p0/c3p0-0.9.1.jar
  5. /lib/optional/c3p0/hibernate-c3p0-4.1.6.Final.jar

4) Aopalliance 1.0 
下载地址:http://sourceforge.net/projects/aopalliance

  1. aopalliance.jar

5) Aspectj 1.7.0 
下载地址:http://www.eclipse.org/aspectj/downloads.php

  1. aspectjrt.jar
  2. aspectjweaver.jar

6) Cglib 2.2.3 
下载地址:http://sourceforge.net/projects/cglib/files

  1. cglib-2.2.3.jar

7) Asm 3.3 
下载地址:http://forge.ow2.org/projects/asm

  1. asm-3.3.jar

8) Log4j 1.2.17 
下载地址:http://logging.apache.org/log4j/1.2/download.html

  1. log4j-1.2.17.jar

9) MySQL Connector Java 5.1.21 
下载地址:http://dev.mysql.com/downloads/connector/j

  1. mysql-connector-java-5.1.21-bin.jar

10)Commons Logging 1.1.1 
下载地址:http://commons.apache.org/logging

  1. commons-logging-1.1.1.jar

2. web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. version="3.0">
  7. <!-- Welcome File List -->
  8. <welcome-file-list>
  9. <welcome-file>login.html</welcome-file>
  10. </welcome-file-list>
  11. <!-- WebApp Root -->
  12. <context-param>
  13. <param-name>webAppRootKey</param-name>
  14. <param-value>webapp.root</param-value>
  15. </context-param>
  16. <!-- Spring Encoding Filter -->
  17. <filter>
  18. <filter-name>encodingFilter</filter-name>
  19. <filter-class>
  20. org.springframework.web.filter.CharacterEncodingFilter
  21. </filter-class>
  22. <init-param>
  23. <param-name>encoding</param-name>
  24. <param-value>UTF-8</param-value>
  25. </init-param>
  26. </filter>
  27. <!-- Spring Encoding Filter Mapping -->
  28. <filter-mapping>
  29. <filter-name>encodingFilter</filter-name>
  30. <url-pattern>/*</url-pattern>
  31. </filter-mapping>
  32. <!-- Struts2 Filter -->
  33. <filter>
  34. <filter-name>struts2</filter-name>
  35. <filter-class>
  36. org.apache.struts2.dispatcher.ng.filter.
  37. StrutsPrepareAndExecuteFilter
  38. </filter-class>
  39. </filter>
  40. <!-- Struts2 Filter Mapping -->
  41. <filter-mapping>
  42. <filter-name>struts2</filter-name>
  43. <url-pattern>/*</url-pattern>
  44. </filter-mapping>
  45. <!-- Log4j ConfigurationFile Location -->
  46. <context-param>
  47. <param-name>log4jConfigLocation</param-name>
  48. <param-value>classpath:log4j.properties</param-value>
  49. </context-param>
  50. <!-- Spring Log4j Listener -->
  51. <listener>
  52. <listener-class>
  53. org.springframework.web.util.Log4jConfigListener
  54. </listener-class>
  55. </listener>
  56. <!-- Spring ConfigurationFile Location -->
  57. <context-param>
  58. <param-name>contextConfigLocation</param-name>
  59. <param-value>classpath:applicationContext.xml</param-value>
  60. </context-param>
  61. <!-- Spring Context Listener -->
  62. <listener>
  63. <listener-class>
  64. org.springframework.web.context.ContextLoaderListener
  65. </listener-class>
  66. </listener>
  67. <!-- Spring Web Request Listener -->
  68. <listener>
  69. <listener-class>
  70. org.springframework.web.context.request.RequestContextListener
  71. </listener-class>
  72. </listener>
  73. <!-- Spring Introspector Cleanup Listener -->
  74. <listener>
  75. <listener-class>
  76. org.springframework.web.util.IntrospectorCleanupListener
  77. </listener-class>
  78. </listener>
  79. </web-app>

3. struts.xml配置

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constant name="struts.devMode" value="false" />
  7. <constant name="struts.i18n.encoding" value="UTF-8" />
  8. <package name="default" namespace="/" extends="struts-default">
  9. <action name="login" method="login" class="loginAction">
  10. <result name="input">/login.html</result>
  11. <result name="success">/home.jsp</result>
  12. </action>
  13. </package>
  14. </struts>

4. hibernate.cfg.xml配置

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="dialect">
  8. org.hibernate.dialect.MySQLDialect
  9. </property>
  10. <property name="show_sql">true</property>
  11. <property name="format_sql">true</property>
  12. <property name="hbm2ddl.auto">update</property>
  13. <mapping class="com.txazo.domain.User" />
  14. </session-factory>
  15. </hibernate-configuration>

5. applicationContext.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/
  17. spring-context-3.1.xsd">
  18. <!-- DataSource -->
  19. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  20. destroy-method="close">
  21. <property name="driverClass" value="com.mysql.jdbc.Driver" />
  22. <property name="jdbcUrl"
  23. value="jdbc:mysql://127.0.0.1:3306/txazo" />
  24. <property name="user" value="root" />
  25. <property name="password" value="root" />
  26. </bean>
  27. <!-- SessionFactory -->
  28. <bean id="sessionFactory"
  29. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
  30. p:dataSource-ref="dataSource"
  31. p:configLocation="classpath:hibernate.cfg.xml" />
  32. <!-- TransactionManager -->
  33. <bean id="transactionManager"
  34. class="org.springframework.orm.hibernate4.HibernateTransactionManager"
  35. p:sessionFactory-ref="sessionFactory" />
  36. <!-- Spring Advice -->
  37. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  38. <tx:attributes>
  39. <tx:method name="get*" read-only="true"></tx:method>
  40. <tx:method name="*"></tx:method>
  41. </tx:attributes>
  42. </tx:advice>
  43. <!-- Spring Aop Config -->
  44. <aop:config>
  45. <aop:pointcut id="pointcut"
  46. expression="
  47. execution(* com.txazo.service.impl.*Impl.*(..))" />
  48. <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
  49. </aop:config>
  50. <!-- Dao -->
  51. <bean id="baseDao" class="com.txazo.dao.impl.BaseDaoImpl">
  52. <property name="sessionFactory">
  53. <ref bean="sessionFactory" />
  54. </property>
  55. </bean>
  56. <!-- Service -->
  57. <bean id="userService" class="com.txazo.service.impl.UserServiceImpl">
  58. <property name="baseDao">
  59. <ref bean="baseDao" />
  60. </property>
  61. </bean>
  62. <!-- Action -->
  63. <bean id="loginAction" class="com.txazo.action.LoginAction"
  64. scope="session">
  65. <property name="userService">
  66. <ref bean="userService" />
  67. </property>
  68. </bean>
  69. </beans>

6. log4j.properties配置

  1. # Set The RootLogger
  2. log4j.rootLogger=warn, console
  3. # Direct Log Messages To Console
  4. log4j.appender.console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.console.Target=System.out
  6. log4j.appender.console.layout=org.apache.log4j.PatternLayout
  7. log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c:%L - %m%n
  8. # Log Hibernate
  9. log4j.logger.org.hibernate=error
  10. # Log Just The SQL
  11. log4j.logger.org.hibernate.SQL=debug
  12. # Log Schema Export Update
  13. log4j.logger.org.hibernate.tool.hbm2ddl=debug

7. Dao层

  1. public interface BaseDao {
  2. public <T> void save(T t);
  3. public <T> void delete(T t);
  4. public <T> void delete(Class<T> entityClass, Integer id);
  5. public <T> void update(T t);
  6. public <T> T get(Class<T> entityClass, Integer id);
  7. public <T> List<T> findAll(String hql, Class<T> entityClass);
  8. public <T> List<T> findAll(String hql, Class<T> entityClass,
  9. Object param);
  10. public <T> List<T> findAll(String hql, Class<T> entityClass,
  11. Object[] params);
  12. public <T> List<T> findByPage(final String hql, final Class<T>
  13. entityClass, final int firstResult, final int maxResult);
  14. public <T> List<T> findByPage(final String hql, final Class<T>
  15. entityClass, final Object param, final int firstResult,
  16. final int maxResult);
  17. public <T> List<T> findByPage(final String hql, final Class<T>
  18. entityClass, final Object[] params,
  19. final int firstResult, final int maxResult);
  20. }
  1. public class BaseDaoImpl implements BaseDao {
  2. private SessionFactory sessionFactory;
  3. public void setSessionFactory(SessionFactory sessionFactory) {
  4. this.sessionFactory = sessionFactory;
  5. }
  6. public Session getSession() {
  7. return sessionFactory.getCurrentSession();
  8. }
  9. @Override
  10. public <T> void save(T t) {
  11. getSession().save(t);
  12. }
  13. @Override
  14. public <T> void delete(T t) {
  15. getSession().delete(t);
  16. }
  17. @Override
  18. public <T> void delete(Class<T> entityClass, Integer id) {
  19. getSession().delete(get(entityClass, id));
  20. }
  21. @Override
  22. public <T> void update(T t) {
  23. getSession().update(t);
  24. }
  25. @Override
  26. public <T> T get(Class<T> entityClass, Integer id) {
  27. return (T) getSession().get(entityClass, id);
  28. }
  29. @Override
  30. public <T> List<T> findAll(String hql, Class<T> entityClass) {
  31. return findAll(hql, entityClass, new Object[] {});
  32. }
  33. @Override
  34. public <T> List<T> findAll(String hql, Class<T> entityClass, Object param) {
  35. return findAll(hql, entityClass, new Object[] { param });
  36. }
  37. @Override
  38. public <T> List<T> findAll(String hql, Class<T> entityClass,
  39. Object[] params) {
  40. Query query = getSession().createQuery(hql);
  41. for (int i = 0; i < params.length; i++) {
  42. query.setParameter(i, params[i]);
  43. }
  44. return (List<T>) query.list();
  45. }
  46. @Override
  47. public <T> List<T> findByPage(final String hql, Class<T> entityClass,
  48. final int firstResult, final int maxResult) {
  49. return findByPage(hql, entityClass, new Object[] {}, firstResult,
  50. maxResult);
  51. }
  52. @Override
  53. public <T> List<T> findByPage(final String hql, Class<T> entityClass,
  54. final Object param, final int firstResult, final int maxResult) {
  55. return findByPage(hql, entityClass, new Object[] { param },
  56. firstResult, maxResult);
  57. }
  58. @Override
  59. public <T> List<T> findByPage(final String hql, Class<T>
  60. entityClass, final Object[] params, final int firstResult,
  61. final int maxResult) {
  62. Query query = getSession().createQuery(hql);
  63. for (int i = 0; i < params.length; i++) {
  64. query.setParameter(i, params[i]);
  65. }
  66. query.setFirstResult(firstResult);
  67. query.setMaxResults(maxResult);
  68. return (List<T>) query.list();
  69. }
  70. }

8. Service层

  1. public interface UserService {
  2. public User login(User user);
  3. }
  1. public class UserServiceImpl implements UserService {
  2. private BaseDao baseDao;
  3. public void setBaseDao(BaseDao baseDao) {
  4. this.baseDao = baseDao;
  5. }
  6. @Override
  7. public User login(User user) {
  8. List<User> list = baseDao.findAll(
  9. "from User where username = ? and password = ?", User.class,
  10. new Object[] { user.getUsername(), user.getPassword() });
  11. if (list.size() == 1) {
  12. return list.get(0);
  13. }
  14. return null;
  15. }
  16. }

9. Action层

  1. public class ActionBase extends ActionSupport implements RequestAware,
  2. SessionAware, ApplicationAware {
  3. private static final long serialVersionUID = 1L;
  4. protected Map<String, Object> request;
  5. protected Map<String, Object> session;
  6. protected Map<String, Object> application;
  7. public Map<String, Object> getRequest() {
  8. return request;
  9. }
  10. public Map<String, Object> getSession() {
  11. return session;
  12. }
  13. public Map<String, Object> getApplication() {
  14. return application;
  15. }
  16. @Override
  17. public void setRequest(Map<String, Object> request) {
  18. this.request = request;
  19. }
  20. @Override
  21. public void setSession(Map<String, Object> session) {
  22. this.session = session;
  23. }
  24. @Override
  25. public void setApplication(Map<String, Object> application) {
  26. this.application = application;
  27. }
  28. }
  1. public class LoginAction extends ActionBase {
  2. private static final long serialVersionUID = 1L;
  3. private String username;
  4. private String password;
  5. private UserService userService;
  6. public void setUserService(UserService userService) {
  7. this.userService = userService;
  8. }
  9. public String login() throws Exception {
  10. User user = new User(username, password);
  11. User login = userService.login(user);
  12. if (login != null) {
  13. session.put("user", login);
  14. return SUCCESS;
  15. }
  16. return INPUT;
  17. }
  18. public String getUsername() {
  19. return username;
  20. }
  21. public void setUsername(String username) {
  22. this.username = username;
  23. }
  24. public String getPassword() {
  25. return password;
  26. }
  27. public void setPassword(String password) {
  28. this.password = password;
  29. }
  30. }

相关Jar包下载如下: