<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 310, comments - 6939, trackbacks - 0, articles - 3
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)

    Posted on 2007-10-08 14:27 詩特林 閱讀(11748) 評(píng)論(9)  編輯  收藏 所屬分類: Struts
                                              Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)

  • Struts2+Spring2+Hibernate3 web應(yīng)用示例------源代碼
  • Struts2+Spring2+Hibernate3 web應(yīng)用示例(七)
  • Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)
  • Struts2+Spring2+Hibernate3 web應(yīng)用示例(五)
  • Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)
  • Struts2+Spring2+Hibernate3 web應(yīng)用示例(三)
  • Struts2+Spring2+Hibernate3 web應(yīng)用示例(二)
  • Struts2+Spring2+Hibernate3 web應(yīng)用示例(一)

  •  
  • 八、       配置Struts2

     

    Struts的配置文件都會(huì)在web.xml中注冊(cè)的。

    a)        Struts的配置文件如下:

    <?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>

        
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        
    <constant name="struts.devMode" value="true" />
        
    <constant name="struts.i18n.encoding" value="GBK" />   

        
    <!-- Add packages here -->

    </struts>

    Src/struts.xml

    b)        struts_book.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>

        
    <package name="products" extends="struts-default">
            
    <!--default-interceptor-ref name="validation"/-->
             
    <!-- Add actions here -->
            
    <action name="list" class="bookAction" method="list">            
                
    <result>/list.jsp</result>
            
    </action>

        
    <action name="delete" class="bookAction" method="delete">            
                
    <result type="redirect">list.action?queryMap=${queryMap}</result>
            
    </action>

            
    <action name="*" class="com.sterning.commons.AbstractAction">
                
    <result>/{1}.jsp</result>
            
    </action>
            
        
    <action name="edit" class="bookAction" method="load">
                
    <result>/editBook.jsp</result>
            
    </action>
           
           
    <action name="save" class="bookAction" method="save">
               
    <interceptor-ref name="params"/>
               
    <interceptor-ref name="validation"/>
                
    <result name="input">/editBook.jsp</result>
                
    <result type="redirect">list.action?queryMap=${queryMap}</result>
                  
            
    </action>
        
    </package>
    </struts>

     

    文件中的<interceptor-ref name="params"/>,使用了struts2自己的攔截器,攔截器在AOPAspect-Oriented Programming)中用于在某個(gè)方法或字段被訪問之前,進(jìn)行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實(shí)現(xiàn)策略。

    Struts 2已經(jīng)提供了豐富多樣的,功能齊全的攔截器實(shí)現(xiàn)。大家可以到struts2-all-2.0.6.jarstruts2-core-2.0.6.jar包的struts-default.xml查看關(guān)于默認(rèn)的攔截器與攔截器鏈的配置。

    struts-default.xml中已經(jīng)配置了大量的攔截器。如果您想要使用這些已有的攔截器,只需要在應(yīng)用程序struts.xml文件中通過“<include file="struts-default.xml" />”struts-default.xml文件包含進(jìn)來,并繼承其中的struts-default包(package),最后在定義Action時(shí),使用“<interceptor-ref name="xx" />”引用攔截器或攔截器棧(interceptor stack)。一旦您繼承了struts-default包(package),所有Action都會(huì)調(diào)用攔截器棧 ——defaultStack。當(dāng)然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆蓋defaultStack。

    作為框架(framework,可擴(kuò)展性是不可或缺的,因?yàn)槭郎蠜]有放之四海而皆準(zhǔn)的東西。雖然,Struts 2為我們提供如此豐富的攔截器實(shí)現(xiàn),但是這并不意味我們失去創(chuàng)建自定義攔截器的能力,恰恰相反,在Struts 2自定義攔截器是相當(dāng)容易的一件事。所有的Struts 2的攔截器都直接或間接實(shí)現(xiàn)接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜歡繼承類com.opensymphony.xwork2.interceptor.AbstractInterceptor。


     

    九、       配置Spring

     

    1Spring的配置文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
     
    <beans>
        
    <!-- dataSource config -->
        
    <bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
            
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
            
    <property name="url" value="jdbc:mysql://localhost:3306/game" /> 
            
    <property name="username" value="root" /> 
            
    <property name="password" value="root"/> 
        
    </bean> 
        
        
    <!-- SessionFactory -->
        
    <bean id="sessionFactory"
            class
    ="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

            
    <property name="dataSource">
                
    <ref bean="dataSource"/>
            
    </property>
            
    <property name="configLocation">
                
    <value>classpath:com\sterning\bean\hibernate\hibernate.cfg.xml</value>
            
    </property>        
        
    </bean>
        
        
    <!-- TransactionManager  不過這里暫時(shí)沒注入-->
        
    <bean id="transactionManager"
            class
    ="org.springframework.orm.hibernate3.HibernateTransactionManager">
            
    <property name="sessionFactory">
                
    <ref local="sessionFactory"/>
            
    </property>
        
    </bean>
        
        
    <!-- DAO -->
        
    <bean id="booksDao" class="com.sterning.books.dao.hibernate.BooksMapDao">
            
    <property name="sessionFactory">
                
    <ref bean="sessionFactory"/>
            
    </property>
        
    </bean>
        
        
    <!-- Services -->
        
    <bean id="booksService" class="com.sterning.books.services.BooksService">
            
    <property name="booksDao">
                
    <ref bean="booksDao"/>
            
    </property>
        
    </bean>
        
        
    <bean id="pagerService" class="com.sterning.commons.PagerService"/>
        
        
    <!-- view -->
        
    <bean id="bookAction" class="com.sterning.books.web.actions.BooksAction" singleton="false">
            
    <property name="booksService">
                
    <ref bean="booksService"/>
            
    </property>
            
    <property name="pagerService">
                
    <ref bean="pagerService"/>
            
    </property>
        
    </bean>  
        
    </beans>

      WebRoot/WEB-INF/srping-content/applicationContent.xml

    2Struts.properties.xml

    本來此文件應(yīng)該寫在struts 配置一節(jié),但主要是考慮這體現(xiàn)了集成spring的配置,所以放在spring的配置這里來講。

    struts.objectFactory = spring  
    struts.locale=zh_CN
    struts.i18n.encoding = GBK
     

    struts.objectFactoryObjectFactory 實(shí)現(xiàn)了 com.opensymphony.xwork2.ObjectFactory接口(spring)。struts.objectFactory=spring,主要是告知Struts 2運(yùn)行時(shí)使用Spring來創(chuàng)建對(duì)象(如Action等)。當(dāng)然,SpringContextLoaderListener監(jiān)聽器,會(huì)在web.xml文件中編寫,負(fù)責(zé)SpringWeb容器交互。

    struts.localeThe default locale for the Struts application。 默認(rèn)的國際化地區(qū)信息。

    struts.i18n.encoding:國際化信息內(nèi)碼。

    十、       Web.xml配置


    <?xml version="1.0" encoding="GB2312"?>
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd"
    >

    <web-app>
        
    <display-name>圖書管理系統(tǒng)</display-name>
        
    <context-param>
            
    <param-name>log4jConfigLocation</param-name>
            
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
        
    </context-param>
        
    <!-- ContextConfigLocation -->
        
    <context-param>
            
    <param-name>contextConfigLocation</param-name>
            
    <param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
          
    </context-param>
        
        
    <filter>
            
    <filter-name>encodingFilter</filter-name>
            
    <filter-class>com.sterning.commons.SetCharacterEncodingFilter</filter-class>
            
    <init-param>
                
    <param-name>encoding</param-name>
                
    <param-value>UTF-8</param-value>
            
    </init-param>
            
    <init-param>
                
    <param-name>forceEncoding</param-name>
                
    <param-value>true</param-value>
            
    </init-param>
        
    </filter>
         
    <filter>
            
    <filter-name>struts2</filter-name>
            
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
            
    <init-param>
                
    <param-name>config</param-name>
                
    <param-value>struts-default.xml,struts-plugin.xml,struts.xml,struts_books.xml</param-value>
            
    </init-param>
        
    </filter>    

        
    <filter-mapping>
            
    <filter-name>encodingFilter</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>
        
    <filter-mapping>
            
    <filter-name>struts2</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>        
        
        
    <!-- Listener contextConfigLocation -->
          
    <listener>
            
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          
    </listener>
        
    <!-- Listener log4jConfigLocation -->
          
    <listener>
            
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
          
    </listener>
     
        
    <!-- The Welcome File List -->
        
    <welcome-file-list>
            
    <welcome-file>index.jsp</welcome-file>
        
    </welcome-file-list>
    </web-app>

     

    Struts 2中,配置有少許改變,最明顯的是分發(fā)器(dispatcher)已由Servlet轉(zhuǎn)為Servlet Filter, 其配置和Servlet一樣簡單。和Servlet配置一樣,Filter配置中定義了Filter的名稱(作為引用)和類名。Filter Mapping通過URI和名稱匹配來調(diào)用相應(yīng)的Filter。默認(rèn)情況下,擴(kuò)展名為“.action”,這是在default.properties文件(在Struts 2 JAR文件里)的“struts.action.extension”屬性定義的。

    待續(xù).....


  • 評(píng)論

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)  回復(fù)  更多評(píng)論   

    2007-10-08 14:59 by 阿蜜果
    蠻不錯(cuò),不過建議在這一系列文章中添加上篇和下篇的鏈接,讓我們?cè)偻迪聭?
    啊哈

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)  回復(fù)  更多評(píng)論   

    2008-08-26 14:25 by ww13515517287@126.com
    Struts.properties.xml
    這個(gè)文件放在哪個(gè)目錄下的?。?br>能發(fā)份完整的jar包和源代碼給我嗎~
    謝了啊
    ww13515517287@126.com

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)  回復(fù)  更多評(píng)論   

    2009-04-03 12:54 by 897197468
    相見恨晚啊,不知道怎么感謝你才好

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)  回復(fù)  更多評(píng)論   

    2009-12-24 13:24 by 您好
    能把源代碼給我嗎?謝謝了啊
    lijinhua_java@yahoo.com.cn

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)  回復(fù)  更多評(píng)論   

    2011-10-17 18:03 by 李曉軍
    你好:

    我最近在學(xué)習(xí)Struts2,你能把源碼給我發(fā)一份嗎?非常感謝

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)  回復(fù)  更多評(píng)論   

    2011-10-17 18:04 by 李曉軍
    剛才忘了留Email了,921997384@qq.com ,謝謝了

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)[未登錄]  回復(fù)  更多評(píng)論   

    2011-10-24 16:38 by dd
    您好,能不能把源碼給我發(fā)一份?365092979@qq.com

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)[未登錄]  回復(fù)  更多評(píng)論   

    2012-06-13 10:22 by 你好
    謝謝

    # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)  回復(fù)  更多評(píng)論   

    2012-12-04 09:38 by Awy
    你好樓主,不知道你還在不,能不能把源碼發(fā)給我一份。謝謝
    我的郵箱是495917320@qq.com
    主站蜘蛛池模板: 亚洲AV本道一区二区三区四区| 亚洲精品色婷婷在线影院| 日韩一级视频免费观看| 免费乱码中文字幕网站| 亚洲人成色77777| 久久亚洲精品无码VA大香大香 | 日韩毛片免费无码无毒视频观看| 在线观看人成网站深夜免费| 全黄性性激高免费视频| 日韩亚洲一区二区三区| 亚洲乱码一二三四区乱码| 美女视频黄视大全视频免费的| 最近国语视频在线观看免费播放| 91成年人免费视频| 免费v片视频在线观看视频| 亚洲阿v天堂在线| 性xxxx黑人与亚洲| a一级毛片免费高清在线| 最近的中文字幕大全免费8| 国产极品美女高潮抽搐免费网站| 亚洲精品乱码久久久久久蜜桃不卡 | 特级毛片A级毛片100免费播放| 中文字幕免费不卡二区| 成年女人午夜毛片免费看| 亚洲色大成网站WWW久久九九| 亚洲依依成人精品| 一区二区视频免费观看| 国内精自视频品线六区免费| 亚洲国产成人久久一区久久| 18gay台湾男同亚洲男同| 人妻18毛片a级毛片免费看| 91频在线观看免费大全| 精品国产香蕉伊思人在线在线亚洲一区二区 | 日韩在线免费播放| 亚洲精品线在线观看| 亚洲精品色在线网站| 久久永久免费人妻精品下载| 国产yw855.c免费视频| 亚洲熟妇无码爱v在线观看| 日日摸夜夜添夜夜免费视频 | 久久香蕉国产线看免费|