Struts之bean標(biāo)簽庫詳解

          Struts Bean標(biāo)簽庫中的標(biāo)簽可以訪問已存在的JavaBean以及它們的屬性,還可以定義新的Bean,把它存放在page范圍內(nèi)或用戶指定的范圍內(nèi),供網(wǎng)頁其他元素訪問。Bean標(biāo)簽庫大概分為三類:

          • 用于訪問HTTP請求信息或JSP隱含對象的Bean標(biāo)簽
          • 用于訪問Web應(yīng)用資源的Bean標(biāo)簽
          • 用于定義或者輸出JavaBean的Bean標(biāo)簽

          訪問HTTP請求信息或JSP隱含對象

          • <bean:cookie>:訪問Cookie信息
          • <bean:header>:訪問HTTP請求中的Header信息
          • <bean:parameter>;訪問請求參數(shù)。
          • <bean:page>:訪問JSP隱含對象。

          1.<bean:cookie>標(biāo)簽

          該標(biāo)簽可以檢索保存在瀏覽器中的Cookie,具有屬性id,name,multiple,value,id是對cookie在該頁面中的唯一標(biāo)識,在頁面其它地方可以通過id進(jìn)行引用。相當(dāng)于用cookie直接引用。Name是相應(yīng)的cookie名稱。Multiple可以賦予任意值,如果設(shè)置了該屬性,可以檢索出所有和Cookie名字匹配的Cookie名字匹配的Cookie,此時,id屬性定義了一個Cookie數(shù)組類型的變量,而不是單個Cookie類型的變量。例如:

          <bean:cookie id="myCookie" name="tags/cookiedemo" value="ddddddddd"/>
          <bean:write name="myCookie" property="name"/>
          <bean:write name="myCookie" property="value"/>
          <%

          String names=myCookie.getName();
          String value=myCookie.getValue();
          out.println("this cookie name is "+names+",value is "+value+"");

          %>

          2.<bean:header>標(biāo)簽

          該標(biāo)簽用于檢索HTTP請求中的Header信息。如果沒有指定multiple屬性則依據(jù)剛?cè)』氐闹祫?chuàng)建一個String類型的bean。如果指定了multiple屬性則依據(jù)剛?cè)』氐闹祫?chuàng)建一個String[]類型的數(shù)組。例如:

          <logic:present header="User-Agent">
          <!– 其它標(biāo)簽通過綁定到page作用域中的屬性使用該值 –>
          您的瀏覽器是<bean:header id="userAgent" name="User-Agent"/>
          <bean:write name="userAgent" /><br/>
          <!– JSP腳本通過scripting變量使用該值 –>
          <%
          out.println("您的瀏覽器是"+userAgent+"。<br/>");
          %>
          </logic:present>
          <%
          String id = "userAgent";
          String name = "User-Agent";
          String value = ((HttpServletRequest)pageContext.getRequest()).getHeader(name);
          out.println("============="+value);

          %>

          3.<bean:parameter>標(biāo)簽

          該標(biāo)簽用于檢索HTTP請求參數(shù),具有以下屬性:
          id屬性:定義一個java.lang.String類型的變量,這個變量存放在page范圍內(nèi)。
          name屬性:指定請求參數(shù)名。
          value屬性:請求指定參數(shù)的默認(rèn)值

          如果沒有指定multiple屬性則依據(jù)剛?cè)』氐闹祫?chuàng)建一個String類型的bean。如果指定了multiple屬性則依據(jù)剛?cè)』氐闹祫?chuàng)建一個String[]類型的數(shù)組。例如:

          <html:link page="/parameter2.jsptestString=this+is+a+test&testInt=123456">
          請求參數(shù)
          </html:link>

          <bean:parameter id="test1" name="testString" value="" />
          The first test is: <bean:write name="test1" />

          <bean:parameter id="test2" name="testInt" value=""/>
          The second test is:<bean:write name="test2"/>

          4.<bean:page>標(biāo)簽

          該標(biāo)簽用于檢索獲取JSP隱含對象,如session、request和response等,具有以下屬性:
          id屬性:定義了一個引用隱含對象的變量,這個變量存放在page范圍
          property屬性:指定隱含對象的名字,可選值包括application,config,request,response,session 例如:

          <bean:page id="mySession" property="session"/>

          <%
          out.println("Session time:"+mySession.getCreationTime());

          %>

          <bean:message>:顯示Resource Bundle中的消息。
          <bean:resource>;把Web資源裝載到一個JavaBean中。
          <bean:struts>;訪問Struts的內(nèi)在配置對象。
          <bean:include>;包含一個web資源。

          1.<bean:include>標(biāo)簽
          該標(biāo)簽用語將其他web資源包含進(jìn)當(dāng)前頁面中,和標(biāo)準(zhǔn)的JSP標(biāo)簽<jsp:include>很類似,都可以用來包含其他Web資源的內(nèi)容,區(qū)別在于<bean:include>標(biāo)簽把其他的Web資源的內(nèi)容存放在一個對象中,而不是直接顯示到網(wǎng)頁,<bean:include>標(biāo)簽的id屬性定義一個代表其他Web資源的變量。
          <bean:include id="value" page="/index.jsp"/> //在當(dāng)前資源中
          <bean:write name="value" filter="false"/><br/>
          // filter為true則為原文件
          <%
          out.println(value);
          %>
          2.<bean:message>標(biāo)簽
          該標(biāo)簽用與顯示資源文件中的消息文本。該標(biāo)簽有一個bundle屬性,它和struts-config.xml文件中的messsge-resources標(biāo)記內(nèi)的key屬性對應(yīng)。
          <message-resources parameter="com.resources" key="beanmessage">
          //struts-config
          message1=this is a test!
          message2=this is a test too!
          //resource中
          <bean:message bundle="beanmessage" key="message1"/>
          //jsp中
          3.<bean:resource>標(biāo)簽
          該標(biāo)簽用于檢索、獲得Web資源內(nèi)容,如:網(wǎng)頁的源代碼。該標(biāo)簽的屬性有:id有其他標(biāo)簽一樣,name指定Web資源的路徑,input如果沒有設(shè)置,則id屬性默認(rèn)為一個字符類型對象,如果給input賦值yes,則id屬性所定義的對象為java.IputStream。例如:
          <bean:resource id="indexpage" name="/index.jsp" input="yes"/>
          <bean:write name="indexpage" filter="false"/>
          4.<bean:struts>標(biāo)簽
          該標(biāo)簽用于獲取Struts框架內(nèi)的一些對象,如AationForm和ActionForward等。。<bean:struts>標(biāo)簽的id屬性定義一個page范圍的變量,用來引用Struts框架的內(nèi)在對象,必需設(shè)置formbean,forward,mapping屬性中的一個屬性,來指定被引用的Struts內(nèi)在對象。
          formbean屬性:指定ActionFormBean對象,和struts配置文件的<form-bean>元素匹配。
          forward屬性;指定ActionForward對象,和配置文件的<global-forwards>元素的<forward>子元素匹配。
          mapping屬性:指定ActionMapping對象,和配置文件的<action>元素匹配
          例如:
          <bean:struts id="listFormBean" formBean="listForm"/>
          name:<bean:write name="listFormBean" property="name"/><br/>
          type:<bean:write name="listFormBean" property="type"/><br/>
          dynamic:<bean:write name="listFormBean" property="dynamic"/><br/>

           

          <bean:define>:用于定義一個變量。
          <bean:write>: 顯示JavaBean屬性的內(nèi)容。
          <bean:size>: 獲得集合對象或數(shù)組對象的長度。
          1.<bean:define>標(biāo)簽
          該標(biāo)簽用于定義一個變量,id屬性指定變量的名字,toScope屬性指定這個變量的存放范圍,如果沒有設(shè)置,則這個變量存放在page范圍內(nèi),給id屬性定義的變量賦值有三種方式:
          第一種是,通過value屬性直接賦值;
          第二種是,通過name和porperty共同指定一個變量來給id所定義的變量,name——bean,porperty——屬性;
          第三種是,通過type屬性和name聯(lián)合指定id所定義的變量類型,type——id定義變量的完整類型,name——存在的javaBean。例如:
          <bean:define id="string" value="this is a test"/>
          <bean:write name="string"/>–%>
          <%
          Date d = new Date();
          pageContext.setAttribute("currDate",d);
          %>
          <bean:define id="milliseconds" name="currDate" property="time"/>
          當(dāng)前時間距離1970年1月1日的毫秒數(shù)為:<bean:write name="milliseconds"/>
          2.<bean:size>標(biāo)簽
          該標(biāo)簽用于獲取集合或者數(shù)組的長度。
          id屬性定義一個Integer類型的變量,那么屬性指定已經(jīng)存在的Map,Collection或數(shù)組變量,id屬性定義的變量值為Map,collection或數(shù)組的長度。
          name屬性為對象名字。例如:

          <%
          ArrayList testlist=new ArrayList();
          testlist.add(new Integer(1));
          testlist.add(new Integer(2));
          testlist.add(new Integer(3));
          pageContext.setAttribute("listforcount",testlist);
          %>
          <bean:size id="size" name="listforcount"/>
          長度為:<bean:write name="size"/>
          3.<bean:write>標(biāo)簽
          該標(biāo)簽用于輸出各種類型的對象,有點類似與System.out.println()。例如:
          <%
          String a = "string for test";
          pageContext.setAttribute("test",a);
          %>
          <bean:write name="test"/>

          posted on 2011-02-22 09:10 小羅 閱讀(260) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2011年2月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272812345
          6789101112

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Web Framework

          常上的技術(shù)網(wǎng)站

          查找資料的java網(wǎng)站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 奉新县| 雅安市| 肃南| 临江市| 玉门市| 芜湖县| 师宗县| 泰兴市| 莒南县| 保靖县| 堆龙德庆县| 盱眙县| 南京市| 鹿邑县| 民丰县| 垫江县| 城口县| 永兴县| 金湖县| 策勒县| 贵阳市| 黑龙江省| 板桥市| 佛山市| 泰来县| 巫溪县| 涿州市| 重庆市| 镇赉县| 岳池县| 孝义市| 新建县| 平安县| 巴林左旗| 越西县| 彰化县| 靖州| 青铜峡市| 伊宁县| 礼泉县| 万源市|