2008年8月9日

          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.

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

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


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

          /**
          *創建或者取回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!!就在這里了,把創建以后的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 閱讀(704) | 評論 (1)編輯 收藏

          Request的getParameter和getAttribute方法的區別

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

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

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

                (2)當兩個Web組件之間為鏈接關系時,被鏈接的組件通過getParameter()方法來獲得請求參數,例如假定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")方法來獲得請求參數username:

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

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

                  假定  authenticate.jsp和hello.jsp之間為轉發關系。authenticate.jsp希望向hello.jsp傳遞當前的用戶名字,  如何傳遞這一數據呢?先在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()方法傳遞的數據,會從Web客戶端傳到Web服務器端,代表HTTP請求數據。request.getParameter()方法返回String類型的數據。

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

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

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

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

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

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

          用javascript實現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;//取得引發事件的對象  
            while(headEventObject.tagName!="TR")   //不是tr行,則從底下的td冒泡上來尋找到相應行  
            {  
            headEventObject=headEventObject.parentElement;  
            }  
             
            for   (i=0;i<headEventObject.children.length;i++)  
            {   alert(headEventObject.children[i].tagName);
            if   (headEventObject.children[i]!=event.srcElement)//找到事件發生的td單元格  
            {  
            headEventObject.children[i].className='listTableHead';//把點擊的列的className屬性設為listTableHead  
            }  
            }  
             
            var   tableRows=0;  
            trObject=clearStart.children[0].children;   //取得表格中行對象,   原來這里叫DataTable,   可能是你寫錯了吧??  
            for   (i=0;i<trObject.length;i++)  
            {  
            Object=clearStart.children[0].children[i];//取得每行的對象  
            tableRows=(trObject[i].id=='ignore')?tableRows:tableRows+1;//如果不是忽略行,則行數加一  
            }  
             
            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;//把行放在數組里  
            tdinnerHTML[i0]=trObject[i].children[colNum].innerHTML;//把要排序的行中td的內容放數組里  
            tdNumber[i0]=i;//行號  
            i0++;//加一,下個循環用  
            }  
            }  
            sourceHTML=clearStart.children[0].outerHTML;//取得表格中所有tr的html代碼  
             
            //對所有td中的字符串進行排序,   算不算冒泡排序???  
            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的內容存在showshow字串里  
            numshow=numshow+tdNumber[i]+'|';             //把排序好的相應的行號也存在numshow中  
            }  
             
            sourceHTML_head=sourceHTML.split("<TBODY>");//從<TBODY>截斷,我試了,前頭串為空  
             
            numshow=numshow.split("|");  
            var   trRebuildHTML='';  
            if   (event.srcElement.className=='listHeadClicked')  
            {//已點擊的列,   則逆排  
            for   (i=0;i<tableRows;i++)  
            {  
            trRebuildHTML=trRebuildHTML+trObject[numshow[tableRows-1-i]].outerHTML;//取出排序好的tr的內容連接起來  
             
            }  
            event.srcElement.className='listHeadClicked0';  
            }  
            else  
            {//默認順排,新點擊順排  
            for   (i=0;i<tableRows;i++)  
            {  
            trRebuildHTML=trRebuildHTML+trObject[numshow[i]].outerHTML;  
            }  
            event.srcElement.className='listHeadClicked';  
            }  
            //取得排序后的tr集合結果字符串  
            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>
             公務員
            </td>
            <td>
             22
            </td>
           </tr>
           <tr>
            <td>
             張三
            </td>
            <td>
             研究員
            </td>
            <td>
             65
            </td>
           </tr>
           <tr>
            <td>
             李思
            </td>
            <td>
             科學家
            </td>
            <td>
             24
            </td>
           </tr>
           <tr>
            <td>
            王武
            </td>
            <td>
             社會學家
            </td>
            <td>
             38
            </td>
           </tr>
          </table>
          </body></html>

           

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

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

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 澄城县| 吐鲁番市| 商河县| 芷江| 崇文区| 全椒县| 山丹县| 沅江市| 南京市| 石门县| 日土县| 容城县| 石城县| 阳春市| 阳朔县| 宜都市| 崇左市| 灌阳县| 萍乡市| 霍邱县| 宜阳县| 鹰潭市| 翁牛特旗| 额济纳旗| 郴州市| 左权县| 定结县| 乐陵市| 奈曼旗| 桐梓县| 武平县| 泽库县| 孟州市| 台南县| 巴林右旗| 荔浦县| 凤庆县| 客服| 泽州县| 河间市| 东乡县|