Spring的基礎標籤庫提供了簡單易用的數據綁定和顯示功能

Spring的基礎標籤庫提供了簡單易用的數據綁定和顯示功能。

配置基礎標籤庫:html

 須要將Spring安裝包中的spring.tld複製到項目的WEB-INF目錄下,並在web.xml中配置該標籤庫: java

<taglib>
        <taglib-uri>/spring</taglib-uri>
        <taglib-location>/WEB-INF/spring.tld</taglib-location>
    </taglib>

 

 而後在JSP頁面的啓示部分加入下面這行聲明:web

<%@ taglib uri="/spring" prefix="/spring"  %> 或者 <%@ taglib uri="/spring" prefix="spring"  %>

其中 「/spring」 中的spring爲該標籤庫的引用別名,即在web.xml中<taglib-uri>/聲明的部分spring

 

1. <spring:bind> express

對應org.springframework.web.servlet.tags.BindTag標記庫處理類api

該標籤用來爲某個bean或bean的屬性賦值,它一般和form一塊兒用,用以指明表單要提交到哪一個類或類的屬性中去,舉例以下:session

<form method="post">
    <spring:bind path="user.age">
       姓名: <input type="text" name="${status.expression}" value="${status.value}">
          <font color="red">${status.errorMessage}</font>
    </spring:bind>
</form>

 

其中path屬性是必需的,指明轉到的類的路徑。app

 

2. <spring:hasBindErrors>jsp

 

    對應org.springframework.web.servlet.tags.BindErrorsTag標記庫處理類。
這個標記提供用於綁定對象的errors,若是這個標記被用到的話,那麼關於這個對象的錯誤將在頁面上顯示出來。使用這個標記的前提條件是要先使用 <spring:bind>標記,而且<spring:hasBindErrors>這個標記不能用來表示對象的狀態,它僅僅能夠綁定對象自己和對象的屬 性。 具體用法以下: post

   

    <spring:hasBindErrors name="priceIncrease">
      <b>Please fix all errors!</b>
        </spring:hasBindErrors>

 

經過這個簡單的例子,我來具體說一下這個標記的屬性吧, name:是要被檢查的Bean的名字。這個屬性是必須要的。這裏是簡單用法,因此就不介紹它包含的errors變量了。具體看英語文檔。下同。 

 

3. <spring:transform>

 

 對應org.springframework.web.servlet.tags.TransformTag標記庫處理類,這個標記用來轉換表單 中不與bean中的屬性一一對應的那些屬性,一般和<spring:bind>一塊兒使用。<spring:transform> 標記爲<spring:bind>使用提供了更好的支持。

屬性以下:

value:必須要的。和當前<spring:bind>標記指向的bean類相同。就是你要轉換的實體類名。

var:不是必需的。這個字符串被用來綁定輸出結果到page,request, session或application scope.默認狀況輸出到jsp中。

scope:不是必需的。前提條件var必須設置的狀況下。它的值能夠是page,request, session或application。

4. <spring:message>

 

根據code取得消息資源,若是指定的code沒有找到任何對應的消息資源,則採用text定義的內容。通常用於I18N。
屬性-
message:MessageSourceResolvable 類型
code:查找消息資源時使用的關鍵字
arguments:屬性message的參數
argumentSeparator:參數分割符號,默認爲「,」(英文)
text:默認替代文本(根據code,未找到資源時)
var(scope):存放查找到的消息資源
htmlEscape: boolean類型,設定html escape 屬性
javaScriptEscape: boolean類型,設定JavaScript escaping 屬性

示範代碼:

<spring:message code="username" test="用戶名"/>

 

5.<spring:htmlEscape> 

 

設置是否啓用 默認html字符轉換,默認爲「false」
示範代碼:

<spring:htmlEscape defaultHtmlEscape="false">

 

   6.<spring:theme>


主題配置,主要用來配置I18N主題message,code,arguments,argumentSeparator,text,var,scope,htmlEscape,javaScriptEscape:相似<spring:message> 
代碼:

<spring:theme code="styleSheet"/>

  

7.<spring:nestedpath> 

 

爲<spring:bind>配置嵌套路徑
變量:nestedPath   java.lang.String類型
path:設置嵌套的路徑,例如:配置"customer"後,能夠設置爲"address.street"替代"customer.address.street" 

 

8.<spring:escapeBody> 

 

相似<spring:htmlEscapey>
escape its enclosed(被附上的) body content, applying HTML escaping and/or JavaScript escaping
屬性(boolean類型值,字符串表示):
htmlEscape:同上
javaScriptEscape:相似htmlEscape

相關文章
相關標籤/搜索