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

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

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

    隨筆-21  評論-29  文章-0  trackbacks-0
     

    StrutsMVC組件
            組件包括:ActionServlet 、Action Classes 、Action Mapping(此處包括ActionForward)、ActionForm Bean。
    Struts中的MVC

            1
    、模型(Model):本質上來說在StrutsModel是一個商業邏輯類,開發者實現商業邏輯。
            2
    、視圖(View):View是由與控制器Servlet配合工作的一整套JSP定制標簽庫構成,利用他們可以快速建立應用系統的界面。
            3
    、控制器(Controller):前端控制器是一個Servlet。它將客戶端請求轉發到相應的后端控制器ActionForm類。
    ActionServlet(中心控制器)
    ——參看APIDOC
          
    定義:繼承自javax.servlet.http.HttpServlet類,是中心控制器(總控制器)。它提供一個中心位置來處理全部的終端請求。
          
    作用:接受請求、填充數據、派發請求、響應用戶  
        配置:在web配置文件web.xml中聲明。

    <servlet>
           
    <servlet-name>action</servlet-name>
           
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
           
    <init-param>
               
    <param-name>config</param-name>
               
    <param-value>/WEB-INF/struts-config.xml</param-value>
           
    </init-param>
           
    <load-on-startup>0</load-on-startup>
        
    </servlet>

        
    <servlet-mapping>
           
    <servlet-name>action</servlet-name>  
           
    <url-pattern>*.do</url-pattern>
        
    </servlet-mapping>

    Action 
      

    APIDOC:An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (RequestProcessor) will select an appropriate Action for each request, create an instance (if necessary), and call the execute method.
      問題:Action是什么時候初始化的?服務啟動的時候,還是接收到用戶第一次請求的時候?
    AddStudentAction中加入以下代碼(加入其構造方法)

    public AddStudentAction(){
            System.out.println(
    "in AddStudentAction constructor");
        }

    重新部署后觀察頁面和控制器


    以上證明:Action發出該Action請求,不是在讀取配置時初始化的!
    再打開另外一個瀏覽器,新建一個會話,發現該語句只打印一次,即證明Action只初始化一次!

    APIDOC:Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:  

    • Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
    • Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.

    注意:Action的線程安全性,不能多個請求共享一個Action實例。
    怎樣實現Action的安全性編程?
       注意不要用實例變量或者類變量共享只是針對某個請求的數據
       注意資源操作的同步性。
    應用:可以統計一個Action訪問次數。(設計一個實例變量count 其在調用時加1)
    新建一個CountAction類,其繼承自Action類


    在struts-config.xml中注冊  即加入如下代碼
    <action path="/countAction" type="cn.itcast.CountAction"></action>
    編輯CountAction代碼 如下

    package cn.itcast;

    import java.io.PrintWriter;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    public class CountAction extends Action {
        
        
    private Integer count = 0 ;
        
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                
    throws Exception {
            
            
    synchronized (count){
            count
    ++ ;
            }

            PrintWriter out 
    = response.getWriter();
            out.println(
    "count="+count);        
            
    return null ;
        }


    }

    重新部署應用 http://localhost:8080/strutstest//countAction.do 實現了計數功能





    有n個請求就會執行n個對應的execute方法,但這n個execute方法同時操作count變量,要注意線程安全!

    posted on 2009-05-02 22:14 特立獨行 閱讀(261) 評論(0)  編輯  收藏 所屬分類: Struts框架
    主站蜘蛛池模板: 学生妹亚洲一区二区| 精品亚洲国产成人av| 久久亚洲春色中文字幕久久久| 亚洲国产日产无码精品| 亚洲精品无码高潮喷水A片软| a毛片成人免费全部播放| 91老湿机福利免费体验| 日日操夜夜操免费视频| 亚洲女初尝黑人巨高清| 蜜芽亚洲av无码一区二区三区| 久久久久成人精品免费播放动漫| 免费精品国偷自产在线在线| 亚洲最大AV网站在线观看| 亚洲人成电影网站久久| 成在人线AV无码免费| 亚洲特级aaaaaa毛片| 天黑黑影院在线观看视频高清免费| 成人免费福利电影| 国产亚洲精品AAAA片APP | 好先生在线观看免费播放| 自拍偷自拍亚洲精品情侣| 日本免费A级毛一片| 亚洲精品无码专区2| 亚洲免费综合色在线视频| 免费永久在线观看黄网站| 亚洲成a人无码亚洲成www牛牛| 亚洲高清视频免费| 亚洲va久久久噜噜噜久久天堂| 韩国亚洲伊人久久综合影院| 亚洲国产一区二区三区| 日韩电影免费在线观看中文字幕 | 亚洲老妈激情一区二区三区| 亚洲国产成人久久精品软件| 免费一级毛片正在播放| 国产亚洲一卡2卡3卡4卡新区| 亚洲无人区一区二区三区| 亚洲综合免费视频| 麻豆69堂免费视频| 免费人成视频在线观看不卡| 国产成人免费ā片在线观看老同学 | 免费A级毛片在线播放不收费|