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

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

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

    空間站

    北極心空

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

    IoC(Inversion of Control,以下譯為控制反轉(zhuǎn))隨著Java社區(qū)中輕量級(jí)容器(Lightweight Contianer)的推廣而越來越為大家耳熟能詳。在此,我不想再多費(fèi)唇舌來解釋“什么是控制反轉(zhuǎn)”和“為什么需要控制反轉(zhuǎn)”。因?yàn)榛ヂ?lián)網(wǎng)上已經(jīng)有非常多的文章對(duì)諸如此類的問題作了精彩而準(zhǔn)確的回答。大家可以去讀一下Rod Johnson和Juergen Hoeller合著的《Expert one-on-one J2EE Development without EJB》或Martin Fowler所寫的《Inversion of Control Containers and the Dependency Injection pattern》。

    言歸正傳,本文的目的主要是介紹在Struts 2中實(shí)現(xiàn)控制反轉(zhuǎn)。

    歷史背景

    眾所周知,Struts 2是以Webwork 2作為基礎(chǔ)發(fā)展出來。而在Webwork 2.2之前的Webwork版本,其自身有一套控制反轉(zhuǎn)的實(shí)現(xiàn),Webwork 2.2在Spring 框架的如火如荼發(fā)展的背景下,決定放棄控制反轉(zhuǎn)功能的開發(fā),轉(zhuǎn)由Spring實(shí)現(xiàn)。值得一提的是,Spring確實(shí)是一個(gè)值得學(xué)習(xí)的框架,因?yàn)橛性絹碓蕉嗟拈_源組件(如iBATIS等)都放棄與Spring重疊的功能的開發(fā)。因此,Struts 2推薦大家通過Spring實(shí)現(xiàn)控制反轉(zhuǎn)。

    具體實(shí)現(xiàn)

    首先,在開發(fā)環(huán)境中配置好Struts 2的工程。對(duì)這部分仍然有問題的朋友,請(qǐng)參考我的早前的文章。

    然后,將所需的Spring的jar包加入到工程的構(gòu)建環(huán)境(Build Path)中,如下圖1所示:

    圖1 所依賴的Spring的jar包
    圖1 所依賴的Spring的jar包

    本文使用的是Spring 2.0,Spring強(qiáng)烈建議大家在使用其jar包時(shí),只引用需要的包,原因是Spring是一個(gè)功能非常強(qiáng)大的框架,其中有些功能是您不需要的;而且Spring提倡的是“按需所取”,而不是EJB的“愛我就要愛我的一切”。當(dāng)然,如果你怕麻煩或者是不清楚每個(gè)包的作用,引用一個(gè)Spring的總包也未嘗不可。

    接下來,就要修改WEB-INF\web.xml文件了,內(nèi)容為:

    <? xml version="1.0" encoding="UTF-8" ?>
    < web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    >

       
    < display-name > Struts 2 IoC Demo </ display-name >

       
    < filter >
           
    < filter-name > struts-cleanup </ filter-name >
           
    < filter-class >
                org.apache.struts2.dispatcher.ActionContextCleanUp
           
    </ filter-class >
       
    </ filter >

       
    < filter >
           
    < filter-name > struts2 </ filter-name >
           
    < filter-class >
                org.apache.struts2.dispatcher.FilterDispatcher
           
    </ filter-class >
       
    </ filter >

       
    < filter-mapping >
           
    < filter-name > struts-cleanup </ filter-name >
           
    < url-pattern > /* </ url-pattern >
       
    </ filter-mapping >

       
    < filter-mapping >
           
    < filter-name > struts2 </ filter-name >
           
    < url-pattern > /* </ url-pattern >
       
    </ filter-mapping >

       
    < listener >
           
    < listener-class >
                org.springframework.web.context.ContextLoaderListener
           
    </ listener-class >
       
    </ listener >

       
    < welcome-file-list >
           
    < welcome-file > index.html </ welcome-file >
       
    </ welcome-file-list >
    </ web-app >
    清單1 WEB-INF\web.xml

    大家一看便知道,主要是加入Spring的ContextLoaderListener監(jiān)聽器,方便Spring與Web容器交互。

    緊接著,修改Struts.properties文件,告知Struts 2運(yùn)行時(shí)使用Spring來創(chuàng)建對(duì)象(如Action等),內(nèi)容如下:

    struts.objectFactory = spring
    清單2 classes\struts.properties

    再下來,遵循Spring的原則——面向接口編程,創(chuàng)建接口ChatService,代碼如下:

    package tutorial;

    import java.util.Set;

    public interface ChatService {
       Set
    < String > getUserNames();
    }
    清單3 tutorial.ChatService.java

    然后,再創(chuàng)建一個(gè)默認(rèn)實(shí)現(xiàn)ChatServiceImpl,代碼如下:

    package tutorial;

    import java.util.HashSet;
    import java.util.Set;

    public class ChatServiceImpl implements ChatService {

       
    public Set < String > getUserNames() {
           Set
    < String > users = new HashSet < String > ();
           users.add(
    " Max " );
           users.add(
    " Scott " );
           users.add(
    " Bob " );
           
    return users;
       }


    }
    清單4 tutorial.ChatServiceImpl.java

    接下來,就該新建Action了。tutorial.ChatAction.java的代碼如下:

    package tutorial;

    import java.util.Set;

    import com.opensymphony.xwork2.ActionSupport;

    public class ChatAction extends ActionSupport {
       
    private static final long serialVersionUID = 8445871212065L
       
       
    private ChatService chatService;
       
    private Set < String > userNames;

       
    public void setChatService(ChatService chatService) {
           
    this .chatService = chatService;
       }

       
       
    public Set < String > getUserNames() {
           
    return userNames;
       }

       
       @Override
       
    public String execute() {
           userNames
    = chatService.getUserNames();
           
    return SUCCESS;
       }

       
    }
    清單5 tutorial.ChatAction.java

    ChatAction類使用屬性(Getter/Setter)注入法取得ChatService對(duì)象。

    然后,配置Spring的applicationContext.xml(位于WEB-INF下)文件,內(nèi)容如下:

    <? xml version="1.0" encoding="UTF-8" ?>
    < beans xmlns ="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
       
    < bean id ="chatService" class ="tutorial.ChatServiceImpl" />
       
    < bean id ="chatAction" class ="tutorial.ChatAction" scope ="prototype" >
           
    < property name ="chatService" >
               
    < ref local ="chatService" />
           
    </ property >
       
    </ bean >
    </ beans >
    清單6 WEB-INF\applicationContext.xml

    上述代碼有二點(diǎn)值得大家注意的:

    1. Struts 2會(huì)為每一個(gè)請(qǐng)求創(chuàng)建一個(gè)Action對(duì)象,所以在定義chatAction時(shí),使用scope="prototype"。這樣Spring就會(huì)每次都返回一個(gè)新的ChatAction對(duì)象了;
    2. 因?yàn)镃hatServiceImpl被配置為默認(rèn)的scope(也即是singleton,唯一的),所以在實(shí)現(xiàn)時(shí)應(yīng)保證其線程安全(關(guān)于編寫線程安全的代碼的討論已經(jīng)超出本文的范圍,更超出了本人的能力范圍,大家可以參考Addison Wesley Professional出版的《Java Concurrency in Practice》)。

    接下來,在classes/struts.xml中配置Action,內(nèi)容如下:

    <! DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd"
    >
    < struts >
       
    < include file ="struts-default.xml" />    
        
       
    < package name ="Struts2_IoC" extends ="struts-default" >
           
    < action name ="Chat" class ="chatAction" >
               
    < result > /UserList.jsp </ result >
           
    </ action >
       
    </ package >    
    </ struts >
    清單7 classes\struts.xml

    這里的Action和平常不同的就是class屬性,它對(duì)應(yīng)于Spring所定義的bean的id,而不是它的類全名。

    最后,讓我們看看/UserList.jsp,內(nèi)容如下:

    <% @ page contentType = " text/html; charset=UTF-8 " %>
    <% @ taglib prefix = " s " uri = " /struts-tags " %>
    < html >
    < head >
       
    < title > User List </ title >
    </ head >

    < body >
       
    < h2 > User List </ h2 >
       
    < ol >
       
    < s:iterator value ="userNames" >
           
    < li >< s:property /></ li >
       
    </ s:iterator >
       
    </ ol >
    </ body >
    </ html >
    清單8 /UserList.jsp

    大功告成,分布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入http://localhost:8080/Struts2_IoC/Chat.action,出現(xiàn)如圖2所示頁(yè)面:

    圖2 /ListUser.jsp
    圖2 /ListUser.jsp

    總結(jié)

    通過Spring在Struts 2上實(shí)現(xiàn)控制反轉(zhuǎn)是強(qiáng)烈推薦的做法,當(dāng)然您也可以組合其它的實(shí)現(xiàn)(如Pico等)。

    posted on 2007-04-19 13:02 蘆葦 閱讀(203) 評(píng)論(0)  編輯  收藏 所屬分類: Struts
    主站蜘蛛池模板: 久久精品免费视频观看| 亚洲国产av高清无码| 亚洲国产一区明星换脸| 四虎永久免费影院| 国内一级一级毛片a免费| 久久精品无码一区二区三区免费| 国产人成网在线播放VA免费| 一级毛片免费播放男男| 美女18毛片免费视频| 香蕉视频在线观看免费| 污污视频网站免费观看| 男女猛烈无遮掩视频免费软件| 亚洲综合在线一区二区三区| 在线亚洲高清揄拍自拍一品区| 亚洲人成网站18禁止久久影院| 亚洲精品国产肉丝袜久久| 亚洲午夜久久久精品电影院| 亚洲 欧洲 视频 伦小说| 亚洲精品欧美综合四区| 国产成人亚洲精品电影| 国产成人1024精品免费| 成全高清在线观看免费| 最近中文字幕mv免费高清在线 | 老司机亚洲精品影视www| 国产亚洲人成网站在线观看 | 久久青草国产免费观看| xxxxx免费视频| 四虎成人免费大片在线| 五月婷婷亚洲综合| 狠狠色伊人亚洲综合成人| 亚洲高清日韩精品第一区| 国产亚洲福利在线视频| 日韩在线观看免费| 中文字幕乱理片免费完整的| 97精品免费视频| 暖暖在线日本免费中文| 国产亚洲一区二区三区在线不卡 | 国产亚洲精午夜久久久久久 | 久久经典免费视频| 国产又黄又爽又刺激的免费网址| 亚洲av中文无码|