In JSF, <h:selectBooleanCheckbox /> tag is used to render a single HTML input element of “checkbox” type.

  1. //JSF...
  2. <h:selectBooleanCheckbox value="#{user.rememberMe}" /> Remember Me
  3. //HTML output...
  4. <input type="checkbox" name="j_idt6:j_idt8" /> Remember Me

While <h:selectManyCheckbox /> tag is used to render a set of HTML input element of type “checkbox”, and format it with HTML table and label tags.

  1. //JSF...
  2. <h:selectManyCheckbox value="#{user.favNumber1}">
  3. <f:selectItem itemValue="1" itemLabel="Number1 - 1" />
  4. <f:selectItem itemValue="2" itemLabel="Number1 - 2" />
  5. <f:selectItem itemValue="3" itemLabel="Number1 - 3" />
  6. </h:selectManyCheckbox>
  1. //HTML output...
  2. <table>
  3. <tr>
  4. <td>
  5. <input name="j_idt6:j_idt10" id="j_idt6:j_idt10:0" value="1" type="checkbox" />
  6. <label for="j_idt6:j_idt10:0" class=""> Number1 - 1</label></td>
  7. <td>
  8. <input name="j_idt6:j_idt10" id="j_idt6:j_idt10:1" value="2" type="checkbox" />
  9. <label for="j_idt6:j_idt10:1" class=""> Number1 - 2</label></td>
  10. <td>
  11. <input name="j_idt6:j_idt10" id="j_idt6:j_idt10:2" value="3" type="checkbox" />
  12. <label for="j_idt6:j_idt10:2" class=""> Number1 - 3</label></td>
  13. <td>
  14. </tr>
  15. </table>

JSF 2.0 example

Here’s a JSF 2.0 example to show the use of “h:selectBooleanCheckbox” and “h:selectManyCheckbox” tags.

h:selectBooleanCheckbox

Render a single checkbox, and wire it with a boolean property.

h:selectManyCheckbox

Render a group of checkboxes and populate the data in different ways :

  • Hardcoded value in “f:selectItem” tag.
  • Generate values with an Array and put it into “f:selectItems” tag.
  • Generate values with a Map and put it into “f:selectItems” tag.
  • Generate values with an Object array and put it into “f:selectItems” tag, then represent the value with “var” attribute.

1. Backing Bean

A backing bean to hold the submitted checkboxes values.

  1. package com.mkyong;
  2. import java.util.Arrays;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import javax.faces.bean.ManagedBean;
  6. import javax.faces.bean.SessionScoped;
  7. @ManagedBean(name="user")
  8. @SessionScoped
  9. public class UserBean{
  10. public boolean rememberMe;
  11. public String[] favNumber1;
  12. public String[] favNumber2;
  13. public String[] favNumber3;
  14. public String[] favNumber4;
  15. //getter and setter methods...
  16. public String getFavNumber1InString() {
  17. return Arrays.toString(favNumber1);
  18. }
  19. //Generated by Array
  20. public String[] getFavNumber2Value() {
  21. favNumber2 = new String[5];
  22. favNumber2[0] = "Number2 - 1";
  23. favNumber2[1] = "Number2 - 2";
  24. favNumber2[2] = "Number2 - 3";
  25. favNumber2[3] = "Number2 - 4";
  26. favNumber2[4] = "Number2 - 5";
  27. return favNumber2;
  28. }
  29. public String getFavNumber2InString() {
  30. return Arrays.toString(favNumber2);
  31. }
  32. //Generated by Map
  33. private static Map<String,Object> number3Value;
  34. static{
  35. number3Value = new LinkedHashMap<String,Object>();
  36. number3Value.put("Number3 - 1", "1"); //label, value
  37. number3Value.put("Number3 - 2", "2");
  38. number3Value.put("Number3 - 3", "3");
  39. number3Value.put("Number3 - 4", "4");
  40. number3Value.put("Number3 - 5", "5");
  41. }
  42. public Map<String,Object> getFavNumber3Value() {
  43. return number3Value;
  44. }
  45. public String getFavNumber3InString() {
  46. return Arrays.toString(favNumber3);
  47. }
  48. //Generated by Object array
  49. public static class Number{
  50. public String numberLabel;
  51. public String numberValue;
  52. public Number(String numberLabel, String numberValue){
  53. this.numberLabel = numberLabel;
  54. this.numberValue = numberValue;
  55. }
  56. public String getNumberLabel(){
  57. return numberLabel;
  58. }
  59. public String getNumberValue(){
  60. return numberValue;
  61. }
  62. }
  63. public Number[] number4List;
  64. public Number[] getFavNumber4Value() {
  65. number4List = new Number[5];
  66. number4List[0] = new Number("Number4 - 1", "1");
  67. number4List[1] = new Number("Number4 - 2", "2");
  68. number4List[2] = new Number("Number4 - 3", "3");
  69. number4List[3] = new Number("Number4 - 4", "4");
  70. number4List[4] = new Number("Number4 - 5", "5");
  71. return number4List;
  72. }
  73. public String getFavNumber4InString() {
  74. return Arrays.toString(favNumber4);
  75. }
  76. }

2. JSF Page

A JSF page to demonstrate the use “h:selectBooleanCheckbox” and “h:selectManyCheckbox” tags.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. xmlns:h="http://java.sun.com/jsf/html"
  6. xmlns:f="http://java.sun.com/jsf/core"
  7. >
  8. <h:body>
  9. <h1>JSF 2 checkboxes example</h1>
  10. <h:form>
  11. <h2>1. Single checkbox</h2>
  12. <h:selectBooleanCheckbox value="#{user.rememberMe}" /> Remember Me
  13. <h2>2. Mutiple checkboxes</h2>
  14. 1. Hard-coded with "f:selectItem" :
  15. <h:selectManyCheckbox value="#{user.favNumber1}">
  16. <f:selectItem itemValue="1" itemLabel="Number1 - 1" />
  17. <f:selectItem itemValue="2" itemLabel="Number1 - 2" />
  18. <f:selectItem itemValue="3" itemLabel="Number1 - 3" />
  19. <f:selectItem itemValue="4" itemLabel="Number1 - 4" />
  20. <f:selectItem itemValue="5" itemLabel="Number1 - 5" />
  21. </h:selectManyCheckbox>
  22. <br />
  23. 2. Generated by Array :
  24. <h:selectManyCheckbox value="#{user.favNumber2}">
  25. <f:selectItems value="#{user.favNumber2Value}" />
  26. </h:selectManyCheckbox>
  27. <br />
  28. 3. Generated by Map :
  29. <h:selectManyCheckbox value="#{user.favNumber3}">
  30. <f:selectItems value="#{user.favNumber3Value}" />
  31. </h:selectManyCheckbox>
  32. <br />
  33. 4. Generated by Object with var :
  34. <h:selectManyCheckbox value="#{user.favNumber4}">
  35. <f:selectItems value="#{user.favNumber4Value}" var="n"
  36. itemLabel="#{n.numberLabel}" itemValue="#{n.numberValue}" />
  37. </h:selectManyCheckbox>
  38. <br />
  39. <h:commandButton value="Submit" action="result" />
  40. <h:commandButton value="Reset" type="reset" />
  41. </h:form>
  42. </h:body>
  43. </html>

result.xhtml…

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. xmlns:h="http://java.sun.com/jsf/html"
  6. >
  7. <h:body>
  8. <h1>JSF 2 checkboxes example</h1>
  9. <h2>result.xhtml</h2>
  10. <ol>
  11. <li>user.rememberMe : #{user.rememberMe}</li>
  12. <li>user.favNumber1 : #{user.favNumber1InString}</li>
  13. <li>user.favNumber2 : #{user.favNumber2InString}</li>
  14. <li>user.favNumber3 : #{user.favNumber3InString}</li>
  15. <li>user.favNumber4 : #{user.favNumber4InString}</li>
  16. </ol>
  17. </h:body>
  18. </html>

3. Demo

When “submit” button is clicked, link to “result.xhtml” page and display the submitted checkboxe values.

How to checked checkbox’s value by default?

h:selectBooleanCheckbox

The value of “f:selectItem” tag is checked if the boolean value is set to true. In above example, if you set boolean property “rememberMe” to true :

  1. @ManagedBean(name="user")
  2. @SessionScoped
  3. public class UserBean{
  4. public boolean rememberMe = true;
  5. //...

The “rememberMe” checkbox value is checked by default.

h:selectManyCheckbox

The values of “f:selectItems” tag are checked if it matched to the “value” of “h:selectManyCheckbox” tag. In above example, if you set favNumber3 to {“1″,”3″} :

  1. @ManagedBean(name="user")
  2. @SessionScoped
  3. public class UserBean{
  4. public String[] favNumber3 = {"1","3"};
  5. //...

The “favNumber3″ checkboxes, “Number 1″ and “Number 3″ values are checked by default.

JSF 2 checkboxes example的更多相关文章

  1. JSF primefaces dataTable paginator 表格分页 问题

    当第一次查询返回list列表,分页1,2,3.....这是选择2,当前页面停留在第2页. 当再次查询后,因为使用的ajax,结果更新了,但当前页面依旧是第2页. 可以在jsf页面,datatable的 ...

  2. 关于JSF中immediate属性的总结(二)

    The immediate attribute in JSF is commonly misunderstood. If you don't believe me, check out Stack O ...

  3. jsf初学selectOneMenu 绑定与取值

    jsf 的selectOneMenu 最后生成的<select>标签.这里涉及到一个binding 起初一直不知道是干嘛的,后来参考了其他文章.就相当于在asp.net 中如:<as ...

  4. 使用JSF框架过程中的若干典型问题及其解决方案

    1.commandXxx点击后,不调用action中的方法: 原因1:xhtml后缀名的文件,最终也会转化为普通的html文件(这是熟悉JSF框架的关键.),commandXxx点击后不调用后台act ...

  5. jsf初学解决GlassFish Server 无法启动

    由于公司需要用JSF开发项目.公司同事都不熟悉,本人C# 转JSf.开发工具 netbeans GlassFish. 遇到GlassFish 非常 纠结的问题.搞了好一段时间,, 装好GlassFis ...

  6. JSF标签的使用2

    n  事件监听器是用于解决只影响用户界面的事件 Ø  特别地,在beans的form数据被加载和触发验证前被调用 •    用immediate=“true”指明这个行为不触发验证 Ø  在监听器调用 ...

  7. JSF 与 HTML 标签的联系

    *页面的开头 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ t ...

  8. Parameter Passing / Request Parameters in JSF 2.0 (转)

    This Blog is a compilation of various methods of passing Request Parameters in JSF (2.0 +) (1)  f:vi ...

  9. JSF中使用jquery拦截ajax请求

    jsf提供一个内置的jsf.ajax.request方法给我们使用,如果在jquery中使用,则需要做一些更改.  此处因为使用jquery,所以可以不必在控件中添加onclick方法了,可以给控件配 ...

随机推荐

  1. apk反编译(7)用ProGuard混淆代码,初级防止反编译

    eclipse为例 1,project.properties去掉 #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:pro ...

  2. Camel、Pastal、匈牙利标记法区别及联系

    在英语中,依靠单词的大小写拼写复合词的做法,叫做"骆驼拼写法"(CamelCase).比如,backColor这个复合词,color的第一个字母采用大写. 这种拼写法在正规的英语中 ...

  3. 1033. Labyrinth(dfs)

    1033 简单dfs 有一点小小的坑 就是图可能不连通 所以要从左上和右下都搜一下 加起来 从讨论里看到的 讨论里看到一句好无奈的回复 “可不可以用中文呀...” #include <iostr ...

  4. bzoj2829

    裸题,直接上凸包,然后加上一个圆周即可 只是在这之前没写过旋转而已 const pi=3.14159265358979323; eps=1e-8; type point=record x,y:doub ...

  5. BZOJ3806: Neerc2011 Dictionary Size

    题解: 这题搞得我真是酸(dan)爽(teng) 原来一直不会,一定会用到什么神奇的东西.因为重复的不知道如何计算. 今天中午睡起来忽然想到好像可以在正trie上故意走无出边,因为这样就保证了这次统计 ...

  6. 高斯消元与xor方程组

    ;i<=n;i++) { ;j<=n;j++) if(a[j]>a[i]) swap(a[i],a[j]); if(!a[i]) break; ;j>=;j--) ) { ;k ...

  7. Linux shell下批量创建缩略图

    一.背景 今天,突然发现手机客户端上的最新新闻缩略图都不显示了,上服务器上看了看, 发现新的新闻图片根本没有生成缩略图. 这套新闻发布系统是很老的程序了,查了一下,问题的原因是不支持png格式的图片, ...

  8. 解决hibernate向mysql插入中文乱码问题

    一.mysql的问题解决 MySQL会出现中文乱码的原因不外乎下列几点:   1.server本身设定问题,例如还停留在latin1   2.table的语系设定问题(包含character与coll ...

  9. Java [Leetcode 102]Binary Tree Level Order Traversal

    题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  10. Java [Leetcode 83]Remove Duplicates from Sorted List

    题目描述: Given a sorted linked list, delete all duplicates such that each element appear only once. For ...