Spring 標記庫分類如下:
1、spring:hasBindErrors
2、spring:bind
3、spring:transform
4、spring:message
5、spring:htmlEscape
6、spring:theme
下面我們來具體介紹一下用法:
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的名字。這個屬性是必需要的。
這里是簡單用法,所以就不介紹它包含的errors變量了。具體看英語文檔。下同。
2、spring:bind
對應org.springframework.web.servlet.tags.BindTag標記庫處理類
這個標記用來為某個bean或bean 的屬性賦值,通常和form一起用,相當于action的作用。它指明表單要提交到那個類或類的屬性中去。
其中path屬性是必須的,指明轉到的類的路徑。舉例如下:
<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
對應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。
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標記庫處理類
不常用,這里省略
學習完這些用法后,讓我們來看個具體的例子吧。
配置步驟如下:
目錄結構圖:
-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>標記經常用來綁定<input>表單元素以便提交給PriceIncrease.java,其實PriceIncrease.java中就是包含一些getter和setter的方法的類。當用戶按下按鈕提交的時候,其表單中的值實際上就被framework放到了PriceIncrease.java中。<spring:bind>經常和form一起使用.這個${status.errorMessage}和${status.value}是一個framework定義的特殊變量,它們被用來顯示錯誤信息和當前表單中的數據。呵呵,今天就到此為止吧,關于Spring Framework標記庫的詳細用法,請參考其文檔。