2008年9月20日

          struts-config.xml中 action 的attribute屬性與name屬性

          1、在一般情況下,actionForm是被存儲在一定的scope中(request或session,通過action的scope屬性來配置),當我們在配置時,指定name而不指定attribute,那么指定的name值就作為actionForm存儲在scope中的key值,我們可以在action中通過httpServletRequest.getAttribute("指定的name屬性值")來獲得這個actionForm;     當我們既配置了name又配置了attribute,那么actionForm存儲在scope中的key值就采用attribute屬性指定的值了,這時要通過httpServletRequest.getAttribute("指定的attribute屬性值")來獲得actionForm,此時通過httpServletRequest.getAttribute("指定的name屬性值")是不能獲得actionForm的。  
             
              所以,是否配置attribute屬性就決定了actionForm存儲在scope中的key值是采用name,還是采用attribute

           2、 在《Programming Jakarta Struts》這本書中的第四章“Configuring the Struts Application”中這樣一段說明來分別闡述這兩
          個屬性:(102頁)
          ++++++++
          atribute:
          ++++++++
          The name of the request or session scope attribute under which the form bean for this action can be accessed.
          A value is only allowed here if there is a form bean specified in the name attribute. This attribute is
          optional and has no default value.

          ++++++++
          name:
          ++++++++
          The name of the form bean, if any, that is associated with this action. This value must be the name attribute
          from one of the form-bean elements defined earlier. This attribute is optional and has no default value.

          最初看這些真的還是不好區(qū)分這兩者。不過在仔細看過struts的源代碼以后,豁然開朗。。。

          下面主要對attribute進行解釋,應為沒有人會對name屬性不了解的(呵呵。。。)


          解釋:在struts實例化actionform的時候,有兩種情況:如果已經存在,那么從內存中取回;如果第一次實例化,那么創(chuàng)建,并放入內存。
          這樣就有一個問題了,struts是根據(jù)什么來取回并創(chuàng)建actionform的呢,答案就是attribute的值。讓我們進入struts的源代碼:

          /**
          *創(chuàng)建或者取回formbean方法
          *該方法在:org.apache.struts.util.RequestUtils中
          */
          public static Actionform createActionform(
          HttpServletRequest request,
          ActionMapping mapping,
          ModuleConfig moduleConfig,
          ActionServlet servlet) {
          。。。。
          。。。
          // Is there a form bean associated with this mapping?
          //得到action mapping中attribute的值
          String attribute = mapping.getAttribute();
          。。。。
          。。。。
          Actionform instance = null;
          HttpSession session = null;
          //yes!!就在這里了,把創(chuàng)建以后的actionform放在request或者session里,看到放入的名字了么,就是mapping.getAttribute();
          if ("request".equals(mapping.getScope())) {
          instance = (Actionform) request.getAttribute(attribute);
          } else {
          session = request.getSession();
          instance = (Actionform) session.getAttribute(attribute);
          }
          。。。
          。。。


          }


          下面又有一個問題浮出水面:如果我沒有在action mapping中指定attribute呢,那struts 是如何解決的?
          答案很簡單,如果單從結果上看,此時struts使用的name的值,為什么呢,看struts源代碼:

          /**
          * The request-scope or session-scope attribute name under which our
          * form bean is accessed, if it is different from the form bean's
          * specified <code>name</code>.
          *該代碼在:org.apache.struts.config.ActionConfig中
          */
          protected String attribute = null;

          public String getAttribute() {
          //yes!!!!就在這里,看到了吧,如果你沒有設定attribute,那么struts 會把name的值拿過來用。呵呵。。。
          if (this.attribute == null) {
          return (this.name);
          } else {
          return (this.attribute);
          }
          }

          public void setAttribute(String attribute) {
          if (configured) {
          throw new IllegalStateException("Configuration is frozen");
          }
          this.attribute = attribute;
          }

          posted @ 2008-09-20 13:37 chenkai 閱讀(701) | 評論 (1)編輯 收藏

          Request的getParameter和getAttribute方法的區(qū)別

          當兩個Web組件之間為轉發(fā)關系時,轉發(fā)源會將要共享 request范圍內的數(shù)據(jù)先用setAttribute將數(shù)據(jù)放入到HttpServletRequest對象中,然后轉發(fā)目標通過 getAttribute方法來取得要共享的數(shù)據(jù)。而MVC中用的就是Web組件之間的轉發(fā)啊!真是笨,怎么當時沒有想到呢?

                下面整理一下getParameter和getAttribute的區(qū)別和各自的使用范圍。

                (1)HttpServletRequest類有setAttribute()方法,而沒有setParameter()方法

                (2)當兩個Web組件之間為鏈接關系時,被鏈接的組件通過getParameter()方法來獲得請求參數(shù),例如假定welcome.jsp和authenticate.jsp之間為鏈接關系,welcome.jsp中有以下代碼:

                <a  href="authenticate.jsp?username=wolf">authenticate.jsp  </a>

                或者:

                <form  name="form1"  method="post"  action="authenticate.jsp">
                    請輸入用戶姓名:<input  type="text"  name="username">
                    <input  type="submit"  name="Submit"  value="提交">
                </form>

                 在authenticate.jsp中通過request.getParameter("username")方法來獲得請求參數(shù)username:

                 <%  String  username=request.getParameter("username");  %>

                 (3)當兩個Web組件之間為轉發(fā)關系時,轉發(fā)目標組件通過getAttribute()方法來和轉發(fā)源組件共享request范圍內的數(shù)據(jù)。

                  假定  authenticate.jsp和hello.jsp之間為轉發(fā)關系。authenticate.jsp希望向hello.jsp傳遞當前的用戶名字,  如何傳遞這一數(shù)據(jù)呢?先在authenticate.jsp中調用setAttribute()方法:

                  <%
                  String  username=request.getParameter("username");
                  request.setAttribute("username",username);
                  %>

                  <jsp:forward  page="hello.jsp"  />

                  在hello.jsp中通過getAttribute()方法獲得用戶名字:

                  <%  String  username=(String)request.getAttribute("username");  %>
                  Hello:  <%=username  %>

                  從更深的層次考慮,request.getParameter()方法傳遞的數(shù)據(jù),會從Web客戶端傳到Web服務器端,代表HTTP請求數(shù)據(jù)。request.getParameter()方法返回String類型的數(shù)據(jù)。

                  request.setAttribute()和getAttribute()方法傳遞的數(shù)據(jù)只會存在于Web容器內部,在具有轉發(fā)關系的Web組件之間共享。這兩個方法能夠設置Object類型的共享數(shù)據(jù)。

                  request.getParameter()取得是通過容器的實現(xiàn)來取得通過類似post,get等方式傳入的數(shù)據(jù)。

                  request.setAttribute()和getAttribute()只是在web容器內部流轉,僅僅是請求處理階段。

                  getAttribute是返回對象,getParameter返回字符串

                  總的來說:request.getAttribute()方法返回request范圍內存在的對象,而request.getParameter()方法是獲取http提交過來的數(shù)據(jù)。

          posted @ 2008-09-20 09:57 chenkai 閱讀(2792) | 評論 (0)編輯 收藏

          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統(tǒng)計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 香港 | 泸溪县| 平江县| 石台县| 图木舒克市| 平凉市| 安远县| 德江县| 康乐县| 固阳县| 高安市| 上虞市| 衡阳县| 盐亭县| 会同县| 余庆县| 夹江县| 南乐县| 安远县| 朝阳县| 平山县| 通道| 仁化县| 杭州市| 东城区| 平顺县| 赤水市| 土默特右旗| 门头沟区| 林州市| 页游| 隆化县| 通城县| 朝阳市| 德保县| 巴楚县| 商都县| 万安县| 辉县市| 开封县| 简阳市|