Spring 標(biāo)記庫分類如下:
1、spring:hasBindErrors
2、spring:bind
3、spring:transform
4、spring:message
5、spring:htmlEscape
6、spring:theme
下面我們來具體介紹一下用法:
1、spring:hasBindErrors
對(duì)應(yīng)org.springframework.web.servlet.tags.BindErrorsTag標(biāo)記庫處理類。
這個(gè)標(biāo)記提供用于綁定對(duì)象的errors,如果這個(gè)標(biāo)記被用到的話,那么關(guān)于這個(gè)對(duì)象的錯(cuò)誤將在頁面上顯示出來。使用這個(gè)標(biāo)記的前提條件是要先使用<spring:bind>標(biāo)記,并且<spring:hasBindErrors>這個(gè)標(biāo)記不能用來表示對(duì)象的狀態(tài),它僅僅可以綁定對(duì)象本身和對(duì)象的屬性。 具體用法如下:
<spring:hasBindErrors name="priceIncrease"> <b>Please fix all errors!</b> </spring:hasBindErrors> |
通過這個(gè)簡單的例子,我來具體說一下這個(gè)標(biāo)記的屬性吧。
name:是要被檢查的Bean的名字。這個(gè)屬性是必需要的。
這里是簡單用法,所以就不介紹它包含的errors變量了。具體看英語文檔。下同。
2、spring:bind
對(duì)應(yīng)org.springframework.web.servlet.tags.BindTag標(biāo)記庫處理類
這個(gè)標(biāo)記用來為某個(gè)bean或bean 的屬性賦值,通常和form一起用,相當(dāng)于action的作用。它指明表單要提交到那個(gè)類或類的屬性中去。
其中path屬性是必須的,指明轉(zhuǎn)到的類的路徑。舉例如下:
<form method="post"> <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> <tr> <td alignment="right" width="20%">Increase (%):</td> <spring:bind path="priceIncrease.percentage"> <td width="20%"> <input type="text" name="percentage" value="<c:out value="${status.value}"/>"> </td> <td width="60%"> <font color="red"><c:out value="${status.errorMessage}"/></font> </td> </spring:bind> </tr> </table> <br> ……………………省略 </form> |
3、spring:transform
對(duì)應(yīng)org.springframework.web.servlet.tags.TransformTag標(biāo)記庫處理類,這個(gè)標(biāo)記用來轉(zhuǎn)換表單中不與bean中的屬性一一對(duì)應(yīng)的那些屬性,通常和<spring:bind>一起使用。<spring:transform>標(biāo)記為<spring:bind>使用提供了更好的支持。
屬性如下:
value:必需要的。和當(dāng)前<spring:bind>標(biāo)記指向的bean類相同。就是你要轉(zhuǎn)換的實(shí)體類名。
var:不是必需的。這個(gè)字符串被用來綁定輸出結(jié)果到page,request, session或application scope.默認(rèn)情況輸出到j(luò)sp中。
scope:不是必需的。前提條件var必須設(shè)置的情況下。它的值可以是page,request, session或application。
4、spring:message 對(duì)應(yīng)org.springframework.web.servlet.tags.MessageTag標(biāo)記庫處理類
這個(gè)標(biāo)記用來幫助springframework支持國際化。和JSTL的fmt:message標(biāo)記類似。當(dāng)然這個(gè)標(biāo)記可以很好的工作的本地的springframework框架下。
屬性如下:
code:不是必需的。用來查找message,如果沒有被使用的話,text將被使用。
text:不是必需的。假如code不存在的話,默認(rèn)是text輸出。當(dāng)code和text都沒有設(shè)置的話,標(biāo)記將輸出為null.
var:不是必需的。這個(gè)字符串被用來綁定輸出結(jié)果到page,request, session或application scope.默認(rèn)情況輸出到j(luò)sp中。
scope:不是必需的。前提條件var必須設(shè)置的情況下。它的值可以是page,request, session或application。
5、spring:htmlEscape
對(duì)應(yīng)org.springframework.web.servlet.tags.HtmlEscapeTag標(biāo)記庫處理類
不常用,這里省略
6、spring:theme
對(duì)應(yīng)org.springframework.web.servlet.tags.ThemeTag標(biāo)記庫處理類
不常用,這里省略
學(xué)習(xí)完這些用法后,讓我們來看個(gè)具體的例子吧。
配置步驟如下:
目錄結(jié)構(gòu)圖:
-spirngapp
-WEB-INF
-classes
-lib
-src
-war
1、將spring.tld拷貝到WEB-INF目錄。
2、將spring.jar拷貝到WEB-INF\lib包下
舉例如下:
在web.xml中配置好taglib屬性,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'> <web-app> <servlet> <servlet-name>springapp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springapp</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/spring.tld</taglib-location> </taglib> </web-app> |
jsp頁面代碼如下:
<%@ include file="/WEB-INF/jsp/include.jsp" %> <%@ taglib prefix="spring" uri="/spring" %> <html> <head> <title><fmt:message key="title"/></title> </head> <body> <h1> <fmt:message key="priceincrease.heading"/> </h1> <form method="post"> <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> <tr> <td alignment="right" width="20%">Increase (%):</td> <spring:bind path="priceIncrease.percentage"> <td width="20%"> <input type="text" name="percentage" value="<c:out value="${status.value}"/>"> </td> <td width="60%"> <font color="red"> <c:out value="${status.errorMessage}"/> </font> </td> </spring:bind> </tr> </table> <br> <spring:hasBindErrors name="priceIncrease"> <b>Please fix all errors!</b> </spring:hasBindErrors> <br><br> <input type="submit" alignment="center" value="Execute"> </form> <a href="<c:url value="hello.htm"/>">Home</a> </body> </html> |
<spring:bind>標(biāo)記經(jīng)常用來綁定<input>表單元素以便提交給PriceIncrease.java,其實(shí)PriceIncrease.java中就是包含一些getter和setter的方法的類。當(dāng)用戶按下按鈕提交的時(shí)候,其表單中的值實(shí)際上就被framework放到了PriceIncrease.java中。<spring:bind>經(jīng)常和form一起使用.這個(gè)${status.errorMessage}和${status.value}是一個(gè)framework定義的特殊變量,它們被用來顯示錯(cuò)誤信息和當(dāng)前表單中的數(shù)據(jù)。呵呵,今天就到此為止吧,關(guān)于Spring Framework標(biāo)記庫的詳細(xì)用法,請(qǐng)參考其文檔。