struts2 spring3 hibernate4 最简单整合

刚刚自学了一点ssh的内容,所以简单地把这三个框架整到一起!(这个只是最简单的整合,并不涉及高级的内容)

直接进入主题

一.首先配struts:

1.以下为刚开始要用到的jar包(要加的jar包每个版本好像都有变化,多试试就知道加要哪个包了,下面要加的包都同理):

jar包


2.接下来配web.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7.   <display-name></display-name>   
  8.     
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.   
  14.   
  15.     <filter-mapping>  
  16.         <filter-name>struts2</filter-name>  
  17.         <url-pattern>/*</url-pattern>  
  18.     </filter-mapping>  
  19.     
  20.   <welcome-file-list>  
  21.     <welcome-file>index.jsp</welcome-file>  
  22.   </welcome-file-list>  
  23. </web-app>  


3.接下来新建index.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>register page</title>  
  6.   </head>  
  7.   <body>  
  8.     <form action="register.action">  
  9.         username:<input type="text" name="username"/><br/>  
  10.         password:<input type="text" name="password"/><br/>  
  11.         <input type="submit" value="register"/>  
  12.     </form>  
  13.   </body>  
  14. </html>  


4.再新建registerOK.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>register success</title>  
  6.   </head>  
  7.   <body>  
  8.     congratulation!!!!   register success!!!!<br>  
  9.     <a href="index.jsp">back to the register Page!</a>  
  10.   </body>  
  11. </html>  


5.接下来新建action类RegisterAction.java

[java]  view plain copy
  1. package com.ice.action;  
  2.   
  3. import com.opensymphony.xwork2.Action;  
  4.   
  5. public class RegisterAction implements Action  
  6. {  
  7.   
  8.     private long userID;  
  9.     private String username;  
  10.     private String password;  
  11.       
  12.     public long getUserID()  
  13.     {  
  14.         return userID;  
  15.     }  
  16.   
  17.     public void setUserID(long userID)  
  18.     {  
  19.         this.userID = userID;  
  20.     }  
  21.   
  22.     public String getUsername()  
  23.     {  
  24.         return username;  
  25.     }  
  26.   
  27.     public void setUsername(String username)  
  28.     {  
  29.         this.username = username;  
  30.     }  
  31.   
  32.     public String getPassword()  
  33.     {  
  34.         return password;  
  35.     }  
  36.   
  37.     public void setPassword(String password)  
  38.     {  
  39.         this.password = password;  
  40.     }  
  41.   
  42.   
  43.     @Override  
  44.     public String execute() throws Exception  
  45.     {  
  46.         System.out.println(getUsername());  
  47.         System.out.println(getPassword());  
  48.         return SUCCESS;  
  49.     }  
  50.   
  51. }  


6.最后新建struts.xml:

[html]  view plain copy
  1. <!DOCTYPE struts PUBLIC  
  2.   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.   "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.   
  6.   <package name="register" namespace="/" extends="struts-default">  
  7.     <action name="register" class="com.ice.action.RegisterAction" >  
  8.         <result name="success">registerOK.jsp</result>  
  9.     </action>  
  10.   </package>  
  11. </struts>  


7.这时struts最简单的就完了。

以下为结果图:

这是控制台的输出:


二.把Spring整合进去(这一部分是使用xml配置的,只是写了,懒得删掉了睡觉,第三部分是使用Annotation配置的,本部分可以跳过直接进入第三部分

1.加入spring的jar包

另外还要加入一个包,在struts的目录下:


2.在web.xml加下spring的支持,以下为修改后的web.xml:

[html]  view plain copy
  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.   
  8.   
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.   
  14.   
  15.     <filter-mapping>  
  16.         <filter-name>struts2</filter-name>  
  17.         <url-pattern>/*</url-pattern>  
  18.     </filter-mapping>  
  19.   
  20.   
  21.     <welcome-file-list>  
  22.         <welcome-file>index.jsp</welcome-file>  
  23.     </welcome-file-list>  
  24.   
  25.   
  26.     <context-param>  
  27.         <param-name>contextConfigLocation</param-name>  
  28.         <param-value>classpath*:applicationContext.xml</param-value>  
  29.     </context-param>  
  30.   
  31.   
  32.     <listener>  
  33.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  34.     </listener>  
  35. </web-app>  


3.在src目录下新建一个applicationContext.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans   
  7.             http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.             http://www.springframework.org/schema/context  
  9.             http://www.springframework.org/schema/context/spring-context.xsd">  
  10.               
  11.     <bean id="reigsterAction"  
  12.         class="com.ice.action.RegisterAction">  
  13.     </bean>  
  14.   
  15.   
  16. </beans>  


4.个改struts.xml:

[html]  view plain copy
  1. <!DOCTYPE struts PUBLIC  
  2.   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.   "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.   
  6.   
  7.   <package name="register" namespace="/" extends="struts-default">  
  8.     <action name="register" class="reigsterAction" >  
  9.         <result name="success">registerOK.jsp</result>  
  10.     </action>  
  11.   </package>  
  12. </struts>  

5.结果就不上图了,因为跟前面一样,这一步只是把Actioin交给spring而己

三.把struts整合进spring(想一想spring好像用Annotation比较方便,那就改成Annotation吧!(那第二部分可以略过大笑大笑大笑))

1.加入spring的jar包

另外还要加入一个包,在struts的目录下:


2.在web.xml加下spring的支持,以下为修改后的web.xml:

[html]  view plain copy
  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.   
  8.   
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.   
  14.   
  15.     <filter-mapping>  
  16.         <filter-name>struts2</filter-name>  
  17.         <url-pattern>/*</url-pattern>  
  18.     </filter-mapping>  
  19.   
  20.   
  21.     <welcome-file-list>  
  22.         <welcome-file>index.jsp</welcome-file>  
  23.     </welcome-file-list>  
  24.   
  25.   
  26.     <context-param>  
  27.         <param-name>contextConfigLocation</param-name>  
  28.         <param-value>classpath*:applicationContext.xml</param-value>  
  29.     </context-param>  
  30.   
  31.   
  32.     <listener>  
  33.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  34.     </listener>  
  35. </web-app>  


3.在src目录下新建一个applicationContext.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans   
  7.             http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.             http://www.springframework.org/schema/context  
  9.             http://www.springframework.org/schema/context/spring-context.xsd">  
  10.               
  11.     <!-- 添加对Annotation的支持 -->  
  12.     <context:annotation-config />  
  13.       
  14.     <!-- 扫描该包下的所有Bean(@Component) -->  
  15.     <context:component-scan base-package="com.ice" />  
  16.   
  17.   
  18.   
  19.   
  20. </beans>  


4.个改struts.xml:

[html]  view plain copy
  1. <!DOCTYPE struts PUBLIC  
  2.   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.   "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.   
  6.   <package name="register" namespace="/" extends="struts-default">  
  7.     <action name="register" class="registerAction" >  
  8.         <result name="success">registerOK.jsp</result>  
  9.     </action>  
  10.   </package>  
  11. </struts>  


5.修改一下Aaction:

[java]  view plain copy
  1. package com.ice.action;  
  2.   
  3.   
  4. import org.springframework.context.annotation.Scope;  
  5. import org.springframework.stereotype.Controller;  
  6.   
  7.   
  8. import com.opensymphony.xwork2.Action;  
  9.   
  10.   
  11. @Controller(value="reigsterAction")  
  12. @Scope(value="prototype")  
  13. public class RegisterAction implements Action  
  14. {  
  15.   
  16.   
  17.     private long userID;  
  18.     private String username;  
  19.     private String password;  
  20.       
  21.     public long getUserID()  
  22.     {  
  23.         return userID;  
  24.     }  
  25.   
  26.   
  27.     public void setUserID(long userID)  
  28.     {  
  29.         this.userID = userID;  
  30.     }  
  31.   
  32.   
  33.     public String getUsername()  
  34.     {  
  35.         return username;  
  36.     }  
  37.   
  38.   
  39.     public void setUsername(String username)  
  40.     {  
  41.         this.username = username;  
  42.     }  
  43.   
  44.   
  45.     public String getPassword()  
  46.     {  
  47.         return password;  
  48.     }  
  49.   
  50.   
  51.     public void setPassword(String password)  
  52.     {  
  53.         this.password = password;  
  54.     }  
  55.   
  56.   
  57.   
  58.   
  59.     @Override  
  60.     public String execute() throws Exception  
  61.     {  
  62.         System.out.println(getUsername());  
  63.         System.out.println(getPassword());  
  64.         return SUCCESS;  
  65.     }  
  66.   
  67.   
  68. }  


5.结果就不上图了,因为跟前面一样,这一步只是把Actioin交给spring而己


四.把hibernate整进去;

1.还是先加包:

hibernate下的必需包(required):

还 需要dbcp的这两个包:


除此之外还需要再加这个包(在spring的目录下):



2.建立与数据库对映的model:

[java]  view plain copy
  1. package com.ice.model;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.GenerationType;  
  6. import javax.persistence.Id;  
  7. import javax.persistence.Table;  
  8.   
  9. @Entity  
  10. @Table(name="user_table")  
  11. public class UserModel  
  12. {  
  13.     private long userID;  
  14.     private String username;  
  15.     private String password;  
  16.       
  17.     @Id  
  18.     @GeneratedValue(strategy= GenerationType.AUTO)  
  19.     public long getUserID()  
  20.     {  
  21.         return userID;  
  22.     }  
  23.       
  24.     public void setUserID(long userID)  
  25.     {  
  26.         this.userID = userID;  
  27.     }  
  28.     public String getUsername()  
  29.     {  
  30.         return username;  
  31.     }  
  32.     public void setUsername(String username)  
  33.     {  
  34.         this.username = username;  
  35.     }  
  36.     public String getPassword()  
  37.     {  
  38.         return password;  
  39.     }  
  40.     public void setPassword(String password)  
  41.     {  
  42.         this.password = password;  
  43.     }  
  44. }  


3.建立DAO层:

[java]  view plain copy
  1. package com.ice.dao;  
  2.   
  3. import com.ice.model.UserModel;  
  4.   
  5. public interface UserDAO  
  6. {  
  7.     public void save(UserModel userModel);  
  8.       
  9. }  

[java]  view plain copy
  1. package com.ice.dao.impl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.Transaction;  
  8. import org.springframework.stereotype.Repository;  
  9.   
  10. import com.ice.dao.UserDAO;  
  11. import com.ice.model.UserModel;  
  12.   
  13. @Repository  
  14. public class UserDAOImpl implements UserDAO  
  15. {  
  16.     private SessionFactory sessionFactory;  
  17.   
  18.     public SessionFactory getSessionFactory()  
  19.     {  
  20.         return sessionFactory;  
  21.     }  
  22.   
  23.     @Resource(name="mySessionFactory")  
  24.     public void setSessionFactory(SessionFactory sessionFactory)  
  25.     {  
  26.         this.sessionFactory = sessionFactory;  
  27.     }  
  28.   
  29.     @Override  
  30.     public void save(UserModel userModel)  
  31.     {  
  32.         Session session = sessionFactory.getCurrentSession();  
  33.         Transaction tx = session.getTransaction();  
  34.         try{  
  35.             tx.begin();  
  36.             session.save(userModel);  
  37.             tx.commit();  
  38.         }catch (Exception e) {  
  39.             tx.rollback();  
  40.             e.printStackTrace();  
  41.         }  
  42.           
  43.     }  
  44.   
  45. }  


4.建立Service层:

[java]  view plain copy
  1. package com.ice.service;  
  2.   
  3. import com.ice.model.UserModel;  
  4.   
  5. public interface UserService  
  6. {  
  7.     public void save(UserModel userModel);  
  8. }  

[java]  view plain copy
  1. package com.ice.service.impl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Repository;  
  6. import org.springframework.stereotype.Service;  
  7.   
  8. import com.ice.dao.UserDAO;  
  9. import com.ice.model.UserModel;  
  10. import com.ice.service.UserService;  
  11.   
  12. @Service  
  13. public class UserServiceImpl implements UserService  
  14. {  
  15.   
  16.     private UserService loginService;  
  17.     private UserDAO loginDAO;  
  18.       
  19.     public UserService getLoginService()  
  20.     {  
  21.         return loginService;  
  22.     }  
  23.   
  24.     public void setLoginService(UserService loginService)  
  25.     {  
  26.         this.loginService = loginService;  
  27.     }  
  28.   
  29.     public UserDAO getLoginDAO()  
  30.     {  
  31.         return loginDAO;  
  32.     }  
  33.   
  34.     @Resource(name="userDAOImpl")  
  35.     public void setLoginDAO(UserDAO loginDAO)  
  36.     {  
  37.         this.loginDAO = loginDAO;  
  38.     }  
  39.   
  40.     @Override  
  41.     public void save(UserModel userModel)  
  42.     {  
  43.         loginDAO.save(userModel);  
  44.     }  
  45.   
  46. }  


5.修改Action:

[java]  view plain copy
  1. package com.ice.action;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.context.annotation.Scope;  
  6. import org.springframework.stereotype.Controller;  
  7.   
  8. import com.ice.model.UserModel;  
  9. import com.ice.service.UserService;  
  10. import com.opensymphony.xwork2.Action;  
  11.   
  12. @Controller(value="registerAction")  
  13. @Scope(value="prototype")  
  14. public class RegisterAction implements Action  
  15. {  
  16.   
  17.     private long userID;  
  18.     private String username;  
  19.     private String password;  
  20.     private UserService loginService;  
  21.       
  22.     public long getUserID()  
  23.     {  
  24.         return userID;  
  25.     }  
  26.   
  27.     public void setUserID(long userID)  
  28.     {  
  29.         this.userID = userID;  
  30.     }  
  31.   
  32.     public String getUsername()  
  33.     {  
  34.         return username;  
  35.     }  
  36.   
  37.     public void setUsername(String username)  
  38.     {  
  39.         this.username = username;  
  40.     }  
  41.   
  42.     public String getPassword()  
  43.     {  
  44.         return password;  
  45.     }  
  46.   
  47.     public void setPassword(String password)  
  48.     {  
  49.         this.password = password;  
  50.     }  
  51.   
  52.     public UserService getLoginService()  
  53.     {  
  54.         return loginService;  
  55.     }  
  56.       
  57.     @Resource(name="userServiceImpl")  
  58.     public void setLoginService(UserService loginService)  
  59.     {  
  60.         this.loginService = loginService;  
  61.     }  
  62.   
  63.     @Override  
  64.     public String execute() throws Exception  
  65.     {  
  66.         UserModel userModel = new UserModel();  
  67.         userModel.setUserID(userID);  
  68.         userModel.setUsername(username);  
  69.         userModel.setPassword(password);  
  70.         loginService.save(userModel);  
  71.         return SUCCESS;  
  72.     }  
  73.   
  74. }  


6.把hibernate交给spring:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans   
  7.             http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.             http://www.springframework.org/schema/context  
  9.             http://www.springframework.org/schema/context/spring-context.xsd">  
  10.               
  11.     <!-- 添加对Annotation的支持 -->  
  12.     <context:annotation-config />  
  13.       
  14.     <!-- 扫描该包下的所有Bean(@Component) -->  
  15.     <context:component-scan base-package="com.ice" />  
  16.       
  17.     <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  18.         destroy-method="close">  
  19.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  20.         <property name="url" value="jdbc:mysql://localhost:3306/ssh" />  
  21.         <property name="username" value="root" />  
  22.         <property name="password" value="" />  
  23.     </bean>  
  24.   
  25.   
  26.     <bean id="mySessionFactory"  
  27.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  28.         <property name="dataSource" ref="myDataSource" />  
  29.         <property name="packagesToScan">  
  30.             <list>  
  31.                 <value>com.ice.model</value>  
  32.             </list>  
  33.         </property>  
  34.         <property name="hibernateProperties">  
  35.             <props>  
  36.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  37.                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>  
  38.                 <prop key="hibernate.current_session_context_class">thread</prop>  
  39.                 <prop key="hibernate.show_sql">true</prop>  
  40.                 <prop key="hibernate.format_sql">true</prop>  
  41.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  42.             </props>  
  43.         </property>  
  44.     </bean>  
  45.   
  46.   
  47. </beans>  


7.在数据库中新建一个数据库:

[sql]  view plain copy
  1. create database ssh;  

8.运行,成功!结果最后结果:



五.这是我第一次技术博客,大家多多指教!

这是项目的源文件:http://download.csdn.net/detail/yingbingxue/5356867

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值