Struts1 標籤庫

 

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

HTML標籤 : 用來建立可以和Struts 框架和其餘相應的HTML 標籤交互的HTML 輸入表單 html

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

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

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

Nested標籤:  加強對其餘的Struts 標籤的嵌套使用的能力 數組

標籤的公共特徵

>>styleId:命名自定義標籤建立時的腳本變量名。 服務器

>>name :指出關鍵字值,在該關鍵字下能夠找到一個存在的bean 。若是給出了scope屬性,則僅僅在scope中查找。不然,根據標準的順序在各類scope中查找。標準順序爲 (page,request, session, or application)。 
>> property :指出bean 中的某個屬性,能夠在其中檢索值。若是沒有標明,則使用對象自己的值。 session

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

 

說明: 框架

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的做用:

二、 html:cancel

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

三、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>

四、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

五、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中接收到該標籤的多選值

六、html:multibox標籤

該標籤用法同html:checkbox

八、html:radio標籤

該標籤生成radio元素

eg

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

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

九、html:img標籤

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


      <html:img

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

十、html:link標籤

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

此行代碼解析後:

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

十一、html:html標籤

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

<html:htmllocale=\"true\">

此行代碼解析後:

<htmllang=\"en\">

說明:

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

 

十二、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:相應的信息在錯誤信息的單獨列表後顯示。

1三、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進行處理,很是方便。

相關文章
相關標籤/搜索