struts標籤-參數傳遞測試(二)

來源:互聯網
上載者:User

     參數的傳遞,此例子為方便學習只從MyJsp3.jsp介面中傳到Myjsp4.jsp中顯示。就目前為止,個人接觸的資料交換大概有兩種情況:一是從jsp頁面中,提交表單給Servlet,Servlet執行個體化JavaBean,並與資料庫互動,然後將所得的結果傳回jsp頁面;二是直接調用Servlet與資料庫互動,取出資料,將資料(參數)傳回jsp介面。 此處所說的Servlet在struts中則是Action部分的操作了。

       而無論是從jsp頁面到jsp頁面,還是Servlet(Action)到jsp頁面,個人覺得其傳參數(資料)的原理是一樣的。都是通過request,session。。。來傳遞。現把相關標籤代碼帖下:

<MyJsp3.jsp>

Code:
  1. <body>   
  2.               這是第三個JSP介面! <br>       
  3.       <!-- page使用的是絕對路徑 -->   
  4.       <html:link page="/MyJsp4.jsp">連結</html:link><br>       
  5.       <%   
  6.         //簡單集合的傳遞   
  7.         ArrayList list=new ArrayList();   
  8.         list.add("楊龍");   
  9.         list.add("好孩子");   
  10.         list.add("goodbye");   
  11.         session.setAttribute("boys",list);   
  12.            
  13.         //  集合裡面含有JavaBean。   
  14.         ArrayList stus=new ArrayList();   
  15.         Student stu1=new Student(); stu1.setStuId("999"); stus.add(stu1);  //為方便,暫時不規範   
  16.         Student stu2=new Student(); stu2.setStuId("888"); stus.add(stu2);   
  17.         Student stu3=new Student(); stu3.setStuId("777"); stus.add(stu3);   
  18.         session.setAttribute("stus",stus);   
  19.          
  20.       //JavaBean裡面含有集合的遍曆   
  21.       Student student=new Student();   
  22.       ArrayList myList=new ArrayList();   
  23.       myList.add("1596475");   
  24.       myList.add("1586475");   
  25.       student.setList(myList);     
  26.       session.setAttribute("student",student);   
  27.          
  28.       //複雜集合的遍曆   
  29.          HashMap hm=new HashMap();   
  30.          hm.put("param1","0001");   
  31.          hm.put("param2","002");   
  32.          hm.put("param3","003");   
  33.          session.setAttribute("paramList",hm);   
  34.       %>      
  35.   </body>  

<MyJsp4.jsp>

Code:
  1. <body>     
  2.             這是第四個JSP介面! <hr>   
  3.             簡單的集合遍曆:   
  4.        <logic:iterate id="boy" name="boys">   
  5.         <bean:write name="boy"/>   
  6.        </logic:iterate>  <hr>   
  7.        集合裡面含有JavaBean,遍曆:   
  8.        <logic:iterate id="stu" name="stus">   
  9.         <bean:write name="stu" property="stuId"/> <!-- 表示遍曆的是JavaBean裡面的stuId屬性 -->   
  10.        </logic:iterate><hr>   
  11.           
  12.        JavaBean裡面含有集合的遍曆(遍曆集合)   
  13.        <logic:iterate id="stu" name="student" property="list"> <!-- 表示遍曆的是JavaBean裡面的集合 -->   
  14.          <bean:write name="stu" />   
  15.        </logic:iterate><hr>   
  16.           
  17.        HashMap的遍曆(包括key和value)   
  18.        <logic:iterate id="element" name="paramList"><!-- 遍曆每個元素 -->   
  19.        <bean:write name="element" property="key" />  <!-- 遍曆key -->   
  20.        <bean:write name="element" property="value" />     
  21.        </logic:iterate>                     
  22. </body>  

 Student類的屬性如上篇筆記所示。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.