更新時間:2021-09-13 11:39:46 來源:動力節(jié)點(diǎn) 瀏覽1218次
Web項(xiàng)目若使用Spring Web MVC并使用JSP作為表現(xiàn)的話。從Spring2.0版本開始提供一套標(biāo)簽庫可供使用。
使用標(biāo)簽庫無非是易于開發(fā),維護(hù)之類云云。這里就不闡述了。我們還是更關(guān)注spring有哪些標(biāo)簽庫和如何使用。
spring.tld標(biāo)簽庫核心類的包在org.springframework.web.servlet.tags。
(1)spring:hasBindErrors
對應(yīng)org.springframework.web.servlet.tags.BindErrorsTag標(biāo)記庫處理類。
這個標(biāo)記提供用于綁定對象的errors,如果這個標(biāo)記被用到的話,那么關(guān)于這個對象的錯誤將在頁面上顯示出來。使用這個標(biāo)記的前提條件是要先使用<spring:bind>標(biāo)記,并且<spring:hasBindErrors>這個標(biāo)記不能用來表示對象的狀態(tài),它僅僅可以綁定對象本身和對象的屬性。
舉例
<spring:hasBindErrors name="priceIncrease">
<b>Please fix all errors!</b>
</spring:hasBindErrors>
屬性
name:是要被檢查的Bean的名字。這個屬性是必需要的。
(2)spring:bind
對應(yīng)org.springframework.web.servlet.tags.BindTag標(biāo)記庫處理類
這個標(biāo)記用來為某個bean或bean的屬性賦值,通常和form一起用,相當(dāng)于action的作用。它指明表單要提交到那個類或類的屬性中去。
其中path屬性是必須的,指明轉(zhuǎn)到的類的路徑。
(3)spring:transform
對應(yīng)org.springframework.web.servlet.tags.TransformTag標(biāo)記庫處理類,這個標(biāo)記用來轉(zhuǎn)換表單中不與bean中的屬性一一對應(yīng)的那些屬性,通常和<spring:bind>一起使用。<spring:transform>標(biāo)記為<spring:bind>使用提供了更好的支持。
屬性
value:必需要的。和當(dāng)前標(biāo)記指向的bean類相同。就是你要轉(zhuǎn)換的實(shí)體類名。
var:不是必需的。這個字符串被用來綁定輸出結(jié)果到page,request, session或application scope.默認(rèn)情況輸出到j(luò)sp中。
scope:不是必需的。前提條件var必須設(shè)置的情況下。它的值可以是page,request, session或application。
(4)spring:message
對應(yīng)org.springframework.web.servlet.tags.MessageTag標(biāo)記庫處理類
這個標(biāo)記用來幫助springframework支持國際化。和JSTL的fmt:message標(biāo)記類似。當(dāng)然這個標(biāo)記可以很好的工作的本地的springframework框架下。
屬性
code:不是必需的。用來查找message,如果沒有被使用的話,text將被使用。
text:不是必需的。假如code不存在的話,默認(rèn)是text輸出。當(dāng)code和text都沒有設(shè)置的話,標(biāo)記將輸出為null.
var:不是必需的。這個字符串被用來綁定輸出結(jié)果到page,request, session或application scope.默認(rèn)情況輸出到j(luò)sp中。
scope:不是必需的。前提條件var必須設(shè)置的情況下。它的值可以是page,request, session或application。
(5)spring:htmlEscape
對應(yīng)org.springframework.web.servlet.tags.HtmlEscapeTag標(biāo)記庫處理類
(6)spring:theme
對應(yīng)org.springframework.web.servlet.tags.ThemeTag標(biāo)記庫處理類
Spring-form.tld標(biāo)簽庫核心類的包在org.springframework.web.servlet.tags.form。
spring的表單標(biāo)簽庫
(1)方法1
曾在《[JSP]自定義標(biāo)簽》介紹過如何自定義標(biāo)簽,那么我們知道我們必須取得標(biāo)簽庫描述文件(spring.tld和Spring-form.tld)、標(biāo)簽處理類、并在web.xml中引入、最后才在jsp中使用。
1)將spring.tld和Spring-form.tld拷貝到WEB-INF目錄。
2)將spring.jar拷貝到WEB-INF\lib包下
3)配置web.xml
<!-- 定義標(biāo)簽庫描述文件 -->
<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>
<!-- 使用監(jiān)聽器加載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。當(dāng)然我們也可以使用網(wǎng)絡(luò)上的tld。
(2)方法2
1)配置web.xml
<!-- 使用監(jiān)聽器加載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>
以上就是動力節(jié)點(diǎn)小編介紹的"使用Spring標(biāo)簽庫的方法",希望對大家有幫助,想了解更多可查看Spring框架教程。動力節(jié)點(diǎn)在線學(xué)習(xí)教程,針對沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識,讓同學(xué)們更好更方便的進(jìn)行Java學(xué)習(xí),感興趣的同學(xué)可以關(guān)注一下。
初級 202925
初級 203221
初級 202629
初級 203743