Spring提供了兩個JSP標簽庫,用來幫助定義Spring MVC Web的視圖。其中一個標簽庫會用來渲染HTML表單標簽,定義在spring-form.tld中,這些標簽可以綁定model中的某個屬性。另外一個標簽庫包含了一些工具類標簽,定義在spring.tld中。
在這篇文章中,我將對平時用到的spring標簽進行較詳細的記錄,以便最終將其掌握。
spring表單標簽
此節均采用以下引入方式:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
form:form
Attribute | Required? | Runtime Expression? | Description |
---|---|---|---|
commandName | False | True | Name of the model attribute under which the form object is exposed. Defaults to 'command'. |
htmlEscape | False | True | Enable/disable HTML escaping of rendered values. |
modelAttribute | False | True | Name of the model attribute under which the form object is exposed. Defaults to 'command'. |
使用Spring的form標簽主要有兩個作用:
1、綁定來自Model中的一個屬性值到當前form對應的實體對象,這樣我們就可以在form表單體內使用該對象的屬性了;
2、它支持我們在提交表單的時候使用除GET
和POST
之外的其他方法進行提交,包括DELETE
和PUT
等。
用法如下:
<form:form id="formid" name="formname" modelAttribute="DummyForm" >
當Model中存在一個屬性名為DummyForm
的javaBean時,在渲染form時就會將DummyForm的對應屬性值賦給對應標簽的值。
<form:form id="formid" name="formname" modelAttribute="DummyForm" >
<form:hidden path="field"/><!--自動綁定DummyForm.field的值-->
</form:form>
form:checkbox
checkbox
標簽會被渲染為一個type為checkbox
的普通HTML input標簽。若制定了其label屬性,則同時生成其對應的label(緊隨input后面)checkbox
標簽支持多種綁定數據的方式。
1.綁定boolean
數據
當checkbox綁定的是一個boolean數據的時候,那么checkbox的狀態跟該boolean數據的狀態是一樣的,即true對應選中,false對應不選中。
<form:form action="formTag/form.do" method="post" commandName="user">
<table>
<tr>
<td>Male:</td><td><form:checkbox path="male"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form:form>
看上面這段代碼,這個時候假設我們在渲染該視圖之前往Model中添加了一個user屬性,并且該user對象有一個類型為boolean的屬性male,那么這個時候如果male屬性為true則Male那一欄的復選框將會被選中。
2.綁定列表數據
這里的列表數據包括數組、List和Set。下面將以List為例講一下checkbox是如何根據綁定的列表數據來設定選中狀態的。現在假設有一個類User,其有一個類型為List的屬性roles,如下所示:
public class User {
private List<String> roles;
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
}
那么當我們需要展現該User是否擁有某一個Role的時候,我們可以使用checkbox標簽來綁定roles數據進行展現。當checkbox標簽的value在我們綁定的列表數據中存在的時候該checkbox將為選中狀態。來看下面一段代碼:
<form:form action="formTag/form.do" method="post" commandName="user">
<table>
<tr>
<td>Roles:</td>
<td>
<form:checkbox path="roles" value="role1"/>Role1<br/>
<form:checkbox path="roles" value="role2"/>Role2<br/>
<form:checkbox path="roles" value="role3"/>Role3
</td>
</tr>
</table>
</form:form>
就上面代碼而言就是當User擁有role1
的時候對應的<form:checkbox path="roles" value="role1"/>
就會為選中狀態,也就是說roles列表中包含role1的時候該checkbox就會為選中狀態。
3.綁定一個Object數據
checkbox還支持綁定數據類型為Object的數據,這種情況下Spring會拿所綁定對象數據的toString結果跟當前checkbox的value進行比較,如果能夠進行匹配則該checkbox將為選中狀態。看這樣一個例子,有一個User類代碼如下:
public class User {
private Blog blog;
public Blog getBlog() {
return blog;
}
public void setBlog(Blog blog) {
this.blog = blog;
}
}
Blog類的代碼如下:
public class Blog {
public String toString() {
return "HelloWorld";
}
}
我們可以看到Blog類的toString
方法已經被寫死為“HelloWorld”
了。這個時候假設我們往ModelMap中放了一個user對象,而且給該user對象設定了一個blog屬性,那么當我們使用該ModelMap對象渲染如下視圖代碼時,checkbox標簽的選中狀態是怎樣的呢?根據前面描述的當checkbox標簽綁定的是一個Object對象的時候我們會拿該Object對象的toString和checkbox的value值進行比較,如果匹配則當前checkbox為選中狀態,我們知道這里的checkbox將為選中狀態。
<form:form action="formTag/form.do" method="post" commandName="user">
<table>
<tr>
<td>HelloWorld:</td>
<td>
<form:checkbox path="blog" value="HelloWorld"/>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form:form>
spring工具標簽
此節均采用以下引入方式:
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
spring:url
Attribute | Required? | Runtime Expression? | Description |
---|---|---|---|
value | True | True | The URL to build. This value can include template {placeholders} that are replaced with the URL encoded value of the named parameter. Parameters must be defined using the param tag inside the body of this tag. |
context | False | True | Specifies a remote application context path. The default is the current application context path. |
var | False | True | The name of the variable to export the URL value to. If not specified the URL is written as output. |
scope | False | True | The scope for the var. 'application', 'session', 'request' and 'page' scopes are supported. Defaults to page scope. This attribute has no effect unless the var attribute is also defined. |
htmlEscape | False | True | Set HTML escaping for this tag, as a boolean value. Overrides the default HTML escaping setting for the current page. |
javaScriptEscape | False | True | Set JavaScript escaping for this tag, as a boolean value. Default is false. |
<spring:url value="/path/index" />
spring:url
標簽的value有三種格式:
1,含有://
則認為value指定了一個絕對路徑
2,非絕對路徑,以/
開頭,則在其前面拼接當前web上下文路徑
3,非絕對路徑,且不以/
開頭,先拼接/
之后同2
spring:url和c:url的區別
spring:url
和c:url
最大的區別是,spring:url
可以幫你解決htmlEscape的問題,當spring:url
標簽的htmlEscape屬性設置為ture時,它會把查詢參數連接符 &
符號化為&
因此使用c:url
時往往需要通過c:out
進行符號化處理(c:out
的escapeXml屬性默認為true):
<c:url value="/something" var="url"/>
<a href="<c:out value='${url}'/>">...</a>
而使用spring:url
則只需要將htmlEscape屬性設置為ture(默認為false
):
<spring:url value="/something" var="url" htmlEscape="true"/>
<a href="${url}">...</a>
而不需擔心something中含所有&
的問題。
什么情況下使用spring:url
1、正如上面提到的那樣,當你需要為你的url進行符號化的時候。
2、當你的url需要由動態參數構成的時候(e.g. /app/resources/{name}
) ,spring:url
支持使用占位符的方式動態作成url。
寫法如下:api
<spring:url value="/url/path/{variableName}">
<spring:param name="variableName" value="more than JSTL c:url" />
</spring:url>