2008年8月9日

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

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

           2、 在《Programming Jakarta Struts》這本書中的第四章“Configuring the Struts Application”中這樣一段說明來分別闡述這兩
          個(gè)屬性:(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ū)分這兩者。不過在仔細(xì)看過struts的源代碼以后,豁然開朗。。。

          下面主要對(duì)attribute進(jìn)行解釋,應(yīng)為沒有人會(huì)對(duì)name屬性不了解的(呵呵。。。)


          解釋:在struts實(shí)例化actionform的時(shí)候,有兩種情況:如果已經(jīng)存在,那么從內(nèi)存中取回;如果第一次實(shí)例化,那么創(chuàng)建,并放入內(nèi)存。
          這樣就有一個(gè)問題了,struts是根據(jù)什么來取回并創(chuàng)建actionform的呢,答案就是attribute的值。讓我們進(jìn)入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);
          }
          。。。
          。。。


          }


          下面又有一個(gè)問題浮出水面:如果我沒有在action mapping中指定attribute呢,那struts 是如何解決的?
          答案很簡(jiǎn)單,如果單從結(jié)果上看,此時(shí)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!!!!就在這里,看到了吧,如果你沒有設(shè)定attribute,那么struts 會(huì)把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) | 評(píng)論 (1)編輯 收藏

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

          當(dāng)兩個(gè)Web組件之間為轉(zhuǎn)發(fā)關(guān)系時(shí),轉(zhuǎn)發(fā)源會(huì)將要共享 request范圍內(nèi)的數(shù)據(jù)先用setAttribute將數(shù)據(jù)放入到HttpServletRequest對(duì)象中,然后轉(zhuǎn)發(fā)目標(biāo)通過 getAttribute方法來取得要共享的數(shù)據(jù)。而MVC中用的就是Web組件之間的轉(zhuǎn)發(fā)?。≌媸潜浚趺串?dāng)時(shí)沒有想到呢?

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

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

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

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

                或者:

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

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

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

                 (3)當(dāng)兩個(gè)Web組件之間為轉(zhuǎn)發(fā)關(guān)系時(shí),轉(zhuǎn)發(fā)目標(biāo)組件通過getAttribute()方法來和轉(zhuǎn)發(fā)源組件共享request范圍內(nèi)的數(shù)據(jù)。

                  假定  authenticate.jsp和hello.jsp之間為轉(zhuǎn)發(fā)關(guān)系。authenticate.jsp希望向hello.jsp傳遞當(dāng)前的用戶名字,  如何傳遞這一數(shù)據(jù)呢?先在authenticate.jsp中調(diào)用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ù),會(huì)從Web客戶端傳到Web服務(wù)器端,代表HTTP請(qǐng)求數(shù)據(jù)。request.getParameter()方法返回String類型的數(shù)據(jù)。

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

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

                  request.setAttribute()和getAttribute()只是在web容器內(nèi)部流轉(zhuǎn),僅僅是請(qǐng)求處理階段。

                  getAttribute是返回對(duì)象,getParameter返回字符串

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

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

          用javascript實(shí)現(xiàn)table的排序

          <%@page
          language="java"
          contentType="text/html;charset=GBK"
          %>

          <html>
          <head>
              <title>MyHtml.html</title>
              <meta http-equiv="content-type" content="text/html; charset=gbk">
             
              <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

             <script  language="javascript">

           

          function   JM_PowerList(colNum)  
            {  
            headEventObject=event.srcElement;//取得引發(fā)事件的對(duì)象  
            while(headEventObject.tagName!="TR")   //不是tr行,則從底下的td冒泡上來尋找到相應(yīng)行  
            {  
            headEventObject=headEventObject.parentElement;  
            }  
             
            for   (i=0;i<headEventObject.children.length;i++)  
            {   alert(headEventObject.children[i].tagName);
            if   (headEventObject.children[i]!=event.srcElement)//找到事件發(fā)生的td單元格  
            {  
            headEventObject.children[i].className='listTableHead';//把點(diǎn)擊的列的className屬性設(shè)為listTableHead  
            }  
            }  
             
            var   tableRows=0;  
            trObject=clearStart.children[0].children;   //取得表格中行對(duì)象,   原來這里叫DataTable,   可能是你寫錯(cuò)了吧??  
            for   (i=0;i<trObject.length;i++)  
            {  
            Object=clearStart.children[0].children[i];//取得每行的對(duì)象  
            tableRows=(trObject[i].id=='ignore')?tableRows:tableRows+1;//如果不是忽略行,則行數(shù)加一  
            }  
             
            var   trinnerHTML=new   Array(tableRows);    
            var   tdinnerHTML=new   Array(tableRows);  
            var   tdNumber=new   Array(tableRows)  
            var   i0=0  
            var   i1=0  
            for   (i=0;i<trObject.length;i++)  
            {  
            if   (trObject[i].id!='ignore')  
            {  
            trinnerHTML[i0]=trObject[i].innerHTML;//把行放在數(shù)組里  
            tdinnerHTML[i0]=trObject[i].children[colNum].innerHTML;//把要排序的行中td的內(nèi)容放數(shù)組里  
            tdNumber[i0]=i;//行號(hào)  
            i0++;//加一,下個(gè)循環(huán)用  
            }  
            }  
            sourceHTML=clearStart.children[0].outerHTML;//取得表格中所有tr的html代碼  
             
            //對(duì)所有td中的字符串進(jìn)行排序,   算不算冒泡排序???  
            for   (bi=0;bi<tableRows;bi++)  
            {  
            for   (i=0;i<tableRows;i++)  
            {  
            if(tdinnerHTML[i]>tdinnerHTML[i+1])  
            {  
            t_s=tdNumber[i+1];  
            t_b=tdNumber[i];  
            tdNumber[i+1]=t_b;  
            tdNumber[i]=t_s;  
            temp_small=tdinnerHTML[i+1];  
            temp_big=tdinnerHTML[i];  
            tdinnerHTML[i+1]=temp_big;  
            tdinnerHTML[i]=temp_small;  
            }  
            }  
            }  
             
             
             
            var   showshow='';  
            var   numshow='';  
            for   (i=0;i<tableRows;i++)  
            {  
            showshow=showshow+tdinnerHTML[i]+' ';//把排序好的td的內(nèi)容存在showshow字串里  
            numshow=numshow+tdNumber[i]+'|';             //把排序好的相應(yīng)的行號(hào)也存在numshow中  
            }  
             
            sourceHTML_head=sourceHTML.split("<TBODY>");//從<TBODY>截?cái)?我試了,前頭串為空  
             
            numshow=numshow.split("|");  
            var   trRebuildHTML='';  
            if   (event.srcElement.className=='listHeadClicked')  
            {//已點(diǎn)擊的列,   則逆排  
            for   (i=0;i<tableRows;i++)  
            {  
            trRebuildHTML=trRebuildHTML+trObject[numshow[tableRows-1-i]].outerHTML;//取出排序好的tr的內(nèi)容連接起來  
             
            }  
            event.srcElement.className='listHeadClicked0';  
            }  
            else  
            {//默認(rèn)順排,新點(diǎn)擊順排  
            for   (i=0;i<tableRows;i++)  
            {  
            trRebuildHTML=trRebuildHTML+trObject[numshow[i]].outerHTML;  
            }  
            event.srcElement.className='listHeadClicked';  
            }  
            //取得排序后的tr集合結(jié)果字符串  
            var   DataRebuildTable='';  
            //把舊的表格頭和新的tr排序好的元素連接起來,   (修改了一下)  
            DataRebuildTable   =   "<table   border=1 width=100%  cellpadding=1 cellspacing=1 id='clearStart'><TBODY>"
                +   trObject[0].outerHTML   +   trRebuildHTML   +   "</TBODY>"   +      "</table>";  
            clearStart.outerHTML=DataRebuildTable;//表格用新串重新寫一次  
             
            }  

          </script>

            </head>
          <table border=1 id="clearStart">
           <tr bgcolor=cccccc id='ignore'>
            <td onclick="JM_PowerList(0)">列一
            </td>
            <td onclick="JM_PowerList(1)">
             列二
            </td>
            <td onclick="JM_PowerList(2)">
             列二
            </td>
           </tr>
            <tr>
            <td>
             周
            </td>
            <td>
             公務(wù)員
            </td>
            <td>
             22
            </td>
           </tr>
           <tr>
            <td>
             張三
            </td>
            <td>
             研究員
            </td>
            <td>
             65
            </td>
           </tr>
           <tr>
            <td>
             李思
            </td>
            <td>
             科學(xué)家
            </td>
            <td>
             24
            </td>
           </tr>
           <tr>
            <td>
            王武
            </td>
            <td>
             社會(huì)學(xué)家
            </td>
            <td>
             38
            </td>
           </tr>
          </table>
          </body></html>

           

          posted @ 2008-08-09 11:40 chenkai 閱讀(396) | 評(píng)論 (0)編輯 收藏

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 萝北县| 紫云| 舟曲县| 富裕县| 青州市| 洞头县| 东源县| 藁城市| 新郑市| 贵州省| 康平县| 阿图什市| 岳阳县| 筠连县| 聂拉木县| 台东县| 安福县| 孟州市| 武冈市| 凭祥市| 汉寿县| 枣阳市| 台州市| 本溪| 五华县| 高青县| 凉城县| 同德县| 陇川县| 闽清县| 通道| 伊川县| 祥云县| 扎兰屯市| 个旧市| 稷山县| 峡江县| 禹州市| 凭祥市| 湛江市| 临洮县|