Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9_配置步骤

【版权声明:本文为新价值网原创,未经准许谢绝转载。如需转载,请务必在转载时注明本博客地址。】

一. 创建项目

二.    搭建struts-2.3.4.11.struts2必须的Jar包(放到WEB-INF/lib目录下):

2.配置struts2.3的过滤器

web.xml位置

 

Web.xml内容

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0" 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.     <display-name></display-name> 
  7.     <welcome-file-list> 
  8.         <welcome-file>index.jsp</welcome-file> 
  9.     </welcome-file-list> 
  10.  
  11.     <!-- struts2拦截器 --> 
  12.     <filter> 
  13.         <filter-name>struts2</filter-name> 
  14.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  15.     </filter> 
  16.     <filter-mapping> 
  17.         <filter-name>struts2</filter-name> 
  18.         <url-pattern>/*</url-pattern> 
  19.     </filter-mapping> 
  20.  
  21. </web-app> 

配置struts.xml(struts.xml在src目录下)

 

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.  
  6. <struts> 
  7.     <package name="struts2" extends="struts-default"> 
  8.         <action name="TestAction" class="com.xinvalue.action.TestAction"> 
  9.             <result name="success">/test.jsp</result> 
  10.         </action> 
  11.     </package> 
  12. </struts>     

4.测试struts2配置

新建一个TestAction

TestAction.java

  1. package com.xinvalue.action; 
  2. import com.opensymphony.xwork2.ActionSupport; 
  3. public class TestAction extends ActionSupport { 
  4.     @Override 
  5.     public String execute() throws Exception { 
  6.     return super.execute(); 
  7.     } 

新建一个测试页面

 

成功界面:


至此,struts2集成完毕!

整合Spring 3.2.3和Struts-2.3.4.11.

必须的jar包

在配置好的strutsjar的基础上,添加额外Struts jar包:

struts2-spring-plugin-2.3.4.1.jar

commons-logging-1.1.1.jar

Spring的jar包:

2.web.xml配置

Web.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0" 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.     <display-name></display-name> 
  7.     <welcome-file-list> 
  8.         <welcome-file>index.jsp</welcome-file> 
  9.     </welcome-file-list> 
  10.  
  11.     <!-- struts2拦截器 --> 
  12.     <filter> 
  13.         <filter-name>struts2</filter-name> 
  14.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  15.     </filter> 
  16.     <filter-mapping> 
  17.         <filter-name>struts2</filter-name> 
  18.         <url-pattern>/*</url-pattern> 
  19.     </filter-mapping> 
  20.  
  21.     <!-- 创建spring工厂监听器 --> 
  22.     <listener> 
  23.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  24.     </listener> 
  25.  
  26.     <!-- 告知spring context config location 的存储位置 --> 
  27.     <context-param> 
  28.         <param-name>contextConfigLocation</param-name> 
  29.         <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
  30.     </context-param> 
  31.  
  32. </web-app> 

3.spring的applicationContext.xml配置

 

 

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" xmlns:p="http://www.springframework.org/schema/p" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.             http://www.springframework.org/schema/tx 
  8.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  9.             http://www.springframework.org/schema/aop 
  10.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
  11.             http://www.springframework.org/schema/context 
  12.             http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  13.  
  14. </beans> 

整合Hibernate4.1.9
1.必需jar包
添加Hibernate jar文件
Hibernate压缩包中的
lib/required/

 

Spring中的jar文件

数据库连接池支持文件

以及连接mysql的jar
mysql-connector-java-5.1.22-bin.jar

2.配置文件applicationContext.xml

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" xmlns:p="http://www.springframework.org/schema/p" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.             http://www.springframework.org/schema/tx 
  8.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  9.             http://www.springframework.org/schema/aop 
  10.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
  11.             http://www.springframework.org/schema/context 
  12.             http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  13.  
  14.     <!-- 数据库连接 --> 
  15.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
  16.         destroy-method="close"> 
  17.         <property name="driverClassName"> 
  18.             <value>com.mysql.jdbc.Driver</value> 
  19.         </property> 
  20.  
  21.         <property name="url"> 
  22.             <value>jdbc:mysql://localhost:3306/ssh2?characterEncoding=utf8</value> 
  23.         </property> 
  24.  
  25.         <property name="username"> 
  26.             <value>root</value> 
  27.         </property> 
  28.  
  29.         <property name="password"> 
  30.             <value></value> 
  31.         </property> 
  32.     </bean> 
  33.  
  34.     <!--Hibernate的Spring配置 --> 
  35.     <bean id="sessionFactory" 
  36.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
  37.         <!-- 数据库连接 --> 
  38.         <property name="dataSource"> 
  39.             <ref local="dataSource" /> 
  40.         </property> 
  41.  
  42.         <!-- hibernate自身属性 --> 
  43.         <property name="hibernateProperties"> 
  44.             <props> 
  45.                 <prop key="hibernate.show_sql">true</prop> 
  46.                 <prop key="hibernate.format_sql">true</prop> 
  47.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
  48.                 <!-- 解决no session found --> 
  49.                 <prop key="hibernate.current_session_context_class">thread</prop> 
  50.             </props> 
  51.         </property> 
  52.  
  53.         <!-- 映射文件 --> 
  54.         <property name="annotatedClasses"> 
  55.             <list> 
  56.                 <value>com.xinvalue.bean.User</value> 
  57.             </list> 
  58.         </property> 
  59.     </bean> 
  60.  
  61.     <!-- 用户Dao --> 
  62.     <bean id="userDao" class="com.xinvalue.dao.impl.UserDaoImpl" 
  63.         scope="singleton"> 
  64.         <property name="sessionFactory"> 
  65.             <ref local="sessionFactory" /> 
  66.         </property> 
  67.     </bean> 
  68.  
  69.     <!-- 用户Service --> 
  70.     <bean id="userService" class="com.xinvalue.service.impl.UserServiceImpl" 
  71.         scope="singleton"> 
  72.         <property name="userDao"> 
  73.             <ref local="userDao" /> 
  74.         </property> 
  75.     </bean> 
  76.  
  77.     <!-- 用户Action --> 
  78.     <bean id="saveUserAction" class="com.xinvalue.action.SaveUserAction" 
  79.         scope="prototype"> 
  80.         <property name="userService"> 
  81.             <ref local="userService" /> 
  82.         </property> 
  83.     </bean> 
  84.  
  85. </beans> 

创建测试数据库

Sql代码

  1. CREATE DATABASE `ssh2` ; 
  2.  
  3. USE `ssh2`; 
  4.  
  5. DROP TABLE IF EXISTS `users`; 
  6.  
  7. CREATE TABLE `users` ( 
  8.   `userid` int(11) NOT NULL AUTO_INCREMENT, 
  9.   `username` varchar(20) DEFAULT NULL
  10.   `userpwd` varchar(20) DEFAULT NULL
  11.   PRIMARY KEY (`userid`) 
  12. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC
  13.  
  14. insert  into `users`(`userid`,`username`,`userpwd`) values (1,'terwer','123456'); 

创建bean

 

User.java

  1. package com.xinvalue.bean; 
  2.  
  3. import javax.persistence.Column; 
  4. import javax.persistence.Entity; 
  5. import javax.persistence.Id; 
  6. import javax.persistence.Table; 
  7.  
  8. @Entity 
  9. @Table(name = "users"
  10. public class User { 
  11.     @Id 
  12.     private int userId; 
  13.     @Column(name = "username"
  14.     private String username; 
  15.     @Column(name = "userpwd"
  16.     private String userpwd; 
  17.  
  18.     public int getUserId() { 
  19.     return userId; 
  20.     } 
  21.  
  22.     public void setUserId(int userId) { 
  23.     this.userId = userId; 
  24.     } 
  25.  
  26.     public String getUsername() { 
  27.     return username; 
  28.     } 
  29.  
  30.     public void setUsername(String username) { 
  31.     this.username = username; 
  32.     } 
  33.  
  34.     public String getUserpwd() { 
  35.     return userpwd; 
  36.     } 
  37.  
  38.     public void setUserpwd(String userpwd) { 
  39.     this.userpwd = userpwd; 
  40.     } 

创建dao

 

 

UserDao.java

  1. package com.xinvalue.dao; 
  2.  
  3. import java.util.List; 
  4.  
  5. import com.xinvalue.bean.User; 
  6.  
  7. public interface UserDao { 
  8.     public List<User> queryAllUsers(); 
  9.  
  10.     boolean saveUser(User user); 

UserDaoImpl.java

  1. package com.xinvalue.dao.impl; 
  2.  
  3. import java.util.List; 
  4.  
  5. import org.hibernate.Session; 
  6. import org.hibernate.SessionFactory; 
  7. import org.hibernate.Transaction; 
  8. import com.xinvalue.bean.User; 
  9. import com.xinvalue.dao.UserDao; 
  10.  
  11. public class UserDaoImpl implements UserDao { 
  12.     private SessionFactory sessionFactory; 
  13.  
  14.     public SessionFactory getSessionFactory() { 
  15.     return sessionFactory; 
  16.     } 
  17.  
  18.     public void setSessionFactory(SessionFactory sessionFactory) { 
  19.     this.sessionFactory = sessionFactory; 
  20.     } 
  21.  
  22.     @Override 
  23.     public List<User> queryAllUsers() { 
  24.     // TODO Auto-generated method stub 
  25.     return null
  26.     } 
  27.  
  28.     @Override 
  29.     public boolean saveUser(User user) { 
  30.     Session session = sessionFactory.getCurrentSession(); 
  31.     Transaction tx = session.beginTransaction(); 
  32.     try
  33.         session.save(user); 
  34.         tx.commit(); 
  35.         return true
  36.     } catch (Exception e) { 
  37.         if (e != null) { 
  38.         tx.rollback(); 
  39.         } 
  40.     } 
  41.     return false
  42.     } 

创建Service

 

 

UserService.java

  1. package com.xinvalue.service; 
  2.  
  3. import java.util.List; 
  4.  
  5. import com.xinvalue.bean.User; 
  6.  
  7. public interface UserService { 
  8.     public List<User> queryAllUsers(); 
  9.  
  10.     boolean saveUser(User user); 

 

UserServiceImpl.java

  1. package com.xinvalue.service.impl; 
  2.  
  3. import java.util.List; 
  4.  
  5. import com.xinvalue.bean.User; 
  6. import com.xinvalue.dao.UserDao; 
  7. import com.xinvalue.service.UserService; 
  8.  
  9. public class UserServiceImpl implements UserService { 
  10.      private UserDao userDao; 
  11.  
  12.      public UserDao getUserDao() { 
  13.         return userDao; 
  14.     } 
  15.  
  16.     public void setUserDao(UserDao userDao) { 
  17.         this.userDao = userDao; 
  18.     } 
  19.  
  20.     @Override 
  21.     public List<User> queryAllUsers() { 
  22.     return userDao.queryAllUsers(); 
  23.     } 
  24.  
  25.     @Override 
  26.     public boolean saveUser(User user) { 
  27.     return userDao.saveUser(user); 
  28.     } 
  29.  

创建Action

 

SaveUserAction.java

  1. package com.xinvalue.action; 
  2.  
  3. import com.opensymphony.xwork2.ActionSupport; 
  4. import com.xinvalue.bean.User; 
  5. import com.xinvalue.service.UserService; 
  6.  
  7. public class SaveUserAction extends ActionSupport { 
  8.  
  9.     private UserService userService; 
  10.     private String username; 
  11.     private String userpwd; 
  12.  
  13.     public UserService getUserService() { 
  14.     return userService; 
  15.     } 
  16.  
  17.     public void setUserService(UserService userService) { 
  18.     this.userService = userService; 
  19.     } 
  20.  
  21.     public String getUsername() { 
  22.     return username; 
  23.     } 
  24.  
  25.     public void setUsername(String username) { 
  26.     this.username = username; 
  27.     } 
  28.  
  29.     public String getUserpwd() { 
  30.     return userpwd; 
  31.     } 
  32.  
  33.     public void setUserpwd(String userpwd) { 
  34.     this.userpwd = userpwd; 
  35.     } 
  36.  
  37.     @Override 
  38.     public String execute() throws Exception { 
  39.     User user = new User(); 
  40.     user.setUsername(this.getUsername()); 
  41.     user.setUserpwd(this.getUserpwd()); 
  42.     boolean status=userService.saveUser(user); 
  43.     System.out.println(status); 
  44.     return SUCCESS; 
  45.     } 

创建测试页面:

addUser.jsp

  1. <body> 
  2.     <s:form action="SaveUserAction.action"> 
  3.         <s:textfield name="username" label="用户名"/> 
  4.         <s:password name="userpwd" label="密码"/> 
  5.         <s:submit label="注册"/> 
  6.     </s:form> 
  7. </body> 

成功界面:

 

 

 

至此,全部整合完毕!

附:项目所有jar包下载:ss2-libs

如有大家有什么问题,可以给我留言,我会抽时间一一解答。

笔者微博:@LeaveBugsAway欢迎叨扰。

你可能感兴趣的:(Hibernate4)