Struts2學(xué)習(xí)筆記(一)

             使用Struts2開發(fā)都快1個(gè)月了,感覺雖然使用它實(shí)現(xiàn)了項(xiàng)目的功能,但很多新的東西都沒有應(yīng)用進(jìn)來(lái),
          前一段時(shí)間項(xiàng)目催的太緊,所以決定最近系統(tǒng)的對(duì)Struts2學(xué)習(xí)一下,也記錄一下學(xué)習(xí)的過(guò)程。
          很多細(xì)節(jié)的例子請(qǐng)參考struts的demo.這里主要介紹一些概念及流程.
          另外推薦一個(gè)好的strtus2 blog: blogjava.net/max
           
          1.先看一下官方網(wǎng)站對(duì)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模式(關(guān)于MVC&MVC2,就不羅嗦了)
           (2)主要從從WebWork and XWork 演變過(guò)來(lái)
          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.當(dāng)Client請(qǐng)求過(guò)來(lái)時(shí),Controller截獲請(qǐng)求,再轉(zhuǎn)發(fā)給合適的Action處理.
            (2)Model 代表application data以及控制數(shù)據(jù)的businness logic,當(dāng)Controller轉(zhuǎn)發(fā)請(qǐng)求給Action后,Action調(diào)用Model
              處理business logci,application data的狀態(tài)被改變,Action控制權(quán)交給Controller,由Controller決定顯示在Client端
              的View.
            (3)View代表顯示在客戶端的數(shù)據(jù)表現(xiàn),可以是JSP、Velocity、Freemaker、XSLT.

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

            4.1 加載Filter Servlet
              當(dāng)客戶端請(qǐng)求一個(gè)Struts2的web application時(shí),web server將查找關(guān)于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 攔截器處理請(qǐng)求
               Interceptors 提供pre-processing & post-processing,request在到達(dá)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 執(zhí)行Action(詳細(xì)請(qǐng)參考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 返回結(jié)果
          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) 評(píng)論(2)  編輯  收藏 所屬分類: J2EE

          評(píng)論

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

          我現(xiàn)在在學(xué)習(xí)struts 1.2.×
          等我學(xué)精通了再看看struts2的內(nèi)容。
          畢竟struts現(xiàn)在用的還很多呀!
            回復(fù)  更多評(píng)論   

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

          @itkui

          我也是用了好久的struts1,現(xiàn)在項(xiàng)目要求才轉(zhuǎn)的
          不過(guò)struts2.0確實(shí)先進(jìn)很多
            回復(fù)  更多評(píng)論   

          公告


          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(13)

          我參與的團(tuán)隊(duì)

          隨筆分類(69)

          隨筆檔案(68)

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 梁平县| 西华县| 日土县| 台湾省| 鄢陵县| 陈巴尔虎旗| 大港区| 桓台县| 汉寿县| 库尔勒市| 长子县| 土默特右旗| 康定县| 白水县| 陇南市| 合作市| 耒阳市| 祁门县| 土默特左旗| 龙井市| 蒙阴县| 固镇县| 桐城市| 紫阳县| 泰安市| 三河市| 开江县| 棋牌| 三台县| 长治县| 三门峡市| 融水| 铜山县| 温泉县| 富锦市| 宁阳县| 盐边县| 阜新| 静宁县| 廊坊市| 肇东市|