使用Spring標籤庫

A、Spring標籤庫 html

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

使用標籤庫無非是易於開發,維護之類云云。這裏就不闡述了。咱們仍是更關注spring有哪些標籤庫和如何使用。spring

B、spring.tld標籤庫網絡

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

B.1、spring:hasBindErrorsapp

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

這個標記提供用於綁定對象的errors,若是這個標記被用到的話,那麼關於這個對象的錯誤將在頁面上顯示出來。使用這個標記的前提條件是要先使用<spring:bind>標記,而且<spring:hasBindErrors>這個標記不能用來表示對象的狀態,它僅僅能夠綁定對象自己和對象的屬性。jsp

舉例spa

<spring:hasBindErrors name="priceIncrease">code

<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中使用。

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

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

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

一、配置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>
相關文章
相關標籤/搜索