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

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

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

    Struts2學習筆記(一)

       使用Struts2開發都快1個月了,感覺雖然使用它實現了項目的功能,但很多新的東西都沒有應用進來,
    前一段時間項目催的太緊,所以決定最近系統的對Struts2學習一下,也記錄一下學習的過程。
    很多細節的例子請參考struts的demo.這里主要介紹一些概念及流程.
    另外推薦一個好的strtus2 blog: blogjava.net/max
     
    1.先看一下官方網站對Struts2的定義(struts2.org):
    Apache Struts 2 was originally known as WebWork 2.
    After working independently 
    for several years, the WebWork and Struts communities joined forces to create Struts2.
    This 
    new version of Struts is simpler to use and closer to how Struts was always meant to be.

    2.Introduction
      (1) 基于MVC2模式(關于MVC&MVC2,就不羅嗦了)
     (2)主要從從WebWork and XWork 演變過來
    Struts is an Open-Source Web Application Framework that simplifies the creation of a Java Web Application.
    It is based on the Model
    -View-Controller 2 (MVC 2) Architecture which was originally found in a language called SmallTalk.
    The recent version of Struts is Struts 
    2.0 and it has borrowed most of the concepts in terms of architecture and functionality
    from two frameworks namely WebWork and XWork. 

    3.Struts2 MVC2 Architecture
      (1)Controller 作為Model&View的mediator.當Client請求過來時,Controller截獲請求,再轉發給合適的Action處理.
      (2)Model 代表application data以及控制數據的businness logic,當Controller轉發請求給Action后,Action調用Model
        處理business logci,application data的狀態被改變,Action控制權交給Controller,由Controller決定顯示在Client端
        的View.
      (3)View代表顯示在客戶端的數據表現,可以是JSP、Velocity、Freemaker、XSLT.

    4.Struts2 的處理流程
      (1) Client發送請求給Web application.Web Server 尋找該app對應的配置文件web.xml并加載相關Boot-strap Component.
            在Struts2中是Servlet Filter(而在s1中是Action Servlet).
      (2) servlet Filter 從配置文件(struts.xml)中匹配請求的動作,即xxx.action
      (3) Controller 先將request請求交給 Interceptor stack 處理,接著在傳給相關Action.
      (4) 請求被傳給Action后,基于請求的動作,Action執行合適的業務邏輯處理.
      (5) Action執行完畢,返回結果
      (6) Controller根據Action返回的結果選擇具體的View顯示在客戶端.

      4.1 加載Filter Servlet
        當客戶端請求一個Struts2的web application時,web server將查找關于application的配置文件web.xml
    <filter>
       <filter-name>Struts2FilterServlet</filter-name>
      
    <filter-class>
            org.apache.struts.action2.dispatcher.FilterDispatcher
      
    </filter-class>
    </filter>
                        
    <filter-mapping>
      <filter-name>Struts2FilterServlet</filter-name>
        
    <url-pattern>/*</url-pattern>
    </filter-mapping>
                        
      
      4.2 攔截器處理請求
         Interceptors 提供pre-processing & post-processing,request在到達framework之前,首先被傳給一系列的攔截器.
     可以自定義攔截器并更改配置文件,如下 


    import com.opensymphony.xwork2.interceptor.*;

    class AuthenticationInterceptor implements Interceptor{

        
    public void init(){}

        
    public void destroy(){}

        
    public String intercept(ActionInvocation invocation) throws Exception{
        
             
    // Get the value of user credentials from the Request and Validate it.
           
        }
    }

    更改struts.xml
    <struts>
            
        
    <interceptors>
            
    <interceptor name = "authentication" 
                class 
    = "myinterceptors.AuthenticationInterceptor">        
            
    </interceptor>
        
    <interceptors>
           
       
    </struts>

      4.3 執行Action(詳細請參考struts2的例子)
     
    import java.servlet.http.*;
    import org.apache.struts.*;

    class MyAction extends Action{

        
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
    throws java.lang.Exception {
            
            
    // Do some business logic here.
            
        }
    }
     
    配置文件定義如下
      
                        
    <struts>

        
    <action name = "Registration" class = "hello.RegistrationAction">
        
    <action name = "Login" class = "hello.LoginAction">
        
    <action name = "Logout" class = "hello.LogoutAction">

    </struts>
                        

    4.4 返回結果
    package myactions;

    public class MyAction{

        
    public String execute(){

            
    if (createOperation()){
                
    return "create";
            }
    else if (deleteOperation()){
                
    return "delete";
            }
    else if( readOperation()){
                
    return "read";
            }
    else if  (writeOperation()){
                
    return "write";
            }
            
    return "error";
        }
    }
    strtus.xml配置:
    <struts>
        
    <action name = "MyAction" class = "myactions.MyAction">
            
    <result name = "create">/myApp/create.jsp</result>
            
    <result name = "delete">/myApp/delete.jsp</result>
            
    <result name = "read">/myApp/read.jsp</result>
            
    <result name = "write">/myApp/write.jsp</result>
        
    </action>
    </struts>





    posted on 2007-06-20 11:26 想飛就飛 閱讀(2530) 評論(2)  編輯  收藏 所屬分類: J2EE

    評論

    # re: Struts2學習筆記(一) 2007-06-20 13:23 itkui

    我現在在學習struts 1.2.×
    等我學精通了再看看struts2的內容。
    畢竟struts現在用的還很多呀!
      回復  更多評論   

    # re: Struts2學習筆記(一) 2007-06-20 13:25 想飛就飛

    @itkui

    我也是用了好久的struts1,現在項目要求才轉的
    不過struts2.0確實先進很多
      回復  更多評論   

    公告


    導航

    <2007年6月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    統計

    常用鏈接

    留言簿(13)

    我參與的團隊

    隨筆分類(69)

    隨筆檔案(68)

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲午夜理论片在线观看| 午夜亚洲www湿好大| 亚洲人成电影网站久久| 1000部国产成人免费视频| 婷婷久久久亚洲欧洲日产国码AV| 亚欧洲精品在线视频免费观看| 亚洲精品黄色视频在线观看免费资源| 国产亚洲男人的天堂在线观看| 国产乱子伦精品免费女| 四虎永久在线精品免费一区二区| 亚洲成a人片在线观看中文动漫| 波多野结衣免费一区视频| 亚洲欧洲国产精品香蕉网| 日本高清免费观看| 亚洲美女免费视频| 插B内射18免费视频| 国产成人精品亚洲| 亚洲日韩精品A∨片无码| 在线播放免费人成毛片乱码| 亚洲黄色在线电影| 免费观看成人毛片a片2008| 亚洲A∨精品一区二区三区下载| 亚洲成a人在线看天堂无码| 你懂的网址免费国产| 亚洲一区二区三区久久| 国产亚洲精品免费| 日韩免费电影网址| 亚洲欧洲无卡二区视頻| 亚洲国产精品一区二区第一页免| 91国内免费在线视频| 亚洲成a人片77777群色| 免费a级毛片无码av| 无码日韩精品一区二区免费暖暖| 亚洲视频无码高清在线| 亚洲熟妇少妇任你躁在线观看无码| 精品亚洲永久免费精品| 亚洲人成无码网站在线观看| 亚洲五月综合缴情在线观看| 又黄又爽又成人免费视频| 一级人做人a爰免费视频| 国产精品亚洲四区在线观看|