struts比較常用的3個標籤庫(一)

來源:互聯網
上載者:User

      腳踏實地,一點一點的積累吧。搞懂基礎、搞懂原理。3個標籤庫的引入:

Code:
  1. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>    
  2. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>   
  3. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>  

struts實現串連的幾種方法:(由MyJsp1.jsp 轉到 MyJsp2.jsp)

<MyJsp1.jsp>

Code:
  1. <%@ page language="java" import=" java.util.HashMap "   pageEncoding="gb2312"%>   
  2. <%@page import="po.Student"%>   
  3.   
  4. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>   
  5. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html:html lang="true">   
  9.   <head>   
  10.     <title>MyJsp1.jsp</title>   
  11.   </head>   
  12.      
  13.   <body>   
  14.               這是第一個JSP介面! <br>   
  15.       <!-- 傳統的連結做法 -->   
  16.       <a href="MyJsp2.jsp">連結1</a><br>   
  17.       <!-- page使用的是絕對路徑 -->   
  18.       <html:link page="/MyJsp2.jsp">連結2</html:link><br>   
  19.       <!-- href 使用的是相對路徑 -->   
  20.       <html:link href="MyJsp2.jsp">連結3</html:link><br>   
  21.       <!-- 使用forward需要修改設定檔,修改配置就需要重新部署 -->   
  22.       <html:link forward="toOtherPage">連結4</html:link><br>   
  23.       <!-- 使用用於傳參數的情況 -->   
  24.       <html:link action="xxx.do">串連5(同時還做一些操作的情況)</html:link><br>   
  25.       <HR>   
  26.       <!-- 參數傳遞操作。 -->   
  27.       <html:link page="/MyJsp2.jsp?param=007"  > 參數的傳遞(?param=007)最傳統的</html:link>   
  28.       <BR>   
  29.       <%   
  30.         //1   
  31.          session.setAttribute("msg","007");     //通過msg        
  32.          //比較那個會話會比較先被查到   
  33.          request.setAttribute("msg","<b>007_request</b>");   
  34.          application.setAttribute("msg","007_applicatioin");   
  35.          //2   
  36.         Student stu=new Student();   
  37.         stu.setStuId("<b>071402119</b>");   
  38.         session.setAttribute("stu",stu);   //現需要將該對象的ID傳到第二個介面。   
  39.             
  40.          //3多個參數的情況   
  41.          HashMap hm=new HashMap();   
  42.          hm.put("param1","0001");   
  43.          hm.put("param2","002");   
  44.          hm.put("param3","003");   
  45.          session.setAttribute("paramList",hm);   
  46.             
  47.          //測試Cookie 標籤只提供讀不提供寫   
  48.          response.addCookie(new Cookie("cname","biao"));  //傳到第二個頁面的Cookie   
  49.             
  50.        %>   
  51.       <html:link page="/MyJsp2.jsp" paramId="param" paramName="msg" > 參數的傳遞(paramId="param" paramName="msg") </html:link><BR>   
  52.       <html:link page="/MyJsp2.jsp" paramId="param" paramName="stu"  paramProperty="stuId" > 參數的傳遞學號</html:link><BR>   
  53.     <br>   
  54.     <html:link page="/MyJsp2.jsp" name="paramList">多參數</html:link>   
  55.   </body>   
  56. </html:html>  

<MyJsp2.jsp>

Code:
  1. <!--部分代碼-->   
  2. ody>     
  3.             這是第二個JSP介面! <br>   
  4.            <!--  request.getParameter("param") -->   
  5.             <!-- 等價於如下 -->   
  6.             <% String str=request.getParameter("param");   
  7.                out.print(str);   
  8.                //使得jsp頁面中盡量的減少Java代碼   
  9.             %>   
  10.             <br>   
  11.             <bean:parameter id="str2" name="param"/>   
  12.             <bean:write name="str2" filter="false"/>   
  13.             //顯示JavaBean裡面的屬性值是   
  14.             <bean:write name="stu" property="stuId"/><br>            
  15.             <bean:cookie id="myselfname" name="cname" />   
  16.             <bean:write name="myselfname" /><br>   
  17.                <bean:write name="myselfname" property="value"/><br>   
  18.                
  19. </body>  

     struts的版本是struts1.2  。單純的標籤學習,沒有Actiong和ActionForm。其中的JavaBean:Student的屬性輸入。

Code:
  1. //封裝一個學生資料   
  2. public class Student {   
  3.     private String stuId;   
  4.     private String stuName;   
  5.     private String stuSex;   
  6.     private String stuBir;   
  7.     private String stuAdd;   
  8.        
  9.     //添加集合 ,作為遍曆使用   
  10.     private ArrayList list;   
  11.     public ArrayList getList() {   
  12.         return list;   
  13.     }   
  14.   //get and set 函數省略。   
  15. }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.