`

Spring MVC 标签之checkbox与checkboxes

 
阅读更多

checkbox标签

这个标签生成一个“checkbox”类型的HTML“input”标签。

让我们假设我们的User有比如新闻订阅和其他一组业余爱好这样的偏好。下面就是一个Preferences的例子:

public class Preferences {

private boolean receiveNewsletter;

private String[] interests;

private String favouriteWord;

public boolean isReceiveNewsletter() {

return receiveNewsletter;

}

public void setReceiveNewsletter(boolean receiveNewsletter) {

this.receiveNewsletter = receiveNewsletter;

}

public String[] getInterests() {

return interests;

}

public void setInterests(String[] interests) {

this.interests = interests;

}

public String getFavouriteWord() {

return favouriteWord;

}

public void setFavouriteWord(String favouriteWord) {

this.favouriteWord = favouriteWord;

}

}


form.jsp如下:

<form:form>

<table>

<tr>

<td>Subscribe to newsletter?:</td>

<%-- Approach 1: Property is of type java.lang.Boolean --%>

<td><form:checkbox path="preferences.receiveNewsletter"/></td>

</tr>

<tr>

<td>Interests:</td>

<td>

<%-- Approach 2: Property is of an array or of type java.util.Collection --%>

Quidditch: <form:checkbox path="preferences.interests" value="Quidditch"/>

Herbology: <form:checkbox path="preferences.interests" value="Herbology"/>

Defence Against the Dark Arts: <form:checkbox path="preferences.interests"

value="Defence Against the Dark Arts"/>

</td>

</tr>

<tr>

<td>Favourite Word:</td>

<td>

<%-- Approach 3: Property is of type java.lang.Object --%>

Magic: <form:checkbox path="preferences.favouriteWord" value="Magic"/>

</td>

</tr>

</table>

</form:form>

有三种checkbox标签的使用方法满足你对checkbox的需求。

·第一种用法:若绑定值是java.lang.Boolean类型,则值为true时,input(checkbox)就标记为选中。其value属性对应于setValue(Object)值的属性的解析值。

·第二种用法:若绑定值是arrayjava.util.Collection类型,则当设定的setValue(Object)值出现在绑定的Collection中时,input(checkbox)就标记为选中。

·第三种用法:若绑定值为其他类型,则当设定的setValue(Object)与其绑定值相等时,input(checkbox)才标记为选中。

注意,不管使用哪种方法,生成的HTML结构都是相同的。

下面是包含一些checkboxHTML片段:

<tr>

<td>Interests:</td>

<td>

Quidditch: <input name="preferences.interests" type="spring.framework.eckbox" value="Quidditch"/>

<input type="hidden" value="1" name="_preferences.interests"/>

Herbology: <input name="preferences.interests" type="spring.framework.eckbox" value="Herbology"/>

<input type="hidden" value="1" name="_preferences.interests"/>

Defence Against the Dark Arts: <input name="preferences.interests" type="spring.framework.eckbox"

value="Defence Against the Dark Arts"/>

<input type="hidden" value="1" name="_preferences.interests"/>

</td>

</tr>

也许没有注意到的是在每个checkbox背后还隐藏了其他字段(field)。当一个HTML页面中的checkbox没有被选中时,它的值不会在表单提交时作为HTTP请求参数的一部分发送到服务器端,因此我们需要给这个HTML的奇怪动作想出一个变通方案,来让Spring的表单数据绑定可以工作。checkbox标签遵循了Spring现有的惯例,就是对于每个checkbox都包含了一个下划线("_"),再跟上一个隐藏参数。这样一来,就相当于告诉Spring“这个checkbox在表单中是可见的,并且希望表单数据将要被绑定到的对象能够反映出任意的checkbox的状态

checkboxes 标签

这个标签生成多个“checkbox”类型的HTML“input”标签。

这一节建立在上一节checkbox标签的例子之上。有时倾向于并不在JSP页面中列出全部可能的业余爱好,而是想在运行时提供一个可用选项的清单,并把它传递给相应标签。这就是checkboxes标签的目标。传入一个ArrayList,或者Map并把可用选项包含在“items”属性中。典型的情况是,这个绑定的属性是一个集合,这样它才能持有用户选择的多个值。下面是使用了这个标签的JSP的一个例子:

<form:form>

<table>

<tr>

<td>Interests:</td>

<td>

<%-- Property is of an array or of type java.util.Collection --%>

<form:checkboxespath="preferences.interests"items="${interestList}"/>

</td>

</tr>

</table>

</form:form>

这个例子假定了“interestList”是一个List,作为模型属性它包含了用于被选择的字符串的值。而在使用一个Map的情况下,map条目的键被用作值,map条目的值被用作显示的文本标记。也可以使用一个定制的对象,提供“itemValue”属性存放值,“itemLabel”属性存放文本标记。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics