更新時間:2021-09-13 11:39:46 來源:動力節點 瀏覽1136次
Web項目若使用Spring Web MVC并使用JSP作為表現的話。從Spring2.0版本開始提供一套標簽庫可供使用。
使用標簽庫無非是易于開發,維護之類云云。這里就不闡述了。我們還是更關注spring有哪些標簽庫和如何使用。
spring.tld標簽庫核心類的包在org.springframework.web.servlet.tags。
(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的名字。這個屬性是必需要的。
(2)spring:bind
對應org.springframework.web.servlet.tags.BindTag標記庫處理類
這個標記用來為某個bean或bean的屬性賦值,通常和form一起用,相當于action的作用。它指明表單要提交到那個類或類的屬性中去。
其中path屬性是必須的,指明轉到的類的路徑。
(3)spring:transform
對應org.springframework.web.servlet.tags.TransformTag標記庫處理類,這個標記用來轉換表單中不與bean中的屬性一一對應的那些屬性,通常和<spring:bind>一起使用。<spring:transform>標記為<spring:bind>使用提供了更好的支持。
屬性
value:必需要的。和當前標記指向的bean類相同。就是你要轉換的實體類名。
var:不是必需的。這個字符串被用來綁定輸出結果到page,request, session或application scope.默認情況輸出到jsp中。
scope:不是必需的。前提條件var必須設置的情況下。它的值可以是page,request, session或application。
(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。
(5)spring:htmlEscape
對應org.springframework.web.servlet.tags.HtmlEscapeTag標記庫處理類
(6)spring:theme
對應org.springframework.web.servlet.tags.ThemeTag標記庫處理類
Spring-form.tld標簽庫核心類的包在org.springframework.web.servlet.tags.form。
spring的表單標簽庫
(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。
(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>
以上就是動力節點小編介紹的"使用Spring標簽庫的方法",希望對大家有幫助,想了解更多可查看Spring框架教程。動力節點在線學習教程,針對沒有任何Java基礎的讀者學習,讓你從入門到精通,主要介紹了一些Java基礎的核心知識,讓同學們更好更方便的進行Java學習,感興趣的同學可以關注一下。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習