checkbox标签java
这个标签生成一个“checkbox”类型的HTML“input”标签。spring
让咱们假设咱们的User有好比新闻订阅和其余一组业余爱好这样的偏好。下面就是一个Preferences类的例子:服务器
public class Preferences {jsp private boolean receiveNewsletter;ui private String[] interests;this private String favouriteWord;spa public boolean isReceiveNewsletter() {rest return receiveNewsletter;orm }对象 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)值的属性的解析值。
· 第二种用法:若绑定值是array或java.util.Collection类型,则当设定的setValue(Object)值出如今绑定的Collection中时,input(checkbox)就标记为选中。
· 第三种用法:若绑定值为其余类型,则当设定的setValue(Object)与其绑定值相等时,input(checkbox)才标记为选中。
注意,无论使用哪一种方法,生成的HTML结构都是相同的。
下面是包含一些checkbox的HTML片断:
<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标签的目标。传入一个Array、List,或者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”属性存放文本标记。