Struts 1 之<bean>標籤庫,strutsbean

來源:互聯網
上載者:User

Struts 1 之<bean>標籤庫,strutsbean

<bean:write>標籤

<bean:write>能輸出request、session、application、page四個域中能訪問到的變數,與EL運算式不同的是,如果變數不存在或者訪問不到,會拋出異常

<bean:write>常用的屬性有如下幾個:

1、name,用來指定屬性的名字

2、filter,用來指定是否屏蔽到屬性值的HTML格式

3、property,用來指定name所代表的對象的屬性名稱字

4、format,用來指定顯示的時間,數字,日期等的格式

例1:

某處設定了request.setAttribute("hello","helloworld");

則在某個jsp頁面中,用struts的write標籤取出並顯示的方式如下:

<bean:writename="hello"/>,則頁面上顯示出hello world。

例2:

某處設定了request.setAttribute("bj","<fontcolor='red>歡迎你</font>");

則在某個jsp頁面中,用struts的write標籤取出並按紅色的方式顯示的方式如下:

<bean:writename="bj" filter="false"/>,則頁面上顯示出紅色的歡迎你。如果filter屬性不設定為false,則預設為true,那麼顯示出的內容就為<fontcolor='red>歡迎你</font>。

例3:

某處設定了request.setAttribute("date",newDate());

則在某個jsp頁面中,用struts的write標籤取出並按指定方式顯示日期的方法如下:

<bean:writename=“date”/>,此為預設的顯示方法,顯示出的時間為:

Fri Mar 2815:04:21 CST 2008

<bean:writename=“date” format="yyyy-MM-dd HH:mm:ss"/>,此為自己指定日期的顯示格式,顯示出的時間為2008-3-28 15:04:21

例4:

某處設定了request.setAttribute("n",“1223333.333”);

則在某個jsp頁面中,用struts的write標籤取出並按指定方式顯示數位方法如下:

<bean:writename=“n”/>,此為預設的顯示方法,顯示出的數字位1223333.333

<bean:writename=“n” format="###,###.####"/>,此為自己指定數位顯示格式,顯示出的時間為1,223,333.333

如果希望小數點後的不足四位時,缺位補0,則應

<bean:writename=“n” format="###,###.0000"/>,此為自己指定數位顯示格式,顯示出的時間為1,223,333.3330

例5:

假如有User類和Groupe類,User類有屬性名稱字userName,年齡age,性別sex和所屬的Groupe,Groupe類有屬性群組名groupeName,並均具有相應的get和set方法。

某處設定了request.setAttribute("user",newUser("張三","20","男",new Groupe("三組")));

則在某個jsp頁面中,用struts的write標籤取出並按指定方式顯示結構體的方法如下:

使用者名稱:<input type="text" value="<bean:write name="user" property="userName"/>">年齡:<input type="text" value="<bean:write name="user" property="age"/>">性別:<input type="text" value="<bean:write name="user" property="sex"/>">組名:<input type="text" value="<bean:write name="user" property="groupe.groupeName"/>">

<bean:cookie>、<bean:parameter>、<bean:header>標籤

<bean:cookie>、<bean:parameter>、<bean:header>標籤用於擷取cookie、地址欄參數、header中的對象,這幾個標籤不能輸出,只能將擷取到的對象儲存到另一個變數中,供其他標籤使用

<bean:cookie id="sessionid" name="JSESSIONID"/> <!--擷取cookieJSESSIONID放進名為sessionid的變數中--><bean:write name="sessionid" property="value"/> <bean:parameter id="action1" name="action2"/>  <!--擷取地址欄名為action2的參數,放入名為action1的變數中--><bean:write name="action1" /> <bean:header id="host1" name="host"/>  <!--擷取Header參數host,放入變數host1--><bean:write name="host1" />

<bean:message>標籤

<bean:message>標籤用於輸出ResourceBundle中的一條訊息.<bean:message>標籤的bundle屬性指定Resource Bundle,它和Struts設定檔的<message:resource>元素的可以屬性相匹配.如果沒有設定bundle屬性,就採用預設的ResourceBundle。

 

Beantaglibs應用的Struts設定檔(Struts-config.xml)中配置了兩個ResourceBundle:

<message-resources parameter="ApplicationResource" /><message-resources parameter="SpecialResouces" key="special"/>
 

第一個Resource bundle 沒有指定key屬性,因此預設的ResourceBundle,它的資源檔ApplicationResource.properties,在這個檔案中定義一條訊息:

  .hello=Hello,{0}

第二個Resource bundle指定key屬性為”special”,它的資源檔為SpecialResources.properties,這個檔案中定義了一條訊息:

  .hello=Hello,everyone!

 在<bean:message>標籤中指定訊息key有三種方式:

 1.<bean:message>標籤的key屬性直接指定訊息key,例如:

    

<bean:message bundle="special" key="hello"/>

 2.<bean:message>標籤的name屬性指定一個可以轉化為字串的變數,這個變數的字串值為訊息key,例如:

<%              //這個hello要對應到資源檔中的hello              Request.setAttribute("StringBean","hello");%> <bean:message bundle="special" name="stringBean"/>

 3.同時指定<bean:message>標籤的name屬性和property屬性.name屬性指定一個JavaBean,property屬性指定JavaBean的一個屬性,這個JavaBean的屬性的值就是訊息的key,例如

 

<%              SomeBean bean = new SomeBean();               Bean.setName("hello");               Request.setAttribute("someBean",bean);%> <bean:message bundle="special" name="someBean" property="name"/>

 上面定義一個SomeBean類型的someBean變數,它的name屬性為”hello”,因此訊息的key為”hello”

 

對於帶參數的複合訊息,可以使用Bean:message標籤中的arg0,arg1等屬性來設定參數值

 

<bean:message key="hello" arg0="Linda"/>

 資源檔ApplicationResources.properties

 

welcome =<h1>歡迎光臨!!!!!!</h1>welcome1 =<h1>歡迎 {0} 光臨!!!!!!</h1>

<bean:resource>標籤

<bean:resource>標籤用於檢索Web資源的內容,它具有以下屬性:

1.  id屬性:定義一個代表Web資源的變數。

2.  name屬性:指定Web資源的路徑。

3.  input屬性:如果沒有設定input屬性,則id屬性定義的變數為字串類型;如果給input屬性設定了值(可以是任一字元串),則id屬性定義的變數為java.io.InputStream類型。

 

<bean:struts>標籤

<bean:struts>標籤用於檢索Struts架構內在的對象,如ActionFormBean、ActionForward或ActionMapping。<bean:struts>標籤的id屬性定義一個page範圍的變數,用來引用Struts架構的內在對象。必須設定formbean、forward或mapping屬性中的一個屬性,來指定被引用的Struts內在對象:

1.  formbean屬性:指定ActionFormBean對象,和Struts設定檔的<form-bean>元素匹配。

2.  forward屬性:指定ActionForward對象,和Struts設定檔的<global-forwards>元素的<forward>子項目匹配。

3.  mapping屬性:指定ActionMapping對象,和Struts設定檔的<action>元素匹配。

 

<bean:include>標籤

<bean:include>標籤和標準的JSP標籤<jsp:include>很相似,都可以用來包含其他Web資源的內容,區別在於<bean:include>標籤把其他Web資源的內容存放在一個變數中,而不是直接顯示到網頁上。<bean:include>標籤的id屬性定義一個代表其他Web資源的變數。

可以通過<bean:include>標籤的forward、page或href屬性來指定其他Web資源:

1.  forward:指定全域轉寄路徑,和Struts設定檔的<global-forwards>元素中的<forward>子項目匹配。

2.  page:指定相對於當前應用的URI,以“/”開頭。

3.  href:指定完整的URL。


聯繫我們

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