使用Spring標簽庫


A、Spring標簽庫

Web項目若使用Spring Web MVC並使用JSP作為表現的話。從Spring2.0版本開始提供一套標簽庫可供使用。

使用標簽庫無非是易於開發,維護之類雲雲。這里就不闡述了。我們還是更關注spring有哪些標簽庫和如何使用。

B、spring.tld標簽庫

spring.tld標簽庫核心類的包在org.springframework.web.servlet.tags。

B.1、spring:hasBindErrors

對應org.springframework.web.servlet.tags.BindErrorsTag標記庫處理類。

這個標記提供用於綁定對象的errors,如果這個標記被用到的話,那么關於這個對象的錯誤將在頁面上顯示出來。使用這個標記的前提條件是要先使用<spring:bind>標記,並且<spring:hasBindErrors>這個標記不能用來表示對象的狀態,它僅僅可以綁定對象本身和對象的屬性。

舉例

<spring:hasBindErrors name="priceIncrease">

<b>Please fix all errors!</b>

</spring:hasBindErrors>

屬性

  name:是要被檢查的Bean的名字。這個屬性是必需要的。

B.2、spring:bind

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

  這個標記用來為某個bean或bean 的屬性賦值,通常和form一起用,相當於action的作用。它指明表單要提交到那個類或類的屬性中去。

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

B.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。

B.4、spring:message

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

  這個標記用來幫助springframework支持國際化。和JSTL的fmt:message標記類似。當然這個標記可以很好的工作的本地的springframework框架下。

 

屬性

  code:不是必需的。用來查找message,如果沒有被使用的話,text將被使用。

text:不是必需的。假如code不存在的話,默認是text輸出。當code和text都沒有設置的話,標記將輸出為null.

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

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

 

B.5、spring:htmlEscape

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

B.6、spring:theme

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

 

C、spring-form.tld標簽庫

Spring-form.tld標簽庫核心類的包在org.springframework.web.servlet.tags.form。

spring的表單標簽庫

D、使用Spring標簽庫

D.1、方法1

曾在《[JSP]自定義標簽》介紹過如何自定義標簽,那么我們知道我們必須取得標簽庫描述文件(spring.tld和Spring-form.tld)、標簽處理類、並在web.xml中引入、最后才在jsp中使用。

1、將spring.tld和Spring-form.tld拷貝到WEB-INF目錄。

2、將spring.jar拷貝到WEB-INF\lib包下

3、配置web.xml

<!-- 定義標簽庫描述文件 -->

<jsp-config>

    <taglib>

       <taglib-uri>/spring-form</taglib-uri>

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

    </taglib>

    <taglib>

       <taglib-uri>/spring</taglib-uri>

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

    </taglib>

</jsp-config>

<!-- 使用監聽器加載spring配置文件 -->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

</context-param>

<listener>

    <listener-class>

       org.springframework.web.context.ContextLoaderListener

    </listener-class>

</listener>

一定要使用listener加載spring配置文件,不然會報“No WebApplicationContext found”的錯。

4.JSP代碼

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

<%@ taglib uri="/spring-form" prefix="from"%>

<html>

    <head>

       <title></title>

    </head>

    <body>

       <spring:message></spring:message>

       <from:form></from:form>

    </body>

</html>

這種方法我們使用本地的tld,這種方法的好處我們可以自定義dtl。當然我們也可以使用網絡上的tld。

D.2、方法2

1、配置web.xml

<!-- 使用監聽器加載spring配置文件 -->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

</context-param>

<listener>

    <listener-class>

       org.springframework.web.context.ContextLoaderListener

    </listener-class>

</listener>

2.JSP代碼

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="from"%>

<html>

    <head>

       <title></title>

    </head>

    <body>

       <spring:message></spring:message>

       <from:form></from:form>

    </body>

</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM