Struts2+Sring+Hibernate簡單配置

Struts2+Spring+Hibernate是J2EE的最新流行框架。本篇是我搭建這個框架的經驗總結,有不少人搭建這個框架總會遇到 css

大大小小的問題,網上也沒有什麼行之有效的方案或成體系的介紹,因此我就決定總結一下個人搭建過程。給一些搭 html

建尚存問題的朋友提供幫助。 java

我用這個框架,實現的是基本的CRUD功能的一個僱員管理系統,原本打算豐富一下功能,可是一直沒能抽出空去搞。 mysql

目前版本暫定爲1.0,除了CRUD外還配置了表單驗證框架JSValidation。功能都能很順利的實現。 web

如今分享部分源碼,來講明一些注意事項。 spring

如下是部分搭建過程及源碼: sql

1.先組合實現Hibernate3.2+Spring2.5支持,刪除hibernate.cfg.xml文件,修改applicationContext.xml文件的內容,增長SessionFactory和dataSource的設置。 express

2.經過MyEclipse的嚮導方式,生成POJO類和對應的映射文件。 apache

3.修改applicationContext.xml文件中<property name="mappingResources">元素的內容。 session

4.編寫DAO接口和實現類。

5.修改applicationContext.xml文件,增長對Dao實現類的配置。

6.組合Struts2和Spring2.5,修改web.xml文件,增長struts2的所須要的過濾器配置。

7.增長struts2相應類庫,增長struts2與spring的配置jar包。

8.拷貝struts.xml文件到src根目錄下,再修改struts.xml文件,進行常量配置。

9.修改web.xml文件,配置Spring監聽器,和上下文變量。並增長OpenSessionInViewFilter的設置。

10.寫入action類。

11.配置struts.xml文件。

12.修改applicationContext.xml

13.編寫Jsp文件。

14.加載運行項目。

下面是關鍵文件的源碼:

struts.xml源碼:

複製代碼
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
<!--  struts2委託spring管理  -->
    
< constant  name ="struts.objectFactory"  value ="spring" />
    
<!--  /crm/emp/add.action  -->
    
< package  name ="crm_employee"  extends ="struts-default"  namespace ="/emp" >
        
< action  name ="add"  class ="addBean"  method ="add" >
            
< result > add.action </ result >
            
< result > /emp/add_suc.jsp </ result >
        
</ action >
        
< action  name ="list"  class ="listBean"  method ="list" >
            
< result > /emp/list.jsp </ result >
        
</ action >
        
< action  name ="delete"  class ="deleteBean"  method ="delete" >
            
< result > delete.action </ result >
            
< result > /emp/delete_suc.jsp </ result >
        
</ action >
        
< action  name ="update"  class ="updateBean"  method ="update" >
            
< result > update.action </ result >
            
< result > /emp/edit_suc.jsp </ result >
        
</ action >
        
< action  name ="edit"  class ="editBean"  method ="edit" >
            
< result > /emp/edit.jsp </ result >
        
</ action >
        
<!--  Add actions here  -->
    
</ package >
</ struts >
複製代碼

 

web.xml源碼:

複製代碼
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  version ="2.5"  xmlns ="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    
<!--  配置spring的監聽器  -->
    
< context-param >
        
< param-name > contextConfigLocation </ param-name >
        
< param-value > /WEB-INF/applicationContext*.xml </ param-value >
    
</ context-param >
    
<!--  開啓監聽  -->
    
< listener >
        
< listener-class >
            org.springframework.web.context.ContextLoaderListener
        
</ listener-class >
    
</ listener >
    
<!--  配置OpenSessionInViewFilter,必須在struts2監聽以前  -->
    
< filter >
        
< filter-name > lazyLoadingFilter </ filter-name >
        
< filter-class >
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        
</ filter-class >
    
</ filter >
    
<!--  設置監聽加載上下文  -->
    
< filter >
        
< filter-name > struts2 </ filter-name >
        
< filter-class >
            org.apache.struts2.dispatcher.FilterDispatcher
        
</ filter-class >
    
</ filter >
    
< filter-mapping >
    
< filter-name > lazyLoadingFilter </ filter-name >
    
< url-pattern > *.action </ url-pattern >
    
</ filter-mapping >
    
< filter-mapping >
        
< filter-name > struts2 </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >
    
< welcome-file-list >
    
< welcome-file > index.jsp </ welcome-file >
    
</ welcome-file-list >
</ web-app >
複製代碼


applicationContext.xml源碼:

複製代碼
<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>
    
<!--  配置Hibernate支持  -->
    
< bean  id ="dataSource"
        class
="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName"
            value
="com.mysql.jdbc.Driver" >
        
</ property >
        
< property  name ="url"
            value
="jdbc:mysql://localhost:3306/tables" >
        
</ property >
        
< property  name ="username"  value ="root" ></ property >
        
< property  name ="password"  value ="hicc" ></ property >
    
</ bean >
    
< bean  id ="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  bean ="dataSource"   />
        
</ property >
        
< property  name ="hibernateProperties" >
            
< props >
                
< prop  key ="hibernate.dialect" >
                    org.hibernate.dialect.MySQLDialect
                
</ prop >
                
< prop  key ="hibernate.show_sql" > true </ prop >
            
</ props >
        
</ property >
        
< property  name ="mappingResources" >
            
< list >
                
< value > com/sy/crm/model/Employee.hbm.xml </ value >
            
</ list >
        
</ property >
    
</ bean >
    
< bean  id ="employeeDao"
        class
="com.sy.crm.dao.hibernate.EmployeeDaoHibernate" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory"   />
        
</ property >
    
</ bean >
    
< bean  id ="employeeManager"
        class
="com.sy.crm.service.impl.EmployeeManagerImpl" >
        
< property  name ="employeeDao" >
            
< ref  bean ="employeeDao"   />
        
</ property >
    
</ bean >
    
    
< bean  id ="addBean"  class ="com.sy.crm.action.EmployeeAction"  scope ="prototype" >
        
< property  name ="employeeManager" >
            
< ref  bean ="employeeManager"   />
        
</ property >
    
</ bean >
    
< bean  id ="listBean"  class ="com.sy.crm.action.EmployeeAction"  scope ="prototype" >
        
< property  name ="employeeManager" >
            
< ref  bean ="employeeManager"   />
        
</ property >
    
</ bean >
    
< bean  id ="deleteBean"  class ="com.sy.crm.action.EmployeeAction"  scope ="prototype" >
        
< property  name ="employeeManager" >
            
< ref  bean ="employeeManager"   />
        
</ property >
    
</ bean >
    
< bean  id ="updateBean"  class ="com.sy.crm.action.EmployeeAction"  scope ="prototype" >
        
< property  name ="employeeManager" >
            
< ref  bean ="employeeManager"   />
        
</ property >
    
</ bean >
    
< bean  id ="editBean"  class ="com.sy.crm.action.EmployeeAction"  scope ="prototype" >
        
< property  name ="employeeManager" >
            
< ref  bean ="employeeManager"   />
        
</ property >
    
</ bean >
    
<!--  事務管理器  -->
    
< bean  id ="transactionManager"  
    class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
    
< property  name ="sessionFactory" >
    
< ref  local ="sessionFactory" />
    
</ property >
    
</ bean >
    
<!--  配置事務特性,配置add,delete,update開始的方法,事務傳播特性爲required  -->
    
< tx:advice  id ="txAdvice"  transaction-manager ="transactionManager" >
    
< tx:attributes >
    
< tx:method  name ="add*"  propagation ="REQUIRED" />
    
< tx:method  name ="delete*"  propagation ="REQUIRED" />
    
< tx:method  name ="update*"  propagation ="REQUIRED" />
    
< tx:method  name ="*"  read-only ="true" />
    
</ tx:attributes >
    
</ tx:advice >
    
<!--  配置那些類的方法進行事務管理,當前com.sy.crm.service包中的子包,
    類中全部方法須要,還須要參考tx:advice的設置 
-->
    
< aop:config >
    
< aop:pointcut  id ="allManagerMethod"  expression ="execution(*
    com.sy.crm.service.*.*(..))"
/>
    
< aop:advisor  advice-ref ="txAdvice"  pointcut-ref ="allManagerMethod" />
    
</ aop:config >
    
</ beans >
複製代碼
 
add.jsp源碼:
複製代碼
複製代碼
<% @ page language = " java "  pageEncoding = " utf-8 " %>
<% @ taglib uri = " /struts-tags "  prefix = " s "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
  
< head >
    
< title > add page </ title >
    
< script language = " JavaScript "  src = " validation-framework.js " ></ script >
    
< meta http - equiv = " pragma "  content = " no-cache " >
    
< meta http - equiv = " cache-control "  content = " no-cache " >
    
< meta http - equiv = " expires "  content = " 0 " >     
    
< meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " >
    
< meta http - equiv = " description "  content = " This is my page " >

  
</ head >
  
< body >
  
< center >
   
< h3 > 僱員註冊: </ h3 >< br >
   
< h4 >< a href = " ../emp/list.action " > 查看全部僱員 </ a ></ h4 >
   
< div id = " error "  style = " color:blue; font-weight:bold; " ></ div >
   
< s:form action = " add "  method = " post "  onsubmit = " return doValidate('form') "  name = " form "  id = " form " >
   
< s:textfield name = " employee.name "  label = " 姓名 "  id = " name " />
   
< s:textfield name = " employee.address "  label = " 地址 " />
   
< s:textfield name = " employee.phone "  label = " 電話 " />
   
< s:submit value = " 員工註冊 " />
   
</ s:form >
   
</ center >
  
</ body >
</ html >
複製代碼
複製代碼

list.jsp源碼:

 

複製代碼
<% @ page language = " java "  pageEncoding = " utf-8 " %>
<% @ taglib uri = " /struts-tags "  prefix = " s " %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
    
< head >
        
< title > list employee page </ title >

        
< meta http - equiv = " pragma "  content = " no-cache " >
        
< meta http - equiv = " cache-control "  content = " no-cache " >
        
< meta http - equiv = " expires "  content = " 0 " >
        
< meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " >
        
< meta http - equiv = " description "  content = " This is my page " >
        
< style type = " text/css " >
table {
    border: 1px solid black;
    border
- collapse: collapse;
}

table thead tr th {
    border: 1px solid black;
    padding: 3px;
    background
- color: #cccccc;
}

table tbody tr td {
    border: 1px solid black;
    padding: 3px;
}
</ style >
    
</ head >

    
< body >
        
< center >
            
< h3 >
                僱員管理:
            
</ h3 >
            
< br >
            
< h4 >
                
< a href = " ../emp/add.jsp " > 員工註冊 </ a >
            
</ h4 >
            
< s:form action = " delete "  theme = " simple " >
                
< table >
                    
< thead >
                        
< tr >
                            
< th >
                                選擇
                            
</ th >
                            
< th >
                                編號
                            
</ th >
                            
< th >
                                姓名
                            
</ th >
                            
< th >
                                電話
                            
</ th >
                            
< th >
                                地址
                            
</ th >
                            
< th >
                                操做
                            
</ th >
                        
</ tr >
                    
</ thead >
                    
< tbody >
                        
< s:iterator value = " employees " >
                            
< tr >
                                
< td >
                                    
< input type = " checkbox "  name = " id "
                                        value
= ' <s:property value="id" /> '   />
                                
</ td >
                                
< td >
                                    
< s:property value = " id "   />
                                
</ td >
                                
< td >
                                    
< s:property value = " name "   />
                                
</ td >
                                
< td >
                                    
< s:property value = " phone "   />
                                
</ td >
                                
< td >
                                    
< s:property value = " address "   />
                                
</ td >
                                
< td >
                                    
< a
                                        href
= ' <s:url action="edit"><s:param name="id" value="id" /></s:url> ' >
                                        修改 
</ a >   & nbsp;
                                    
< a
                                        href
= ' <s:url action="delete"><s:param name="id" value="id" /></s:url> ' >
                                        刪除 
</ a >
                                
</ td >
                            
</ tr >
                        
</ s:iterator >
                    
</ tbody >
                
</ table >
                
< s:submit value = " delete "   />
            
</ s:form >
        
</ center >
    
</ body >
</ html >
複製代碼
相關文章
相關標籤/搜索