Struts1 標籤庫 說明

Struts提供了五個標籤庫,即:HTML、Bean、Logic、Template和Nested。

HTML標籤 : 用來創建能夠和Struts 框架和其他相應的HTML 標籤交互的HTML 輸入表單

Bean標籤:  在訪問JavaBeans 及其屬性,以及定義一個新的bean 時使用

Logic標籤: 管理條件產生的輸出和對象集產生的循環

Template標籤:隨着Tiles框架包的出現,此標記已開始減少使用

Nested標籤:  增強對其他的Struts 標籤的嵌套使用的能力

 

標籤的公共特徵

>>styleId:命名自定義標籤創建時的腳本變量名。

>>name :指出關鍵字值,在該關鍵字下可以找到一個存在的bean 。如果給出了scope屬性,則僅僅在scope中查找。否則,根據標準的順序在各種scope中查找。標準順序爲 (page,request, session, or application)。 
>> property :指出bean 中的某個屬性,可以在其中檢索值。如果沒有標明,則使用對象本身的值。

>><text>標籤、<hidden>標籤、<textarea>標籤、<radio>標籤、<checkbox>標籤、<submit>標籤、<reset>標籤都有一個property屬性,最後會被轉換成HTML中的name屬性
>> scope :定義了Bean在哪個範圍(page, request, session, or application)中被查找。如果沒有標明按順序查找。腳本變量(見styleId)將在相同的範圍中創建。

 

說明:

Struts標籤也支持嵌套引用。

eg

property="foo.bar.baz"

這相當於進行下面的調用:

getFoo().getBar().getBaz()

或者做爲setter

getFoo().getBar().setBaz(value)

 

雖然Struts 標籤的設計原意是爲了避免使用scriptlet,scriptlet的表達式還能夠提供給所有的Struts 標籤使用。但請確保使用完整的表達式。

錯誤:

<html:linkhref="'<%= "/" + name %>/index.jsp>'>

正確:

<html:linkhref="'<%= "/" + name + "/index.jsp"%>'>    // 表達式必須提供整個屬性值

Html標籤庫

struts標籤使用舉例-HTML

html:base

表示所包含頁面的絕對位置。這個標籤只有內嵌在head標籤中才有效。

<html:base/>

此行代碼解析後:

<basehref=\"http://www.mymain.com/myStrutsApp/testing.jsp\">

htmlbase元素。Htmlbase的作用:

2、 html:cancel

該標籤生成一個取消按鈕。當點擊該按鈕後action servlet會繞過相應的form bean的validate()方法,同時將控制權交給相應的action。在action中可使用Action.isCancelled(HttpServletRequest)方法判斷是否被取消了。如果返回true表示這個action被取消了,否則表示這個action沒有被取消。
eg.  <html:cancel>取消</html:cancel>

3、html:form


1)  標籤中必須包含一個action屬性,它是這個標籤中唯一必需的屬性。如果不具備該屬性則JSP頁面會拋出一個異常。之後你必須給這個action屬性指定一個有效值。一個有效值是指應用程序的Struts配置文件中元素裏的任何一個子元素的訪問路徑。而且相應的元素中必須有一個name屬性,它的值是form bean的名稱。

<html:form action="/login.do" >

如果你有上述一個標籤 ,那麼你的Struts配置文件的元素中必須有一個如下顯示爲粗體的元素: 
<action-mappings> 
     <action path="/login" 
      type="com.javapro.struts.LoginAction" 
      name="loginForm"
      scope="request"
      input="/login.jsp">
      <forward name="success"path="/mainMenu.jsp"/>
    </action>

</action-mappings>

// 這就是說一個form標籤是和form bean相關聯的

2) 任何包含在<form>中用來接收用戶輸入的標籤(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)必須在相關的form bean中有一個指定的屬性值。比如,如果你有一個屬性值被指定爲“username”的<text>標籤,那麼相關的form bean中也必須有一個名爲“username”的屬性。輸入<text>標籤中的值會被用於生成form bean的userName屬性。

3)可以用focus屬性來生成JavaScript,它會“定焦”(focus)到該form所包含的一個元素上。使用focus屬性時你需要給它指定元素的名稱。

<body>

<html:form action="/login" focus="password">

User Name: <html:text property="userName"/><br>

Password: <html:text property="password"/><br>

<html:submit/>

</html:form>

</body>

代碼解析後:

<body>

<form name="loginForm"method="post"  action="/myStrutsApp/login.do">

User Name: <input type="text" name="userName"  value=""><br>

Password: <input type="text" name="password" value=""> <br>

<input type="submit"  value="Submit">

</form>

<script  type="text/javascript">

<!--

if (document.forms["loginForm"].elements["password"].type!= "hidden")

document.forms["loginForm"].elements["password"].focus()

// -->

</script>

</body>

4、html:select標籤

該標籤生成一個select元素。multiple屬性決定是否爲多選。如果指定了multiple="true"則爲多選,此時對應的屬性應該是一個數組。否則,此時對應的屬性應該是標量。

注意:爲了正確的處理未作選擇的情況,在ActionForm中的reset()方法中必須將標量屬性設置爲默認值而將數組的長度置爲0

另外的一個重要問題就是struts如何生成option元素了,這個任務struts交給了html:option、html:options和html:optionsCollection三個標籤。

html:option

該標籤生成一個HTML的option元素。該標籤必須嵌在html:select標籤中。它的顯示文本來自其標籤體,也可以來自於資源文件。

eg.

<html:optionvalue="red">red</html:option>

<html:optionvalue="blue">blue</html:option>

 

來自於資源文件:

<html:optionvalue="color1" bundle="htmlselect.Colors" key="htmlselect.red"/>

它和配置文件中的<message-resources>元素的key屬性匹配 --><message-resource parmeter="HtmlSelectColors"key="htmlselect.Colors"/>

<message-resource>中配置的資源文件爲HtmlSelectColors.properties,相關內容爲 htmlselect.red=RED

html:options

該標籤生成多個HTML的option元素。該標籤必須嵌在html:select標籤中。

指定collection屬性的方式舉例如下:

<html:selectname="selectForm" property="orgId" size="1">


<html:options

collection="orgCollection"(coll)

property="orgId"(value)

labelProperty="orgName"/>(label)


       </html:select>

注意:orgCollection是在某個引藏對象範圍(pageContext,rqeust,…)內存在的

未指定collection屬性方式的舉例如下:

<html:selectname="selectForm" property="orgId"size="1">     
     <html:options  property="orgIds"labelProperty="orgNames"/>  
  </html:select>

html:optionsCollection標籤

該標籤生成多個HTML的option元素。其功能和html:options標籤的相同。

<html:selectname="selectForm" property="orgIds"size="1">      
            <html:optionsCollection

name="selectForm"

property="orgs"

label="orgName"

value="orgId"/>   
     </html:select>

注意:orgs一定是在ActionForm裏面出現的一個Collection類型的Bean

5、html:checkbox標籤

該標籤生成checkbox元素

eg

<html:checkboxproperty="loves" value="bb"name="studentForm"/>bb

<html:checkboxproperty="loves" value="cc"name="studentForm"/>cc

<html:checkboxproperty="loves" value="dd"name="studentForm"/>dd

在名爲studentForm中有一個名爲loves的String類型的數組來與此標籤對應,這樣才能在studentForm中接收到該標籤的多選值

6、html:multibox標籤

該標籤用法同html:checkbox

8、html:radio標籤

該標籤生成radio元素

eg

<html:radioproperty="sex" name="studentForm" value="男"/>男

在名爲studentForm中有一個名爲sex的String類型的屬性來與此標籤對應,這樣才能在studentForm中接收到該標籤的單選值

9、html:img標籤

最重要的屬性page:圖象文件的路徑,前面必須帶有一個斜線。其它屬性:heignt、width、alt。


      <html:img

page="/logo.gif"height="50"  width="200" alt="Web Logo"/>

10、html:link標籤

<html:linkpage="/index.html">Click demo</html:link>

此行代碼解析後:

<ahref="/index.html">Click demo</a>

11、html:html標籤

它有兩個屬性:locale和xhtml,兩者都不是必需的。

<html:htmllocale=\"true\">

此行代碼解析後:

<htmllang=\"en\">

說明:

生成的結果取決於Struts應用程序所位於的服務器的locale。如果你將應用程序部署到一個不同locale的服務器,你不需要改變代碼,Locale會自動調整。

 

12、html:errors(不常用)

通過一個簡單的<html:errors/>標籤,你就可以在一個JSP頁面上顯示完全自定義的錯誤信息。功能超強大!!

說明:

這個標籤在Request對象的屬性集合中查找reserved key。如果它找到一個reserved key,它就假設這個key是一個String、或是一個String數組  (它包含在模塊的MessageResources中查找的message keys)、或是類型爲org.apache.struts.action.ActionErrors的一個對象。

如果在應用程序資源中存在相應的信息,那麼就可以用下面這些可選的message keys:

errors.header  or errors.prefix:相應的信息在錯誤信息的單獨列表前顯示。

errors.footer or  errors.suffix:相應的信息在錯誤信息的單獨列表後顯示。

13、html:password

eg:

<html:passwordproperty="password" redisplay="false"/>

該標籤中的一個很重要的屬性是"redisplay",它用於重新顯示以前輸入到這個區域中的值。該屬性的缺省值爲true。然而,爲了使password不能被重新顯示,你或許希望將該屬性的值設爲false。



struts標籤使用舉例-logic

logic:empty


該標籤是用來判斷是否爲空的。如果爲空,該標籤體中嵌入的內容就會被處理。該標籤用於以下情況:

1)當Java對象爲null時;
2)當String對象爲""時; 
3)當java.util.Collection對象中的isEmpty()返回true時;
4)當java.util.Map對象中的isEmpty()返回true時。
eg. 
< logic:empty   name="userList">   
             ...   
 < /logic:empty> 
 該句等同於:
 if   (userList.isEmpty())   {   
                ...   
 }

logic:notEmpty

該標籤的應用正好和logic:empty標籤相反,略。

logic:equal

該標籤爲等於比較符。
eg1. 比較用戶的狀態屬性是否1,若爲1,輸出"啓用";
 < logic:equal   name="user"  property="state"   value="1">
          啓用
 < /logic:equal>
eg2. 如果上例中的value值是動態獲得的,例如需要通過bean:write輸出,因struts不支持標籤嵌套,可採用EL來解決該問題。
<logic:equal   name="charge"  property="num"  value="${business.num}">   
                   ......
< /logic:equal>

logic:notEqual

該標籤意義與logic:equal相反,使用方法類似,略。

logic:forward

該標籤用於實現頁面導向,查找配置文件的全局forward。
eg. < logic:forward name="index"/>

logic:greaterEqual

爲大於等於比較符。
eg. 當某學生的成績大於等於90時,輸出“優秀”:
< logic:greaterEqual name="student" property="score"value="90">
                 優秀
< /logic:greaterEqual>

logic:greaterThan

此爲大於比較符,使用方法同logic:greaterEqual,略;

logic:lessEqual

此爲小於等於比較符,使用方法同logic:greaterEqual,略;

logic:lessThan

此爲小於比較符,使用方法同logic:greaterEqual,略;

logic:match

此標籤比較對象是否相等;
eg1. 檢查在request範圍內的name屬性是否包含"amigo"串: 
   < logic:match name="name"scope="request" value="amigo">
     < bean:write name="name"/>中有一個“amigo”串。
   < /logic:match>
eg2. 檢查在request範圍內的name屬性是否已“amigo”作爲起始字符串:
           < logic:matchname="name" scope="request" value="amigo"location="start">
              < bean:write name="name"/>以“amigo”作爲起始字符串。
            < /logic:match>
         eg3. 
            <logic:match header="user-agent" value="Windows">
              你運行的是Windows系統
            </logic:match>

logic:notMatch

此標籤用於比較對象是否不相同,與logic:match意義相反,使用方法類似,略。

logic:messagePresent

該標籤用於判斷ActionMessages/ActionErrors對象是否存在;
eg. 如果存在error信息,將其全部輸出:
    < logic:messagePresentproperty="error"> 
       < html:messagesproperty="error" id="errMsg" > 
           <bean:write name="errMsg"/> 
       < /html:messages>   
  < /logic:messagePresent >

logic:messagesNotPresent

該標籤用於判斷ActionMessages/ActionErrors對象是否不存在,使用方法與logic:messagePresent類似,略

logic:present

此標籤用於判斷request對象傳遞參數是否存在。
eg1. user對象和它的name屬性在request中都存在時,輸出相應字符串:
   < logic:present name="user"property="name">
            user對象和該對象的name屬性都存在
   < /logic:present> 
eg2. 若有一個名字爲“user”的JavaBean,輸出對應字符串:
    < logic:present name="user" >
       有一個名字爲“user”的JavaBean。
    < /logic:present>
eg3. 
    < logic:present header="user-agent">
        we got a user-agent header.
    < /logic:present>

logic:notPresent

此標籤用於判斷request對象傳遞參數是否不存在,意義與了logic:present相反,使用方法類似,略。

logic:redirect

該標籤用於實現頁面轉向,可傳遞參數。
eg1. < logic:redirect href="http://www.chinaitlab.com"/>

logic:iterator

用於顯示列表爲collection的值(List ,ArrayList,HashMap等)。
eg1. 逐一輸出用戶列表(userlList)中用戶的姓名:
< logic:iterate  id="user"name="userList">
     < bean:write name="user"property="name"/>< br>
< /logic:iterate>

eg2. 從用戶列表中輸出從1開始的兩個用戶的姓名:
< logic:iterate  id="user" name="userList"indexId="index"  offset="1" length="2">
   < bean:write name="index"/>.
   < bean:write name="user"property="name"/>< br>
 < /logic:iterate>
eg3. logic:iterator標籤的嵌套舉例
  < logic:iterate id="user" indexId="index"name="userList">
    < bean:write name="index"/>. 
    < bean:write name="user"property="name"/>< br>
    < logic:iterate id="address"name="user" property="addressList" length="3"offset="1">
     < bean:write name="address"/><br>
    < /logic:iterate>
  </logic:iterate>

 

struts標籤使用舉例-BEAN

bean:write

該標籤將指定的bean的屬性值寫到當前的JspWriter中,並且可以對輸出進行格式化。

例如在struts的action着那個通過request.setAttribute("names","dddd");將屬性值dddd中放入names,可在jsp頁面中通過bean:write將names屬性輸出。

eg:<bean:write name="names"/>

對於日期型的屬性,可在bean:write標籤中指定format來輸出日期格式,

eg:<bean:write name="date"format="MM/dd/yyyy"/>

如果要輸出某對象的某屬性,例如屬性名爲person的對象的name屬性,可通過如下方式

eg:<bean:write name="person"property="name"/>

 

bean:message

該標籤用來從指定的locale中取回國際化的消息並輸出,在這個過程中我們還可以傳遞5個以內的參數。message key可以通過key直接指定,也可以通過name和property間接的指定。

eg1.  <bean:messagekey="welcome.title.content"/>

該句要求在資源文件中有welcome.title.content的鍵值對(資源文件ApplicationSource.properties在struts的配置文件中指定)。

eg2.  傳遞參數信息的bean:message的用法,

<bean:messagekey="greeting" arg1="good morning" arg2="goodevening"/>

在資源文件中greeting的配置舉例如下:

greeting = hello, {0}, {1}.

bean:parameter

該標籤取回請求中的參數值。如果沒有指定multiple屬性則依據剛取回的值創建一個String類型的bean,否則根據剛取回的值創建一個String[]類型的數組。然後用id屬性值將String或String[]綁定到page作用域中(這種綁定是爲了其它標籤能夠使用該值),並創建對應的scripting變量(這種變量是爲了JSP腳本能夠使用該值)。

eg. 當請求如下的jsp頁面時:http://localhost:8080/test.jsp?orgId=1

在test.jsp頁中可通過如下方式獲得orgId參數:

<bean:parameterid="ok" name="orgId"/>
 
 <bean:write name="ok"/>

bean:size

該標籤創建一個java.lang.Integer類型的bean,該值爲該標籤指定的Collection或Map,List中所含元素的個數。它可和logic:iterate標籤配合使用。

如下語句輸出userList屬性中元素的個數:

eg. <bean:sizeid="size" name="userList"/>
      <bean:write name="size"/>

 

 

 struts標籤使用舉例-NESTED

StrutsNested標籤庫的分兩部分:

一部分用於表達JavaBean之間的嵌套關係

另一部分能夠在特定的嵌套級別提供和其他Struts標籤相同的功能。

<nested:nest>,定義一個新的嵌套級別

<nested:writeNesting>,輸出當前嵌套級別信息

<nested:nest>標籤可以表達JavaBean之間的嵌套關係

eg.

以三個JavaBean爲例,分別是:PersonForm Bean,Person Bean和Address Bean,在PersonForm Bean中包含一個Person Bean類型的屬性person,在Person Bean中又包含一個Address Bean類型的屬性address。

則用nested標籤表示如下:

定義兩個<nested:nest>標籤,第一個<nested:nest>標籤嵌套在<html:form>標籤中,如下:

<html:form action="/showPerson">

<nested:nestproperty="person">

LastName:<nested:textproperty="lastName"/><BR>
          .....

</nested:nest>
</html:form>
以上<nested:nest>標籤的上層JavaBean位於<html:form>表單標籤對應的PersonForm Bean,<nested:nest>標籤的property屬性爲“person",代表PersonForm Bean的person屬性,這個person屬性代表Person Bean,因此嵌套在<nested:nest>標籤內部的Nested標籤都相對於這個Person Bean,例如第一個<nested:text>標籤的property屬性”lastName“,代表Person Bean的lastName屬性。

第二個<nested:nest>標籤嵌套在第一個<nested:nest>標籤內部:如下

<html:formaction="/showPerson">

<nested:nestproperty="person">

.............
          <nested:nestproperty="address">


Current nesting is :

<nested:writeNesting/><br>

Street1:<nested:text property="street1"/><BR>

</nested:nest>

</nested:nest>

</html:form>


在以上代碼中,第二個<nested:nest>標籤的property屬性爲“address",代表PersonBean 的address屬性,這個address屬性代表Address Bean,因此嵌套在第二個<nested:nest>標籤內部的Nested標籤都相對於這個Address Bean。

第二個<nested:nest>標籤內還嵌套了一個<nested:writeNesting>標籤,它顯示當前的嵌套級別,輸出結果爲”person.address".

在默認情況下,<nested:nest>標籤的property屬性爲當前ActionForm Bean的某個屬性,或者位於上層<nested:nest>標籤對應的JavaBean的某個屬性。

可以使用<nested:root>標籤來顯式指定頂層級別的JavaBean。

<nested:root>標籤的name屬性指定JavaBean的名字,嵌套在<nested:root>標籤中的<nested:nest>標籤的property屬性爲這個JavaBean的某個屬性。

和其他標籤庫中的標籤功能相同的Nested標籤


許多Nestd標籤庫中的標籤具有和其他標籤庫中的標籤相同的功能,區別在於Nested標籤庫中的標籤屬性相對於當前的嵌套級別,例如
   <nested:nest property ="person">
        Last name:<nested:text property="lastName"/>
   </nested:nest>
上面的<nested:text>標籤和<html:text>標籤具有相同的功能,都可以生成文本框,兩者的區別在於<nested:text>標籤的property屬性爲位於當前嵌套級別對應的JavaBean的某個屬性,而<html:text>標籤的property屬性爲於當前表單對應的ActionForm Bean的某個屬性。比如我有一個User類和一個UserInfo類,前者記錄用戶的帳號密碼,後者記錄用戶的詳細信息。前者也有一個UserInfo屬性,這樣它們兩者是嵌套了。   
現在我要把這個用戶的帳號和詳細信息都顯示到界面上。  一種方式是在actionForm中用兩個屬性User user和UserInfo userInfo來存儲,在jsp中就可以用如下方式顯示出來:   
   <nested:nest property="user">   
           帳號:<nested:write    property="account"/>  
   </nested:nest>   
   <nested:nest property="userInfo">  
           姓名:<nested:write    property="name"/>  
           性別:<nested:write    property="sex"/>  
   </nested:nest>
   由於user和userInfo本身就是嵌套的,所以第二種方式就在actionForm中使用一個User  user屬性即可:
   <nested:nest property="user">   
           帳號:<nested:write  property="account"/>   
      <nested:nest property="userInfo">  
          姓名:<nested:write    property="name"/>  
          性別:<nested:write    property="sex"/>  
      </nested:nest>   
   </nested:nest>   
    
   這樣處理是不是很方便了,actionForm可以直接放上數據存儲對象,如果使用了hibernate做數據持久層,我們就可以直接把持久化對象放入actionForm來顯示到界面上,不用在actionForm裏寫很多屬性來分別存儲數據,也免去了給這些屬性分別賦值的過程。   
    
   如果我們把上邊例子中的<nested:write/>標記換成<nested:text/>,這就類似於<html:text/>標記,是一個輸入框,這樣我們就可以把界面上輸入一次提交到actionForm中的這個數據存儲對象,比如user。我們在action中就可以直接獲得這個user進行處理,非常方便。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章