【转载】Ssh整合开发介绍和简单的登入案例实现

Ssh整合开发介绍和简单的登入案例实现


Ssh整合开发介绍和简单的登入案例实现  

    1. 一  介绍:  
    2. Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2  
    3. 二  搭建环境  
    4. 1.  首先要先引入struts2和sping和hibernate所需要的包  
    5. 1)struts2的包为:  
    6. struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar  
    7. struts-2.3.1.2\lib\ognl-3.0.4.jar  
    8. struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar  
    9. struts-2.3.1.2\lib\ freemarker-2.3.18.jar  
    10. struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar  
    11. struts-2.3.1.2\lib\ commons-io-2.0.1.jar  
    12. struts-2.3.1.2\lib\commons-lang-2.5.jar  
    13. struts-2.3.1.2\lib\commons-logging-1.1.1.jar  
    14. struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar  
    15. struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar  
    16. (2)引入hibernate的包  
    17. hibernate-distribution-3.6.8.Final\lib\required\*.jar  所有的jar包  
    18. hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar  
    19. hibernate-distribution-3.6.8.Final\ hibernate3.jar  
    20. (3)spring的包为:  
    21.     spring-framework-2.5.6\dist\ spring.jar  
    22.     spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar  
    23.     spring-framework-2.5.6\lib\aspectj\*.jar  
    24.     spring-framework-2.5.6\lib\j2ee\common-annotations.jar  
    25.     spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar  
    26.     spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar  
    27. spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar    
    28. spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar  
    29. 2.  配置spring下所需要的文件  
    30. 1)首先配置spring所需要的xml文件  
    31.     我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:  
    32. <?xml version="1.0" encoding="UTF-8"?>  
    33. <beans xmlns="http://www.springframework.org/schema/beans"  
    34.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
    35.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    36.     xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    37.            http://www.springframework.org/schema/aop  
    38.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    39.            http://www.springframework.org/schema/context  
    40.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
    41.            http://www.springframework.org/schema/tx  
    42.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    43. </beans>  
    44. 2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池  
    45.     <!-- 数据库的连接池 -->  
    46.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
    47.         destroy-method="close">  
    48.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
    49.         <property name="jdbcUrl"  
    50.     value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />  
    51.         <property name="user" value="root" />  
    52.         <property name="password" value="1234" />  
    53.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
    54.         <property name="initialPoolSize" value="1" />  
    55.         <!--连接池中保留的最小连接数。 -->  
    56.         <property name="minPoolSize" value="1" />  
    57.         <!--连接池中保留的最大连接数。Default: 15 -->  
    58.         <property name="maxPoolSize" value="300" />  
    59.         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
    60.         <property name="maxIdleTime" value="60" />  
    61.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
    62.         <property name="acquireIncrement" value="5" />  
    63.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->  
    64.         <property name="idleConnectionTestPeriod" value="60" />  
    65.     </bean>  
    66.   
    67. 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:  
    68. <!-- 读取外部的文件 -->  
    69. <context:property-placeholder location="jdbc.properties" />  
    70.   
    71. (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置  
    72.     <!-- sessionFactory工厂 -->  
    73.     <bean id="sessionFactory"  
    74.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
    75.         <!-- 注入数据源 -->  
    76.         <property name="dataSource" ref="dataSource"></property>  
    77.         <!-- hibernate映射文件的引入 -->  
    78.         <property name="mappingResources">  
    79.             <list>  
    80.                 <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>  
    81.             </list>  
    82.         </property>  
    83.         <!-- 配置hibernate属性的设置 -->  
    84.         <property name="hibernateProperties">  
    85.             <props>  
    86.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
    87.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
    88.                 <prop key="hibernate.show_sql">true</prop>  
    89.                 <prop key="hibernate.format_sql">true</prop>  
    90.             </props>  
    91.         </property>  
    92.     </bean>  
    93. 3.配置struts2.xml中需要的内容  
    94. (1)配置基本的struts2.xml   
    95.  <!DOCTYPE struts PUBLIC  
    96.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    97.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
    98. <struts>  
    99.     <!--使用spring创建管理struts2的action操作 -->  
    100.     <constant name="struts.objectFactory" value="spring"/>  
    101.     <include file="struts-admin.xml"></include>  
    102. </struts>  
    103. 注:其中include是引入的文件,一下的语句是非常重要的:  
    104. <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键  
    105. (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现  
    106.   <!DOCTYPE struts PUBLIC  
    107.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    108.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
    109. <struts>  
    110.     <package name="admin" extends="struts-default" namespace="/csdn">  
    111.         <!-- class的名称  等于spring中action配置文件中的id名称 -->  
    112.         <action name="loginAdmin" class="adminAction" method="login">  
    113.             <result name="success">../index.jsp</result>  
    114.         </action>  
    115.     </package>  
    116. </struts>  
    117. 4.  Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了  
    118. 5.搭建层之间的关系  
    119. (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例  
    120. 属性为id和name和pass  
    121. 封装的实体类为:  
    122. package cn.csdn.hr.s2sh.domain;  
    123. import java.io.Serializable;  
    124. public class Admin implements Serializable {  
    125.     private static final long serialVersionUID = 1L;  
    126.     private Integer id;  
    127.     private String name;  
    128.     private String pass;  
    129.   
    130.     public Admin() {  
    131.         super();  
    132.         // TODO Auto-generated constructor stub  
    133.     }  
    134.   
    135.     public Admin(Integer id, String name, String pass) {  
    136.         super();  
    137.         this.id = id;  
    138.         this.name = name;  
    139.         this.pass = pass;  
    140.     }  
    141.   
    142.     public Integer getId() {  
    143.         return id;  
    144.     }  
    145.   
    146.     public void setId(Integer id) {  
    147.         this.id = id;  
    148.     }  
    149.   
    150.     public String getName() {  
    151.         return name;  
    152.     }  
    153.   
    154.     public void setName(String name) {  
    155.         this.name = name;  
    156.     }  
    157.   
    158.     public String getPass() {  
    159.         return pass;  
    160.     }  
    161.   
    162.     public void setPass(String pass) {  
    163.         this.pass = pass;  
    164.     }  
    165.   
    166.     @Override  
    167.     public String toString() {  
    168.         return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";  
    169.     }  
    170. }  
    171. (2)在同一个包下的实体类的映射文件  
    172. <?xml version="1.0" encoding="UTF-8"?>  
    173. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    174.                                    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
    175. <hibernate-mapping package="cn.csdn.hr.s2sh.domain">  
    176.     <class name="Admin" table="admin">  
    177.         <id name="id" column="id">  
    178.             <generator class="native"></generator>  
    179.         </id>  
    180.         <property name="name" column="name"></property>  
    181.         <property name="pass" column="pass"></property>  
    182.     </class>  
    183. </hibernate-mapping>  
    184.   
    185. (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl  
    186. 下面是创建的接口 AdminDao.java  
    187. package cn.csdn.hr.s2sh.dao;  
    188.   
    189. import java.util.List;  
    190.   
    191. import cn.csdn.hr.s2sh.domain.Admin;  
    192.   
    193. public interface AdminDao {  
    194.     public Admin login(final String name,final String pass);  
    195. }  
    196.   
    197. 创建的类为AdminDaoImpl.java  
    198. package cn.csdn.hr.s2sh.dao;  
    199. import java.sql.SQLException;  
    200. import java.util.List;  
    201. import org.hibernate.HibernateException;  
    202. import org.hibernate.Session;  
    203. import org.springframework.orm.hibernate3.HibernateCallback;  
    204. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
    205.   
    206. import cn.csdn.hr.s2sh.domain.Admin;  
    207.   
    208. @SuppressWarnings("unchecked")  
    209. public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {  
    210.     public Admin login(final String name, final String pass) {  
    211.   
    212.         return (Admin) this.getHibernateTemplate().execute(  
    213.                 new HibernateCallback() {  
    214.   
    215.                     public Object doInHibernate(Session session)  
    216.                             throws HibernateException, SQLException {  
    217.                         // TODO Auto-generated method stub  
    218.   
    219.                         Object obj = session  
    220.                                 .createQuery(  
    221.                                         "from Admin where name=:name and pass=:pass")  
    222.                                 .setString("name", name)  
    223.                                 .setString("pass", pass).uniqueResult();  
    224.                         return obj;  
    225.                     }  
    226.                 });  
    227.     }  
    228. }  
    229.   
    230. (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service  
    231. 创建的AdminService.java接口  
    232. package cn.csdn.hr.s2sh.service;  
    233.   
    234. import cn.csdn.hr.s2sh.dao.AdminDao;  
    235.   
    236.   
    237. public interface AdminService extends AdminDao{  
    238.   
    239. }  
    240. 创建的AdminServiceImpl.java  
    241. package cn.csdn.hr.s2sh.service;  
    242. import java.util.List;  
    243. import org.springframework.transaction.TransactionStatus;  
    244. import org.springframework.transaction.support.TransactionCallback;  
    245. import org.springframework.transaction.support.TransactionTemplate;  
    246. import cn.csdn.hr.s2sh.dao.AdminDao;  
    247. import cn.csdn.hr.s2sh.domain.Admin;  
    248. public class AdminServiceImpl implements AdminService {  
    249.   
    250.     private AdminDao adminDao;  
    251.     public void setAdminDao(AdminDao adminDao) {  
    252.         this.adminDao = adminDao;  
    253.     }  
    254.     public Admin login(String name, String pass) {  
    255.         // TODO Auto-generated method stub  
    256.         return adminDao.login(name, pass);  
    257.     }  
    258. }  
    259.   
    260.   
    261. 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中  
    262. <?xml version="1.0" encoding="UTF-8"?>  
    263. <beans xmlns="http://www.springframework.org/schema/beans"  
    264.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
    265.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    266.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    267.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    268.            http://www.springframework.org/schema/aop  
    269.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    270.            http://www.springframework.org/schema/context  
    271.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
    272.            http://www.springframework.org/schema/tx  
    273.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    274.   
    275.     <!-- 创建struts2中的action -->  
    276.     <!-- 配置admin的action -->  
    277.     <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"  
    278.         scope="prototype">  
    279.         <property name="adminService" ref="adminServiceImpl"></property>  
    280.     </bean>  
    281. </beans>  
    282.   
    283. 而ref的adminServiceImpl是在bean-service.xml中  
    284. <?xml version="1.0" encoding="UTF-8"?>  
    285. <beans xmlns="http://www.springframework.org/schema/beans"  
    286.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
    287.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    288.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    289.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    290.            http://www.springframework.org/schema/aop  
    291.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    292.            http://www.springframework.org/schema/context  
    293.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
    294.            http://www.springframework.org/schema/tx  
    295.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    296.   
    297.     <!-- admin的业务操作 -->  
    298.     <bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl">  
    299.         <property name="adminDao" ref="adminDaoImpl"></property>  
    300.     </bean>  
    301. </beans>  
    302.   
    303. 上面的ref是adminDaoImpl,在beans-dao.xml  
    304. <?xml version="1.0" encoding="UTF-8"?>  
    305. <beans xmlns="http://www.springframework.org/schema/beans"  
    306.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
    307.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    308.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    309.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    310.            http://www.springframework.org/schema/aop  
    311.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    312.            http://www.springframework.org/schema/context  
    313.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
    314.            http://www.springframework.org/schema/tx  
    315.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    316.   
    317.   
    318.     <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->  
    319.     <bean id="hibernateDaoSupport"  
    320.         class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"  
    321.         abstract="true">  
    322.         <property name="sessionFactory" ref="sessionFactory"></property>  
    323.     </bean>  
    324.     <!-- admin的dao对象 -->  
    325.     <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"  
    326.         parent="hibernateDaoSupport" />  
    327. </beans>  
    328.   
    329. 然后我们把上面的三个beans.xml导入到applicationContext.xml中  
    330. <!-- 导入其他的配置文件 -->  
    331. <import resource="beans-dao.xml" />  
    332. <import resource="beans-service.xml" />  
    333. <import resource="beans-action.xml" />  
    334.   
    335. 7.配置web.xml  
    336. <?xml version="1.0" encoding="UTF-8"?>  
    337. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    338.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    339.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    340.     id="WebApp_ID" version="2.5">  
    341.     <display-name>s2sh</display-name>  
    342.     <welcome-file-list>  
    343.         <welcome-file>index.html</welcome-file>  
    344.         <welcome-file>index.htm</welcome-file>  
    345.         <welcome-file>index.jsp</welcome-file>  
    346.         <welcome-file>default.html</welcome-file>  
    347.         <welcome-file>default.htm</welcome-file>  
    348.         <welcome-file>default.jsp</welcome-file>  
    349.     </welcome-file-list>  
    350.   
    351.     <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->  
    352.     <context-param>  
    353.         <param-name>contextConfigLocation</param-name>  
    354.         <param-value>classpath:applic*.xml</param-value>  
    355.     </context-param>  
    356.     <!-- 对Spring容器进行实例化 -->  
    357.     <listener>  
    358.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    359.     </listener>  
    360.   
    361.     <!-- struts2 的配置 -->  
    362.     <filter>  
    363.         <filter-name>struts2</filter-name>  
    364.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    365.     </filter>  
    366.   
    367.     <filter-mapping>  
    368.         <filter-name>struts2</filter-name>  
    369.         <url-pattern>/*</url-pattern>  
    370.     </filter-mapping>  
    371.   
    372.     <!-- 配置 使用spring解决hibernate因session关闭导致的延迟加载例外问题 -->  
    373.     <filter>  
    374.         <filter-name>OpenSessionInViewFilter</filter-name>  
    375.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
    376.         <init-param>  
    377.             <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 -->  
    378.             <param-name>sessionFactoryBeanName</param-name>  
    379.             <param-value>sessionFactory</param-value>  
    380.         </init-param>  
    381.     </filter>  
    382.     <filter-mapping>  
    383.         <filter-name>OpenSessionInViewFilter</filter-name>  
    384.         <url-pattern>/*</url-pattern>  
    385.     </filter-mapping>  
    386.   
    387.     <!-- 使用spring解决struts2的中文乱码的问题 -->  
    388.     <filter>  
    389.         <filter-name>encoding</filter-name>     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    390.         <init-param>  
    391.             <param-name>encoding</param-name>  
    392.             <param-value>UTF-8</param-value>  
    393.         </init-param>  
    394.     </filter>  
    395.     <filter-mapping>  
    396.         <filter-name>encoding</filter-name>  
    397.         <url-pattern>/*</url-pattern>  
    398.     </filter-mapping>  
    399. </web-app>  
    400.   
    401. 8.我们来做一个简单的登入,使用的jsp页面为:  
    402. <body>  
    403.     <div>  
    404.         <form action="/s2sh/csdn/loginAdmin.action" method="get">  
    405.             用户名:<input type="text" name="admin.name"/><br/>  
    406.             密  码:<input type="password" name="admin.pass"/><br/>  
    407.             <input type="submit" value="登入"/>  
    408.             <input type="reset" value="重置"/>  
    409.         </form>  
    410.     </div>  
    411.     <div>  
    412.         用户名为:${admin.name}  
    413.     </div>  
    414. </body>  
    415.   
    416. 访问到的action的处理为:  
    417. package cn.csdn.hr.s2sh.action;  
    418. import cn.csdn.hr.s2sh.domain.Admin;  
    419. import cn.csdn.hr.s2sh.service.AdminService;  
    420. import com.opensymphony.xwork2.ActionSupport;  
    421. public class AdminAction extends ActionSupport {  
    422.     private static final long serialVersionUID = 1L;  
    423.   
    424.     private AdminService adminService;  
    425.   
    426.     private Admin admin;  
    427.   
    428.     public Admin getAdmin() {  
    429.         return admin;  
    430.     }  
    431.   
    432.     public void setAdmin(Admin admin) {  
    433.         this.admin = admin;  
    434.     }  
    435.   
    436.     // 注入  
    437.     public void setAdminService(AdminService adminService) {  
    438.       

        1. Ssh整合开发介绍和简单的登入案例实现  
        2. 一  介绍:  
        3. Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2  
        4. 二  搭建环境  
        5. 1.  首先要先引入struts2和sping和hibernate所需要的包  
        6. 1)struts2的包为:  
        7. struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar  
        8. struts-2.3.1.2\lib\ognl-3.0.4.jar  
        9. struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar  
        10. struts-2.3.1.2\lib\ freemarker-2.3.18.jar  
        11. struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar  
        12. struts-2.3.1.2\lib\ commons-io-2.0.1.jar  
        13. struts-2.3.1.2\lib\commons-lang-2.5.jar  
        14. struts-2.3.1.2\lib\commons-logging-1.1.1.jar  
        15. struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar  
        16. struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar  
        17. (2)引入hibernate的包  
        18. hibernate-distribution-3.6.8.Final\lib\required\*.jar  所有的jar包  
        19. hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar  
        20. hibernate-distribution-3.6.8.Final\ hibernate3.jar  
        21. (3)spring的包为:  
        22.     spring-framework-2.5.6\dist\ spring.jar  
        23.     spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar  
        24.     spring-framework-2.5.6\lib\aspectj\*.jar  
        25.     spring-framework-2.5.6\lib\j2ee\common-annotations.jar  
        26.     spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar  
        27.     spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar  
        28. spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar    
        29. spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar  
        30. 2.  配置spring下所需要的文件  
        31. 1)首先配置spring所需要的xml文件  
        32.     我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:  
        33. <?xml version="1.0" encoding="UTF-8"?>  
        34. <beans xmlns="http://www.springframework.org/schema/beans"  
        35.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
        36.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        37.     xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
        38.            http://www.springframework.org/schema/aop  
        39.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
        40.            http://www.springframework.org/schema/context  
        41.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
        42.            http://www.springframework.org/schema/tx  
        43.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
        44. </beans>  
        45. 2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池  
        46.     <!-- 数据库的连接池 -->  
        47.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        48.         destroy-method="close">  
        49.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
        50.         <property name="jdbcUrl"  
        51.     value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />  
        52.         <property name="user" value="root" />  
        53.         <property name="password" value="1234" />  
        54.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
        55.         <property name="initialPoolSize" value="1" />  
        56.         <!--连接池中保留的最小连接数。 -->  
        57.         <property name="minPoolSize" value="1" />  
        58.         <!--连接池中保留的最大连接数。Default: 15 -->  
        59.         <property name="maxPoolSize" value="300" />  
        60.         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
        61.         <property name="maxIdleTime" value="60" />  
        62.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
        63.         <property name="acquireIncrement" value="5" />  
        64.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->  
        65.         <property name="idleConnectionTestPeriod" value="60" />  
        66.     </bean>  
        67.   
        68. 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:  
        69. <!-- 读取外部的文件 -->  
        70. <context:property-placeholder location="jdbc.properties" />  
        71.   
        72. (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置  
        73.     <!-- sessionFactory工厂 -->  
        74.     <bean id="sessionFactory"  
        75.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        76.         <!-- 注入数据源 -->  
        77.         <property name="dataSource" ref="dataSource"></property>  
        78.         <!-- hibernate映射文件的引入 -->  
        79.         <property name="mappingResources">  
        80.             <list>  
        81.                 <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>  
        82.             </list>  
        83.         </property>  
        84.         <!-- 配置hibernate属性的设置 -->  
        85.         <property name="hibernateProperties">  
        86.             <props>  
        87.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
        88.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
        89.                 <prop key="hibernate.show_sql">true</prop>  
        90.                 <prop key="hibernate.format_sql">true</prop>  
        91.             </props>  
        92.         </property>  
        93.     </bean>  
        94. 3.配置struts2.xml中需要的内容  
        95. (1)配置基本的struts2.xml   
        96.  <!DOCTYPE struts PUBLIC  
        97.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
        98.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
        99. <struts>  
        100.     <!--使用spring创建管理struts2的action操作 -->  
        101.     <constant name="struts.objectFactory" value="spring"/>  
        102.     <include file="struts-admin.xml"></include>  
        103. </struts>  
        104. 注:其中include是引入的文件,一下的语句是非常重要的:  
        105. <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键  
        106. (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现  
        107.   <!DOCTYPE struts PUBLIC  
        108.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
        109.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
        110. <struts>  
        111.     <package name="admin" extends="struts-default" namespace="/csdn">  
        112.         <!-- class的名称  等于spring中action配置文件中的id名称 -->  
        113.         <action name="loginAdmin" class="adminAction" method="login">  
        114.             <result name="success">../index.jsp</result>  
        115.         </action>  
        116.     </package>  
        117. </struts>  
        118. 4.  Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了  
        119. 5.搭建层之间的关系  
        120. (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例  
        121. 属性为id和name和pass  
        122. 封装的实体类为:  
        123. package cn.csdn.hr.s2sh.domain;  
        124. import java.io.Serializable;  
        125. public class Admin implements Serializable {  
        126.     private static final long serialVersionUID = 1L;  
        127.     private Integer id;  
        128.     private String name;  
        129.     private String pass;  
        130.   
        131.     public Admin() {  
        132.         super();  
        133.         // TODO Auto-generated constructor stub  
        134.     }  
        135.   
        136.     public Admin(Integer id, String name, String pass) {  
        137.         super();  
        138.         this.id = id;  
        139.         this.name = name;  
        140.         this.pass = pass;  
        141.     }  
        142.   
        143.     public Integer getId() {  
        144.         return id;  
        145.     }  
        146.   
        147.     public void setId(Integer id) {  
        148.         this.id = id;  
        149.     }  
        150.   
        151.     public String getName() {  
        152.         return name;  
        153.     }  
        154.   
        155.     public void setName(String name) {  
        156.         this.name = name;  
        157.     }  
        158.   
        159.     public String getPass() {  
        160.         return pass;  
        161.     }  
        162.   
        163.     public void setPass(String pass) {  
        164.         this.pass = pass;  
        165.     }  
        166.   
        167.     @Override  
        168.     public String toString() {  
        169.         return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";  
        170.     }  
        171. }  
        172. (2)在同一个包下的实体类的映射文件  
        173. <?xml version="1.0" encoding="UTF-8"?>  
        174. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        175.                                    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
        176. <hibernate-mapping package="cn.csdn.hr.s2sh.domain">  
        177.     <class name="Admin" table="admin">  
        178.         <id name="id" column="id">  
        179.             <generator class="native"></generator>  
        180.         </id>  
        181.         <property name="name" column="name"></property>  
        182.         <property name="pass" column="pass"></property>  
        183.     </class>  
        184. </hibernate-mapping>  
        185.   
        186. (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl  
        187. 下面是创建的接口 AdminDao.java  
        188. package cn.csdn.hr.s2sh.dao;  
        189.   
        190. import java.util.List;  
        191.   
        192. import cn.csdn.hr.s2sh.domain.Admin;  
        193.   
        194. public interface AdminDao {  
        195.     public Admin login(final String name,final String pass);  
        196. }  
        197.   
        198. 创建的类为AdminDaoImpl.java  
        199. package cn.csdn.hr.s2sh.dao;  
        200. import java.sql.SQLException;  
        201. import java.util.List;  
        202. import org.hibernate.HibernateException;  
        203. import org.hibernate.Session;  
        204. import org.springframework.orm.hibernate3.HibernateCallback;  
        205. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
        206.   
        207. import cn.csdn.hr.s2sh.domain.Admin;  
        208.   
        209. @SuppressWarnings("unchecked")  
        210. public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {  
        211.     public Admin login(final String name, final String pass) {  
        212.   
        213.         return (Admin) this.getHibernateTemplate().execute(  
        214.                 new HibernateCallback() {  
        215.   
        216.                     public Object doInHibernate(Session session)  
        217.                             throws HibernateException, SQLException {  
        218.                         // TODO Auto-generated method stub  
        219.   
        220.                         Object obj = session  
        221.                                 .createQuery(  
        222.                                         "from Admin where name=:name and pass=:pass")  
        223.                                 .setString("name", name)  
        224.                                 .setString("pass", pass).uniqueResult();  
        225.                         return obj;  
        226.                     }  
        227.                 });  
        228.     }  
        229. }  
        230.   
        231. (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service  
        232. 创建的AdminService.java接口  
        233. package cn.csdn.hr.s2sh.service;  
        234.   
        235. import cn.csdn.hr.s2sh.dao.AdminDao;  
        236.   
        237.   
        238. public interface AdminService extends AdminDao{  
        239.   
        240. }  
        241. 创建的AdminServiceImpl.java  
        242. package cn.csdn.hr.s2sh.service;  
        243. import java.util.List;  
        244. import org.springframework.transaction.TransactionStatus;  
        245. import org.springframework.transaction.support.TransactionCallback;  
        246. import org.springframework.transaction.support.TransactionTemplate;  
        247. import cn.csdn.hr.s2sh.dao.AdminDao;  
        248. import cn.csdn.hr.s2sh.domain.Admin;  
        249. public class AdminServiceImpl implements AdminService {  
        250.   
        251.     private AdminDao adminDao;  
        252.     public void setAdminDao(AdminDao adminDao) {  
        253.         this.adminDao = adminDao;  
        254.     }  
        255.     public Admin login(String name, String pass) {  
        256.         // TODO Auto-generated method stub  
        257.         return adminDao.login(name, pass);  
        258.     }  
        259. }  
        260.   
        261.   
        262. 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中  
        263. <?xml version="1.0" encoding="UTF-8"?>  
        264. <beans xmlns="http://www.springframework.org/schema/beans"  
        265.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
        266.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        267.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
        268.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
        269.            http://www.springframework.org/schema/aop  
        270.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
        271.            http://www.springframework.org/schema/context  
        272.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
        273.            http://www.springframework.org/schema/tx  
        274.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
        275.   
        276.     <!-- 创建struts2中的action -->  
        277.     <!-- 配置admin的action -->  
        278.     <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"  
        279.         scope="prototype">  
        280.         <property name="adminService" ref="adminServiceImpl"></property>  
        281.     </bean>  
        282. </beans>  
        283.   
        284. 而ref的adminServiceImpl是在bean-service.xml中  
        285. <?xml version="1.0" encoding="UTF-8"?>  
        286. <beans xmlns="http://www.springframework.org/schema/beans"  
        287.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
        288.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        289.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
        290.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
        291.            http://www.springframework.org/schema/aop  
        292.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
        293.            http://www.springframework.org/schema/context  
        294.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
        295.            http://www.springframework.org/schema/tx  
        296.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
        297.   
        298.     <!-- admin的业务操作 -->  
        299.     <bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl">  
        300.         <property name="adminDao" ref="adminDaoImpl"></property>  
        301.     </bean>  
        302. </beans>  
        303.   
        304. 上面的ref是adminDaoImpl,在beans-dao.xml  
        305. <?xml version="1.0" encoding="UTF-8"?>  
        306. <beans xmlns="http://www.springframework.org/schema/beans"  
        307.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
        308.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        309.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
        310.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
        311.            http://www.springframework.org/schema/aop  
        312.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
        313.            http://www.springframework.org/schema/context  
        314.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
        315.            http://www.springframework.org/schema/tx  
        316.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
        317.   
        318.   
        319.     <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->  
        320.     <bean id="hibernateDaoSupport"  
        321.         class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"  
        322.         abstract="true">  
        323.         <property name="sessionFactory" ref="sessionFactory"></property>  
        324.     </bean>  
        325.     <!-- admin的dao对象 -->  
        326.     <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"  
        327.         parent="hibernateDaoSupport" />  
        328. </beans>  
        329.   
        330. 然后我们把上面的三个beans.xml导入到applicationContext.xml中  
        331. <!-- 导入其他的配置文件 -->  
        332. <import resource="beans-dao.xml" />  
        333. <import resource="beans-service.xml" />  
        334. <import resource="beans-action.xml" />  
        335.   
        336. 7.配置web.xml  
        337. <?xml version="1.0" encoding="UTF-8"?>  
        338. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        339.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
        340.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
        341.     id="WebApp_ID" version="2.5">  
        342.     <display-name>s2sh</display-name>  
        343.     <welcome-file-list>  
        344.         <welcome-file>index.html</welcome-file>  
        345.         <welcome-file>index.htm</welcome-file>  
        346.         <welcome-file>index.jsp</welcome-file>  
        347.         <welcome-file>default.html</welcome-file>  
        348.         <welcome-file>default.htm</welcome-file>  
        349.         <welcome-file>default.jsp</welcome-file>  
        350.     </welcome-file-list>  
        351.   
        352.     <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->  
        353.     <context-param>  
        354.         <param-name>contextConfigLocation</param-name>  
        355.         <param-value>classpath:applic*.xml</param-value>  
        356.     </context-param>  
        357.     <!-- 对Spring容器进行实例化 -->  
        358.     <listener>  
        359.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
        360.     </listener>  
        361.   
        362.     <!-- struts2 的配置 -->  
        363.     <filter>  
        364.         <filter-name>struts2</filter-name>  
        365.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
        366.     </filter>  
        367.   
        368.     <filter-mapping>  
        369.         <filter-name>struts2</filter-name>  
        370.         <url-pattern>/*</url-pattern>  
        371.     </filter-mapping>  
        372.   
        373.     <!-- 配置 使用spring解决hibernate因session关闭导致的延迟加载例外问题 -->  
        374.     <filter>  
        375.         <filter-name>OpenSessionInViewFilter</filter-name>  
        376.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
        377.         <init-param>  
        378.             <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 -->  
        379.             <param-name>sessionFactoryBeanName</param-name>  
        380.             <param-value>sessionFactory</param-value>  
        381.         </init-param>  
        382.     </filter>  
        383.     <filter-mapping>  
        384.         <filter-name>OpenSessionInViewFilter</filter-name>  
        385.         <url-pattern>/*</url-pattern>  
        386.     </filter-mapping>  
        387.   
        388.     <!-- 使用spring解决struts2的中文乱码的问题 -->  
        389.     <filter>  
        390.         <filter-name>encoding</filter-name>     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        391.         <init-param>  
        392.             <param-name>encoding</param-name>  
        393.             <param-value>UTF-8</param-value>  
        394.         </init-param>  
        395.     </filter>  
        396.     <filter-mapping>  
        397.         <filter-name>encoding</filter-name>  
        398.         <url-pattern>/*</url-pattern>  
        399.     </filter-mapping>  
        400. </web-app>  
        401.   
        402. 8.我们来做一个简单的登入,使用的jsp页面为:  
        403. <body>  
        404.     <div>  
        405.         <form action="/s2sh/csdn/loginAdmin.action" method="get">  
        406.             用户名:<input type="text" name="admin.name"/><br/>  
        407.             密  码:<input type="password" name="admin.pass"/><br/>  
        408.             <input type="submit" value="登入"/>  
        409.             <input type="reset" value="重置"/>  
        410.         </form>  
        411.     </div>  
        412.     <div>  
        413.         用户名为:${admin.name}  
        414.     </div>  
        415. </body>  
        416.   
        417. 访问到的action的处理为:  
        418. package cn.csdn.hr.s2sh.action;  
        419. import cn.csdn.hr.s2sh.domain.Admin;  
        420. import cn.csdn.hr.s2sh.service.AdminService;  
        421. import com.opensymphony.xwork2.ActionSupport;  
        422. public class AdminAction extends ActionSupport {  
        423.     private static final long serialVersionUID = 1L;  
        424.   
        425.     private AdminService adminService;  
        426.   
        427.     private Admin admin;  
        428.   
        429.     public Admin getAdmin() {  
        430.         return admin;  
        431.     }  
        432.   
        433.     public void setAdmin(Admin admin) {  
        434.         this.admin = admin;  
        435.     }  
        436.   
        437.     // 注入  
        438.     public void setAdminService(AdminService adminService) {  
        439.         this.adminService = adminService;  
        440.     }  
        441.   
        442.     public String login() {  
        443.         System.out.println("用户名:" + admin.getName());  
        444.         admin = adminService.login(admin.getName(), admin.getPass());  
        445.         return SUCCESS;  
        446.     }  
        447. }  

            1. Ssh整合开发介绍和简单的登入案例实现  
            2. 一  介绍:  
            3. Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2  
            4. 二  搭建环境  
            5. 1.  首先要先引入struts2和sping和hibernate所需要的包  
            6. 1)struts2的包为:  
            7. struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar  
            8. struts-2.3.1.2\lib\ognl-3.0.4.jar  
            9. struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar  
            10. struts-2.3.1.2\lib\ freemarker-2.3.18.jar  
            11. struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar  
            12. struts-2.3.1.2\lib\ commons-io-2.0.1.jar  
            13. struts-2.3.1.2\lib\commons-lang-2.5.jar  
            14. struts-2.3.1.2\lib\commons-logging-1.1.1.jar  
            15. struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar  
            16. struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar  
            17. (2)引入hibernate的包  
            18. hibernate-distribution-3.6.8.Final\lib\required\*.jar  所有的jar包  
            19. hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar  
            20. hibernate-distribution-3.6.8.Final\ hibernate3.jar  
            21. (3)spring的包为:  
            22.     spring-framework-2.5.6\dist\ spring.jar  
            23.     spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar  
            24.     spring-framework-2.5.6\lib\aspectj\*.jar  
            25.     spring-framework-2.5.6\lib\j2ee\common-annotations.jar  
            26.     spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar  
            27.     spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar  
            28. spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar    
            29. spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar  
            30. 2.  配置spring下所需要的文件  
            31. 1)首先配置spring所需要的xml文件  
            32.     我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:  
            33. <?xml version="1.0" encoding="UTF-8"?>  
            34. <beans xmlns="http://www.springframework.org/schema/beans"  
            35.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
            36.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            37.     xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            38.            http://www.springframework.org/schema/aop  
            39.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            40.            http://www.springframework.org/schema/context  
            41.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
            42.            http://www.springframework.org/schema/tx  
            43.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
            44. </beans>  
            45. 2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池  
            46.     <!-- 数据库的连接池 -->  
            47.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
            48.         destroy-method="close">  
            49.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
            50.         <property name="jdbcUrl"  
            51.     value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />  
            52.         <property name="user" value="root" />  
            53.         <property name="password" value="1234" />  
            54.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
            55.         <property name="initialPoolSize" value="1" />  
            56.         <!--连接池中保留的最小连接数。 -->  
            57.         <property name="minPoolSize" value="1" />  
            58.         <!--连接池中保留的最大连接数。Default: 15 -->  
            59.         <property name="maxPoolSize" value="300" />  
            60.         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
            61.         <property name="maxIdleTime" value="60" />  
            62.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
            63.         <property name="acquireIncrement" value="5" />  
            64.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->  
            65.         <property name="idleConnectionTestPeriod" value="60" />  
            66.     </bean>  
            67.   
            68. 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:  
            69. <!-- 读取外部的文件 -->  
            70. <context:property-placeholder location="jdbc.properties" />  
            71.   
            72. (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置  
            73.     <!-- sessionFactory工厂 -->  
            74.     <bean id="sessionFactory"  
            75.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            76.         <!-- 注入数据源 -->  
            77.         <property name="dataSource" ref="dataSource"></property>  
            78.         <!-- hibernate映射文件的引入 -->  
            79.         <property name="mappingResources">  
            80.             <list>  
            81.                 <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>  
            82.             </list>  
            83.         </property>  
            84.         <!-- 配置hibernate属性的设置 -->  
            85.         <property name="hibernateProperties">  
            86.             <props>  
            87.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
            88.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
            89.                 <prop key="hibernate.show_sql">true</prop>  
            90.                 <prop key="hibernate.format_sql">true</prop>  
            91.             </props>  
            92.         </property>  
            93.     </bean>  
            94. 3.配置struts2.xml中需要的内容  
            95. (1)配置基本的struts2.xml   
            96.  <!DOCTYPE struts PUBLIC  
            97.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
            98.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
            99. <struts>  
            100.     <!--使用spring创建管理struts2的action操作 -->  
            101.     <constant name="struts.objectFactory" value="spring"/>  
            102.     <include file="struts-admin.xml"></include>  
            103. </struts>  
            104. 注:其中include是引入的文件,一下的语句是非常重要的:  
            105. <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键  
            106. (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现  
            107.   <!DOCTYPE struts PUBLIC  
            108.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
            109.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
            110. <struts>  
            111.     <package name="admin" extends="struts-default" namespace="/csdn">  
            112.         <!-- class的名称  等于spring中action配置文件中的id名称 -->  
            113.         <action name="loginAdmin" class="adminAction" method="login">  
            114.             <result name="success">../index.jsp</result>  
            115.         </action>  
            116.     </package>  
            117. </struts>  
            118. 4.  Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了  
            119. 5.搭建层之间的关系  
            120. (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例  
            121. 属性为id和name和pass  
            122. 封装的实体类为:  
            123. package cn.csdn.hr.s2sh.domain;  
            124. import java.io.Serializable;  
            125. public class Admin implements Serializable {  
            126.     private static final long serialVersionUID = 1L;  
            127.     private Integer id;  
            128.     private String name;  
            129.     private String pass;  
            130.   
            131.     public Admin() {  
            132.         super();  
            133.         // TODO Auto-generated constructor stub  
            134.     }  
            135.   
            136.     public Admin(Integer id, String name, String pass) {  
            137.         super();  
            138.         this.id = id;  
            139.         this.name = name;  
            140.         this.pass = pass;  
            141.     }  
            142.   
            143.     public Integer getId() {  
            144.         return id;  
            145.     }  
            146.   
            147.     public void setId(Integer id) {  
            148.         this.id = id;  
            149.     }  
            150.   
            151.     public String getName() {  
            152.         return name;  
            153.     }  
            154.   
            155.     public void setName(String name) {  
            156.         this.name = name;  
            157.     }  
            158.   
            159.     public String getPass() {  
            160.         return pass;  
            161.     }  
            162.   
            163.     public void setPass(String pass) {  
            164.         this.pass = pass;  
            165.     }  
            166.   
            167.     @Override  
            168.     public String toString() {  
            169.         return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";  
            170.     }  
            171. }  
            172. (2)在同一个包下的实体类的映射文件  
            173. <?xml version="1.0" encoding="UTF-8"?>  
            174. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
            175.                                    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
            176. <hibernate-mapping package="cn.csdn.hr.s2sh.domain">  
            177.     <class name="Admin" table="admin">  
            178.         <id name="id" column="id">  
            179.             <generator class="native"></generator>  
            180.         </id>  
            181.         <property name="name" column="name"></property>  
            182.         <property name="pass" column="pass"></property>  
            183.     </class>  
            184. </hibernate-mapping>  
            185.   
            186. (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl  
            187. 下面是创建的接口 AdminDao.java  
            188. package cn.csdn.hr.s2sh.dao;  
            189.   
            190. import java.util.List;  
            191.   
            192. import cn.csdn.hr.s2sh.domain.Admin;  
            193.   
            194. public interface AdminDao {  
            195.     public Admin login(final String name,final String pass);  
            196. }  
            197.   
            198. 创建的类为AdminDaoImpl.java  
            199. package cn.csdn.hr.s2sh.dao;  
            200. import java.sql.SQLException;  
            201. import java.util.List;  
            202. import org.hibernate.HibernateException;  
            203. import org.hibernate.Session;  
            204. import org.springframework.orm.hibernate3.HibernateCallback;  
            205. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
            206.   
            207. import cn.csdn.hr.s2sh.domain.Admin;  
            208.   
            209. @SuppressWarnings("unchecked")  
            210. public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {  
            211.     public Admin login(final String name, final String pass) {  
            212.   
            213.         return (Admin) this.getHibernateTemplate().execute(  
            214.                 new HibernateCallback() {  
            215.   
            216.                     public Object doInHibernate(Session session)  
            217.                             throws HibernateException, SQLException {  
            218.                         // TODO Auto-generated method stub  
            219.   
            220.                         Object obj = session  
            221.                                 .createQuery(  
            222.                                         "from Admin where name=:name and pass=:pass")  
            223.                                 .setString("name", name)  
            224.                                 .setString("pass", pass).uniqueResult();  
            225.                         return obj;  
            226.                     }  
            227.                 });  
            228.     }  
            229. }  
            230.   
            231. (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service  
            232. 创建的AdminService.java接口  
            233. package cn.csdn.hr.s2sh.service;  
            234.   
            235. import cn.csdn.hr.s2sh.dao.AdminDao;  
            236.   
            237.   
            238. public interface AdminService extends AdminDao{  
            239.   
            240. }  
            241. 创建的AdminServiceImpl.java  
            242. package cn.csdn.hr.s2sh.service;  
            243. import java.util.List;  
            244. import org.springframework.transaction.TransactionStatus;  
            245. import org.springframework.transaction.support.TransactionCallback;  
            246. import org.springframework.transaction.support.TransactionTemplate;  
            247. import cn.csdn.hr.s2sh.dao.AdminDao;  
            248. import cn.csdn.hr.s2sh.domain.Admin;  
            249. public class AdminServiceImpl implements AdminService {  
            250.   
            251.     private AdminDao adminDao;  
            252.     public void setAdminDao(AdminDao adminDao) {  
            253.         this.adminDao = adminDao;  
            254.     }  
            255.     public Admin login(String name, String pass) {  
            256.         // TODO Auto-generated method stub  
            257.         return adminDao.login(name, pass);  
            258.     }  
            259. }  
            260.   
            261.   
            262. 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中  
            263. <?xml version="1.0" encoding="UTF-8"?>  
            264. <beans xmlns="http://www.springframework.org/schema/beans"  
            265.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
            266.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            267.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
            268.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            269.            http://www.springframework.org/schema/aop  
            270.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            271.            http://www.springframework.org/schema/context  
            272.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
            273.            http://www.springframework.org/schema/tx  
            274.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
            275.   
            276.     <!-- 创建struts2中的action -->  
            277.     <!-- 配置admin的action -->  
            278.     <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"  
            279.         scope="prototype">  
            280.         <property name="adminService" ref="adminServiceImpl"></property>  
            281.     </bean>  
            282. </beans>  
            283.   
            284. 而ref的adminServiceImpl是在bean-service.xml中  
            285. <?xml version="1.0" encoding="UTF-8"?>  
            286. <beans xmlns="http://www.springframework.org/schema/beans"  
            287.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
            288.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            289.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
            290.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            291.            http://www.springframework.org/schema/aop  
            292.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            293.            http://www.springframework.org/schema/context  
            294.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
            295.            http://www.springframework.org/schema/tx  
            296.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
            297.   
            298.     <!-- admin的业务操作 -->  
            299.     <bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl">  
            300.         <property name="adminDao" ref="adminDaoImpl"></property>  
            301.     </bean>  
            302. </beans>  
            303.   
            304. 上面的ref是adminDaoImpl,在beans-dao.xml  
            305. <?xml version="1.0" encoding="UTF-8"?>  
            306. <beans xmlns="http://www.springframework.org/schema/beans"  
            307.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
            308.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            309.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
            310.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            311.            http://www.springframework.org/schema/aop  
            312.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            313.            http://www.springframework.org/schema/context  
            314.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
            315.            http://www.springframework.org/schema/tx  
            316.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
            317.   
            318.   
            319.     <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->  
            320.     <bean id="hibernateDaoSupport"  
            321.         class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"  
            322.         abstract="true">  
            323.         <property name="sessionFactory" ref="sessionFactory"></property>  
            324.     </bean>  
            325.     <!-- admin的dao对象 -->  
            326.     <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"  
            327.         parent="hibernateDaoSupport" />  
            328. </beans>  
            329.   
            330. 然后我们把上面的三个beans.xml导入到applicationContext.xml中  
            331. <!-- 导入其他的配置文件 -->  
            332. <import resource="beans-dao.xml" />  
            333. <import resource="beans-service.xml" />  
            334. <import resource="beans-action.xml" />  
            335.   
            336. 7.配置web.xml  
            337. <?xml version="1.0" encoding="UTF-8"?>  
            338. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            339.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
            340.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
            341.     id="WebApp_ID" version="2.5">  
            342.     <display-name>s2sh</display-name>  
            343.     <welcome-file-list>  
            344.         <welcome-file>index.html</welcome-file>  
            345.         <welcome-file>index.htm</welcome-file>  
            346.         <welcome-file>index.jsp</welcome-file>  
            347.         <welcome-file>default.html</welcome-file>  
            348.         <welcome-file>default.htm</welcome-file>  
            349.         <welcome-file>default.jsp</welcome-file>  
            350.     </welcome-file-list>  
            351.   
            352.     <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->  
            353.     <context-param>  
            354.         <param-name>contextConfigLocation</param-name>  
            355.         <param-value>classpath:applic*.xml</param-value>  
            356.     </context-param>  
            357.     <!-- 对Spring容器进行实例化 -->  
            358.     <listener>  
            359.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
            360.     </listener>  
            361.   
            362.     <!-- struts2 的配置 -->  
            363.     <filter>  
            364.         <filter-name>struts2</filter-name>  
            365.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
            366.     </filter>  
            367.   
            368.     <filter-mapping>  
            369.         <filter-name>struts2</filter-name>  
            370.         <url-pattern>/*</url-pattern>  
            371.     </filter-mapping>  
            372.   
            373.     <!-- 配置 使用spring解决hibernate因session关闭导致的延迟加载例外问题 -->  
            374.     <filter>  
            375.         <filter-name>OpenSessionInViewFilter</filter-name>  
            376.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
            377.         <init-param>  
            378.             <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 -->  
            379.             <param-name>sessionFactoryBeanName</param-name>  
            380.             <param-value>sessionFactory</param-value>  
            381.         </init-param>  
            382.     </filter>  
            383.     <filter-mapping>  
            384.         <filter-name>OpenSessionInViewFilter</filter-name>  
            385.         <url-pattern>/*</url-pattern>  
            386.     </filter-mapping>  
            387.   
            388.     <!-- 使用spring解决struts2的中文乱码的问题 -->  
            389.     <filter>  
            390.         <filter-name>encoding</filter-name>     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            391.         <init-param>  
            392.             <param-name>encoding</param-name>  
            393.             <param-value>UTF-8</param-value>  
            394.         </init-param>  
            395.     </filter>  
            396.     <filter-mapping>  
            397.         <filter-name>encoding</filter-name>  
            398.         <url-pattern>/*</url-pattern>  
            399.     </filter-mapping>  
            400. </web-app>  
            401.   
            402. 8.我们来做一个简单的登入,使用的jsp页面为:  
            403. <body>  
            404.     <div>  
            405.         <form action="/s2sh/csdn/loginAdmin.action" method="get">  
            406.             用户名:<input type="text" name="admin.name"/><br/>  
            407.             密  码:<input type="password" name="admin.pass"/><br/>  
            408.             <input type="submit" value="登入"/>  
            409.             <input type="reset" value="重置"/>  
            410.         </form>  
            411.     </div>  
            412.     <div>  
            413.         用户名为:${admin.name}  
            414.     </div>  
            415. </body>  
            416.   
            417. 访问到的action的处理为:  
            418. package cn.csdn.hr.s2sh.action;  
            419. import cn.csdn.hr.s2sh.domain.Admin;  
            420. import cn.csdn.hr.s2sh.service.AdminService;  
            421. import com.opensymphony.xwork2.ActionSupport;  
            422. public class AdminAction extends ActionSupport {  
            423.     private static final long serialVersionUID = 1L;  
            424.   
            425.     private AdminService adminService;  
            426.   
            427.     private Admin admin;  
            428.   
            429.     public Admin getAdmin() {  
            430.         return admin;  
            431.     }  
            432.   
            433.     public void setAdmin(Admin admin) {  
            434.         this.admin = admin;  
            435.     }  
            436.   
            437.     // 注入  
            438.     public void setAdminService(AdminService adminService) {  
            439.         this.adminService = adminService;  
            440.     }  
            441.   
            442.     public String login() {  
            443.         System.out.println("用户名:" + admin.getName());  
            444.         admin = adminService.login(admin.getName(), admin.getPass());  
            445.         return SUCCESS;  
            446.     }  
            447. }  

                1. Ssh整合开发介绍和简单的登入案例实现  
                2. 一  介绍:  
                3. Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2  
                4. 二  搭建环境  
                5. 1.  首先要先引入struts2和sping和hibernate所需要的包  
                6. 1)struts2的包为:  
                7. struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar  
                8. struts-2.3.1.2\lib\ognl-3.0.4.jar  
                9. struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar  
                10. struts-2.3.1.2\lib\ freemarker-2.3.18.jar  
                11. struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar  
                12. struts-2.3.1.2\lib\ commons-io-2.0.1.jar  
                13. struts-2.3.1.2\lib\commons-lang-2.5.jar  
                14. struts-2.3.1.2\lib\commons-logging-1.1.1.jar  
                15. struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar  
                16. struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar  
                17. (2)引入hibernate的包  
                18. hibernate-distribution-3.6.8.Final\lib\required\*.jar  所有的jar包  
                19. hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar  
                20. hibernate-distribution-3.6.8.Final\ hibernate3.jar  
                21. (3)spring的包为:  
                22.     spring-framework-2.5.6\dist\ spring.jar  
                23.     spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar  
                24.     spring-framework-2.5.6\lib\aspectj\*.jar  
                25.     spring-framework-2.5.6\lib\j2ee\common-annotations.jar  
                26.     spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar  
                27.     spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar  
                28. spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar    
                29. spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar  
                30. 2.  配置spring下所需要的文件  
                31. 1)首先配置spring所需要的xml文件  
                32.     我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:  
                33. <?xml version="1.0" encoding="UTF-8"?>  
                34. <beans xmlns="http://www.springframework.org/schema/beans"  
                35.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
                36.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                37.     xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
                38.            http://www.springframework.org/schema/aop  
                39.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
                40.            http://www.springframework.org/schema/context  
                41.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
                42.            http://www.springframework.org/schema/tx  
                43.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
                44. </beans>  
                45. 2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池  
                46.     <!-- 数据库的连接池 -->  
                47.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
                48.         destroy-method="close">  
                49.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
                50.         <property name="jdbcUrl"  
                51.     value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />  
                52.         <property name="user" value="root" />  
                53.         <property name="password" value="1234" />  
                54.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
                55.         <property name="initialPoolSize" value="1" />  
                56.         <!--连接池中保留的最小连接数。 -->  
                57.         <property name="minPoolSize" value="1" />  
                58.         <!--连接池中保留的最大连接数。Default: 15 -->  
                59.         <property name="maxPoolSize" value="300" />  
                60.         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
                61.         <property name="maxIdleTime" value="60" />  
                62.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
                63.         <property name="acquireIncrement" value="5" />  
                64.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->  
                65.         <property name="idleConnectionTestPeriod" value="60" />  
                66.     </bean>  
                67.   
                68. 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:  
                69. <!-- 读取外部的文件 -->  
                70. <context:property-placeholder location="jdbc.properties" />  
                71.   
                72. (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置  
                73.     <!-- sessionFactory工厂 -->  
                74.     <bean id="sessionFactory"  
                75.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
                76.         <!-- 注入数据源 -->  
                77.         <property name="dataSource" ref="dataSource"></property>  
                78.         <!-- hibernate映射文件的引入 -->  
                79.         <property name="mappingResources">  
                80.             <list>  
                81.                 <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>  
                82.             </list>  
                83.         </property>  
                84.         <!-- 配置hibernate属性的设置 -->  
                85.         <property name="hibernateProperties">  
                86.             <props>  
                87.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
                88.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
                89.                 <prop key="hibernate.show_sql">true</prop>  
                90.                 <prop key="hibernate.format_sql">true</prop>  
                91.             </props>  
                92.         </property>  
                93.     </bean>  
                94. 3.配置struts2.xml中需要的内容  
                95. (1)配置基本的struts2.xml   
                96.  <!DOCTYPE struts PUBLIC  
                97.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
                98.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
                99. <struts>  
                100.     <!--使用spring创建管理struts2的action操作 -->  
                101.     <constant name="struts.objectFactory" value="spring"/>  
                102.     <include file="struts-admin.xml"></include>  
                103. </struts>  
                104. 注:其中include是引入的文件,一下的语句是非常重要的:  
                105. <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键  
                106. (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现  
                107.   <!DOCTYPE struts PUBLIC  
                108.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
                109.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
                110. <struts>  
                111.     <package name="admin" extends="struts-default" namespace="/csdn">  
                112.         <!-- class的名称  等于spring中action配置文件中的id名称 -->  
                113.         <action name="loginAdmin" class="adminAction" method="login">  
                114.             <result name="success">../index.jsp</result>  
                115.         </action>  
                116.     </package>  
                117. </struts>  
                118. 4.  Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了  
                119. 5.搭建层之间的关系  
                120. (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例  
                121. 属性为id和name和pass  
                122. 封装的实体类为:  
                123. package cn.csdn.hr.s2sh.domain;  
                124. import java.io.Serializable;  
                125. public class Admin implements Serializable {  
                126.     private static final long serialVersionUID = 1L;  
                127.     private Integer id;  
                128.     private String name;  
                129.     private String pass;  
                130.   
                131.     public Admin() {  
                132.         super();  
                133.         // TODO Auto-generated constructor stub  
                134.     }  
                135.   
                136.     public Admin(Integer id, String name, String pass) {  
                137.         super();  
                138.         this.id = id;  
                139.         this.name = name;  
                140.         this.pass = pass;  
                141.     }  
                142.   
                143.     public Integer getId() {  
                144.         return id;  
                145.     }  
                146.   
                147.     public void setId(Integer id) {  
                148.         this.id = id;  
                149.     }  
                150.   
                151.     public String getName() {  
                152.         return name;  
                153.     }  
                154.   
                155.     public void setName(String name) {  
                156.         this.name = name;  
                157.     }  
                158.   
                159.     public String getPass() {  
                160.         return pass;  
                161.     }  
                162.   
                163.     public void setPass(String pass) {  
                164.         this.pass = pass;  
                165.     }  
                166.   
                167.     @Override  
                168.     public String toString() {  
                169.         return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";  
                170.     }  
                171. }  
                172. (2)在同一个包下的实体类的映射文件  
                173. <?xml version="1.0" encoding="UTF-8"?>  
                174. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                175.                                    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
                176. <hibernate-mapping package="cn.csdn.hr.s2sh.domain">  
                177.     <class name="Admin" table="admin">  
                178.         <id name="id" column="id">  
                179.             <generator class="native"></generator>  
                180.         </id>  
                181.         <property name="name" column="name"></property>  
                182.         <property name="pass" column="pass"></property>  
                183.     </class>  
                184. </hibernate-mapping>  
                185.   
                186. (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl  
                187. 下面是创建的接口 AdminDao.java  
                188. package cn.csdn.hr.s2sh.dao;  
                189.   
                190. import java.util.List;  
                191.   
                192. import cn.csdn.hr.s2sh.domain.Admin;  
                193.   
                194. public interface AdminDao {  
                195.     public Admin login(final String name,final String pass);  
                196. }  
                197.   
                198. 创建的类为AdminDaoImpl.java  
                199. package cn.csdn.hr.s2sh.dao;  
                200. import java.sql.SQLException;  
                201. import java.util.List;  
                202. import org.hibernate.HibernateException;  
                203. import org.hibernate.Session;  
                204. import org.springframework.orm.hibernate3.HibernateCallback;  
                205. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
                206.   
                207. import cn.csdn.hr.s2sh.domain.Admin;  
                208.   
                209. @SuppressWarnings("unchecked")  
                210. public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {  
                211.     public Admin login(final String name, final String pass) {  
                212.   
                213.         return (Admin) this.getHibernateTemplate().execute(  
                214.                 new HibernateCallback() {  
                215.   
                216.                     public Object doInHibernate(Session session)  
                217.                             throws HibernateException, SQLException {  
                218.                         // TODO Auto-generated method stub  
                219.   
                220.                         Object obj = session  
                221.                                 .createQuery(  
                222.                                         "from Admin where name=:name and pass=:pass")  
                223.                                 .setString("name", name)  
                224.                                 .setString("pass", pass).uniqueResult();  
                225.                         return obj;  
                226.                     }  
                227.                 });  
                228.     }  
                229. }  
                230.   
                231. (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service  
                232. 创建的AdminService.java接口  
                233. package cn.csdn.hr.s2sh.service;  
                234.   
                235. import cn.csdn.hr.s2sh.dao.AdminDao;  
                236.   
                237.   
                238. public interface AdminService extends AdminDao{  
                239.   
                240. }  
                241. 创建的AdminServiceImpl.java  
                242. package cn.csdn.hr.s2sh.service;  
                243. import java.util.List;  
                244. import org.springframework.transaction.TransactionStatus;  
                245. import org.springframework.transaction.support.TransactionCallback;  
                246. import org.springframework.transaction.support.TransactionTemplate;  
                247. import cn.csdn.hr.s2sh.dao.AdminDao;  
                248. import cn.csdn.hr.s2sh.domain.Admin;  
                249. public class AdminServiceImpl implements AdminService {  
                250.   
                251.     private AdminDao adminDao;  
                252.     public void setAdminDao(AdminDao adminDao) {  
                253.         this.adminDao = adminDao;  
                254.     }  
                255.     public Admin login(String name, String pass) {  
                256.         // TODO Auto-generated method stub  
                257.         return adminDao.login(name, pass);  
                258.     }  
                259. }  
                260.   
                261.   
                262. 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中  
                263. <?xml version="1.0" encoding="UTF-8"?>  
                264. <beans xmlns="http://www.springframework.org/schema/beans"  
                265.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
                266.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                267.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
                268.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
                269.            http://www.springframework.org/schema/aop  
                270.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
                271.            http://www.springframework.org/schema/context  
                272.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
                273.            http://www.springframework.org/schema/tx  
                274.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
                275.   
                276.     <!-- 创建struts2中的action -->  
                277.     <!-- 配置admin的action -->  
                278.     <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"  
                279.         scope="prototype">  
                280.         <property name="adminService" ref="adminServiceImpl"></property>  
                281.     </bean>  
                282. </beans>  
                283.   
                284. 而ref的adminServiceImpl是在bean-service.xml中  
                285. <?xml version="1.0" encoding="UTF-8"?>  
                286. <beans xmlns="http://www.springframework.org/schema/beans"  
                287.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
                288.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                289.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
                290.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
                291.            http://www.springframework.org/schema/aop  
                292.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
                293.            http://www.springframework.org/schema/context  
                294.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
                295.            http://www.springframework.org/schema/tx  
                296.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
                297.   
                298.     <!-- admin的业务操作 -->  
                299.     <bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl">  
                300.         <property name="adminDao" ref="adminDaoImpl"></property>  
                301.     </bean>  
                302. </beans>  
                303.   
                304. 上面的ref是adminDaoImpl,在beans-dao.xml  
                305. <?xml version="1.0" encoding="UTF-8"?>  
                306. <beans xmlns="http://www.springframework.org/schema/beans"  
                307.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
                308.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                309.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
                310.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
                311.            http://www.springframework.org/schema/aop  
                312.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
                313.            http://www.springframework.org/schema/context  
                314.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
                315.            http://www.springframework.org/schema/tx  
                316.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
                317.   
                318.   
                319.     <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->  
                320.     <bean id="hibernateDaoSupport"  
                321.         class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"  
                322.         abstract="true">  
                323.         <property name="sessionFactory" ref="sessionFactory"></property>  
                324.     </bean>  
                325.     <!-- admin的dao对象 -->  
                326.     <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"  
                327.         parent="hibernateDaoSupport" />  
                328. </beans>  
                329.   
                330. 然后我们把上面的三个beans.xml导入到applicationContext.xml中  
                331. <!-- 导入其他的配置文件 -->  
                332. <import resource="beans-dao.xml" />  
                333. <import resource="beans-service.xml" />  
                334. <import resource="beans-action.xml" />  
                335.   
                336. 7.配置web.xml  
                337. <?xml version="1.0" encoding="UTF-8"?>  
                338. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                339.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
                340.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
                341.     id="WebApp_ID" version="2.5">  
                342.     <display-name>s2sh</display-name>  
                343.     <welcome-file-list>  
                344.         <welcome-file>index.html</welcome-file>  
                345.         <welcome-file>index.htm</welcome-file>  
                346.         <welcome-file>index.jsp</welcome-file>  
                347.         <welcome-file>default.html</welcome-file>  
                348.         <welcome-file>default.htm</welcome-file>  
                349.         <welcome-file>default.jsp</welcome-file>  
                350.     </welcome-file-list>  
                351.   
                352.     <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->  
                353.     <context-param>  
                354.         <param-name>contextConfigLocation</param-name>  
                355.         <param-value>classpath:applic*.xml</param-value>  
                356.     </context-param>  
                357.     <!-- 对Spring容器进行实例化 -->  
                358.     <listener>  
                359.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
                360.     </listener>  
                361.   
                362.     <!-- struts2 的配置 -->  
                363.     <filter>  
                364.         <filter-name>struts2</filter-name>  
                365.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
                366.     </filter>  
                367.   
                368.     <filter-mapping>  
                369.         <filter-name>struts2</filter-name>  
                370.         <url-pattern>/*</url-pattern>  
                371.     </filter-mapping>  
                372.   
                373.     <!-- 配置 使用spring解决hibernate因session关闭导致的延迟加载例外问题 -->  
                374.     <filter>  
                375.         <filter-name>OpenSessionInViewFilter</filter-name>  
                376.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
                377.         <init-param>  
                378.             <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 -->  
                379.             <param-name>sessionFactoryBeanName</param-name>  
                380.             <param-value>sessionFactory</param-value>  
                381.         </init-param>  
                382.     </filter>  
                383.     <filter-mapping>  
                384.         <filter-name>OpenSessionInViewFilter</filter-name>  
                385.         <url-pattern>/*</url-pattern>  
                386.     </filter-mapping>  
                387.   
                388.     <!-- 使用spring解决struts2的中文乱码的问题 -->  
                389.     <filter>  
                390.         <filter-name>encoding</filter-name>     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
                391.         <init-param>  
                392.             <param-name>encoding</param-name>  
                393.             <param-value>UTF-8</param-value>  
                394.         </init-param>  
                395.     </filter>  
                396.     <filter-mapping>  
                397.         <filter-name>encoding</filter-name>  
                398.         <url-pattern>/*</url-pattern>  
                399.     </filter-mapping>  
                400. </web-app>  
                401.   
                402. 8.我们来做一个简单的登入,使用的jsp页面为:  
                403. <body>  
                404.     <div>  
                405.         <form action="/s2sh/csdn/loginAdmin.action" method="get">  
                406.             用户名:<input type="text" name="admin.name"/><br/>  
                407.             密  码:<input type="password" name="admin.pass"/><br/>  
                408.             <input type="submit" value="登入"/>  
                409.             <input type="reset" value="重置"/>  
                410.         </form>  
                411.     </div>  
                412.     <div>  
                413.         用户名为:${admin.name}  
                414.     </div>  
                415. </body>  
                416.   
                417. 访问到的action的处理为:  
                418. package cn.csdn.hr.s2sh.action;  
                419. import cn.csdn.hr.s2sh.domain.Admin;  
                420. import cn.csdn.hr.s2sh.service.AdminService;  
                421. import com.opensymphony.xwork2.ActionSupport;  
                422. public class AdminAction extends ActionSupport {  
                423.     private static final long serialVersionUID = 1L;  
                424.   
                425.     private AdminService adminService;  
                426.   
                427.     private Admin admin;  
                428.   
                429.     public Admin getAdmin() {  
                430.         return admin;  
                431.     }  
                432.   
                433.     public void setAdmin(Admin admin) {  
                434.         this.admin = admin;  
                435.     }  
                436.   
                437.     // 注入  
                438.     public void setAdminService(AdminService adminService) {  
                439.         this.adminService = adminService;  
                440.     }  
                441.   
                442.     public String login() {  
                443.         System.out.println("用户名:" + admin.getName());  
                444.         admin = adminService.login(admin.getName(), admin.getPass());  
                445.         return SUCCESS;  
                446.     }  
                447. }  
        this.adminService = adminService;  
    439.     }  
    440.   
    441.     public String login() {  
    442.         System.out.println("用户名:" + admin.getName());  
    443.         admin = adminService.login(admin.getName(), admin.getPass());  
    444.         return SUCCESS;  
    445.     }  
    446. }  
posted @ 2013-08-07 08:40  奋斗?坑  阅读(329)  评论(0编辑  收藏  举报