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

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

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

    隨筆-21  評(píng)論-29  文章-0  trackbacks-0
     

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

            1
    、模型(Model):本質(zhì)上來(lái)說(shuō)在StrutsModel是一個(gè)商業(yè)邏輯類,開(kāi)發(fā)者實(shí)現(xiàn)商業(yè)邏輯。
            2
    、視圖(View):View是由與控制器Servlet配合工作的一整套JSP定制標(biāo)簽庫(kù)構(gòu)成,利用他們可以快速建立應(yīng)用系統(tǒng)的界面。
            3
    、控制器(Controller):前端控制器是一個(gè)Servlet。它將客戶端請(qǐng)求轉(zhuǎn)發(fā)到相應(yīng)的后端控制器ActionForm類。
    ActionServlet(中心控制器)
    ——參看APIDOC
          
    定義:繼承自javax.servlet.http.HttpServlet類,是中心控制器(總控制器)。它提供一個(gè)中心位置來(lái)處理全部的終端請(qǐng)求。
          
    作用:接受請(qǐng)求、填充數(shù)據(jù)、派發(fā)請(qǐng)求、響應(yīng)用戶  
        配置:在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.
      問(wèn)題:Action是什么時(shí)候初始化的?服務(wù)啟動(dòng)的時(shí)候,還是接收到用戶第一次請(qǐng)求的時(shí)候?
    AddStudentAction中加入以下代碼(加入其構(gòu)造方法)

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

    重新部署后觀察頁(yè)面和控制器


    以上證明:Action發(fā)出該Action請(qǐng)求,不是在讀取配置時(shí)初始化的!
    再打開(kāi)另外一個(gè)瀏覽器,新建一個(gè)會(huì)話,發(fā)現(xiàn)該語(yǔ)句只打印一次,即證明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的線程安全性,不能多個(gè)請(qǐng)求共享一個(gè)Action實(shí)例。
    怎樣實(shí)現(xiàn)Action的安全性編程?
       注意不要用實(shí)例變量或者類變量共享只是針對(duì)某個(gè)請(qǐng)求的數(shù)據(jù)
       注意資源操作的同步性。
    應(yīng)用:可以統(tǒng)計(jì)一個(gè)Action訪問(wèn)次數(shù)。(設(shè)計(jì)一個(gè)實(shí)例變量count 其在調(diào)用時(shí)加1)
    新建一個(gè)CountAction類,其繼承自Action類


    在struts-config.xml中注冊(cè)  即加入如下代碼
    <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 ;
        }


    }

    重新部署應(yīng)用 http://localhost:8080/strutstest//countAction.do 實(shí)現(xiàn)了計(jì)數(shù)功能





    有n個(gè)請(qǐng)求就會(huì)執(zhí)行n個(gè)對(duì)應(yīng)的execute方法,但這n個(gè)execute方法同時(shí)操作count變量,要注意線程安全!

    posted on 2009-05-02 22:14 特立獨(dú)行 閱讀(261) 評(píng)論(0)  編輯  收藏 所屬分類: Struts框架
    主站蜘蛛池模板: 亚洲五月六月丁香激情| 国产四虎免费精品视频| 日本高清免费不卡在线| 免费无码不卡视频在线观看| 国产免费人视频在线观看免费| 亚洲综合激情五月色一区| 国产h视频在线观看网站免费| 亚洲综合视频在线| 中文免费观看视频网站| 亚洲色偷偷av男人的天堂| 全免费毛片在线播放| 亚洲精品无码AV中文字幕电影网站| 色欲aⅴ亚洲情无码AV蜜桃 | 亚洲国产精品精华液| 国产成人免费ā片在线观看 | 精品国产亚洲男女在线线电影| aaa毛片视频免费观看| 亚洲国产日韩在线视频| 午夜免费福利视频| 亚洲人成www在线播放| 免费看的黄色大片| 国产成人1024精品免费| 亚洲视频精品在线| 成年人网站在线免费观看| 日本系列1页亚洲系列| 伊人久久亚洲综合| 国产成人精品免费视频网页大全| 456亚洲人成影院在线观| 国产乱子影视频上线免费观看| 中国一级全黄的免费观看| 久久亚洲精品无码AV红樱桃| 精品剧情v国产在免费线观看 | 最新黄色免费网站| 亚洲第一街区偷拍街拍| 亚洲日韩欧洲无码av夜夜摸| 成a人片亚洲日本久久| 日本亚洲视频在线| 最近中文字幕无免费视频| eeuss免费天堂影院| 亚洲毛片免费观看| 亚洲成av人片一区二区三区|