JSF 2 checkboxes example
In JSF, <h:selectBooleanCheckbox />
tag is used to render a single HTML input element of “checkbox
” type.
//JSF...
<h:selectBooleanCheckbox value="#{user.rememberMe}" /> Remember Me
//HTML output...
<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.
//JSF...
<h:selectManyCheckbox value="#{user.favNumber1}">
<f:selectItem itemValue="1" itemLabel="Number1 - 1" />
<f:selectItem itemValue="2" itemLabel="Number1 - 2" />
<f:selectItem itemValue="3" itemLabel="Number1 - 3" />
</h:selectManyCheckbox>
//HTML output...
<table>
<tr>
<td>
<input name="j_idt6:j_idt10" id="j_idt6:j_idt10:0" value="1" type="checkbox" />
<label for="j_idt6:j_idt10:0" class=""> Number1 - 1</label></td>
<td>
<input name="j_idt6:j_idt10" id="j_idt6:j_idt10:1" value="2" type="checkbox" />
<label for="j_idt6:j_idt10:1" class=""> Number1 - 2</label></td>
<td>
<input name="j_idt6:j_idt10" id="j_idt6:j_idt10:2" value="3" type="checkbox" />
<label for="j_idt6:j_idt10:2" class=""> Number1 - 3</label></td>
<td>
</tr>
</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.
package com.mkyong;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public boolean rememberMe;
public String[] favNumber1;
public String[] favNumber2;
public String[] favNumber3;
public String[] favNumber4;
//getter and setter methods...
public String getFavNumber1InString() {
return Arrays.toString(favNumber1);
}
//Generated by Array
public String[] getFavNumber2Value() {
favNumber2 = new String[5];
favNumber2[0] = "Number2 - 1";
favNumber2[1] = "Number2 - 2";
favNumber2[2] = "Number2 - 3";
favNumber2[3] = "Number2 - 4";
favNumber2[4] = "Number2 - 5";
return favNumber2;
}
public String getFavNumber2InString() {
return Arrays.toString(favNumber2);
}
//Generated by Map
private static Map<String,Object> number3Value;
static{
number3Value = new LinkedHashMap<String,Object>();
number3Value.put("Number3 - 1", "1"); //label, value
number3Value.put("Number3 - 2", "2");
number3Value.put("Number3 - 3", "3");
number3Value.put("Number3 - 4", "4");
number3Value.put("Number3 - 5", "5");
}
public Map<String,Object> getFavNumber3Value() {
return number3Value;
}
public String getFavNumber3InString() {
return Arrays.toString(favNumber3);
}
//Generated by Object array
public static class Number{
public String numberLabel;
public String numberValue;
public Number(String numberLabel, String numberValue){
this.numberLabel = numberLabel;
this.numberValue = numberValue;
}
public String getNumberLabel(){
return numberLabel;
}
public String getNumberValue(){
return numberValue;
}
}
public Number[] number4List;
public Number[] getFavNumber4Value() {
number4List = new Number[5];
number4List[0] = new Number("Number4 - 1", "1");
number4List[1] = new Number("Number4 - 2", "2");
number4List[2] = new Number("Number4 - 3", "3");
number4List[3] = new Number("Number4 - 4", "4");
number4List[4] = new Number("Number4 - 5", "5");
return number4List;
}
public String getFavNumber4InString() {
return Arrays.toString(favNumber4);
}
}
2. JSF Page
A JSF page to demonstrate the use “h:selectBooleanCheckbox
” and “h:selectManyCheckbox
” tags.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:body>
<h1>JSF 2 checkboxes example</h1>
<h:form>
<h2>1. Single checkbox</h2>
<h:selectBooleanCheckbox value="#{user.rememberMe}" /> Remember Me
<h2>2. Mutiple checkboxes</h2>
1. Hard-coded with "f:selectItem" :
<h:selectManyCheckbox value="#{user.favNumber1}">
<f:selectItem itemValue="1" itemLabel="Number1 - 1" />
<f:selectItem itemValue="2" itemLabel="Number1 - 2" />
<f:selectItem itemValue="3" itemLabel="Number1 - 3" />
<f:selectItem itemValue="4" itemLabel="Number1 - 4" />
<f:selectItem itemValue="5" itemLabel="Number1 - 5" />
</h:selectManyCheckbox>
<br />
2. Generated by Array :
<h:selectManyCheckbox value="#{user.favNumber2}">
<f:selectItems value="#{user.favNumber2Value}" />
</h:selectManyCheckbox>
<br />
3. Generated by Map :
<h:selectManyCheckbox value="#{user.favNumber3}">
<f:selectItems value="#{user.favNumber3Value}" />
</h:selectManyCheckbox>
<br />
4. Generated by Object with var :
<h:selectManyCheckbox value="#{user.favNumber4}">
<f:selectItems value="#{user.favNumber4Value}" var="n"
itemLabel="#{n.numberLabel}" itemValue="#{n.numberValue}" />
</h:selectManyCheckbox>
<br />
<h:commandButton value="Submit" action="result" />
<h:commandButton value="Reset" type="reset" />
</h:form>
</h:body>
</html>
result.xhtml…
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>
<h:body>
<h1>JSF 2 checkboxes example</h1>
<h2>result.xhtml</h2>
<ol>
<li>user.rememberMe : #{user.rememberMe}</li>
<li>user.favNumber1 : #{user.favNumber1InString}</li>
<li>user.favNumber2 : #{user.favNumber2InString}</li>
<li>user.favNumber3 : #{user.favNumber3InString}</li>
<li>user.favNumber4 : #{user.favNumber4InString}</li>
</ol>
</h:body>
</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
:
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public boolean rememberMe = true;
//...
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″} :
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String[] favNumber3 = {"1","3"};
//...
The “favNumber3
″ checkboxes, “Number 1″ and “Number 3″ values are checked by default.
JSF 2 checkboxes example的更多相关文章
- JSF primefaces dataTable paginator 表格分页 问题
当第一次查询返回list列表,分页1,2,3.....这是选择2,当前页面停留在第2页. 当再次查询后,因为使用的ajax,结果更新了,但当前页面依旧是第2页. 可以在jsf页面,datatable的 ...
- 关于JSF中immediate属性的总结(二)
The immediate attribute in JSF is commonly misunderstood. If you don't believe me, check out Stack O ...
- jsf初学selectOneMenu 绑定与取值
jsf 的selectOneMenu 最后生成的<select>标签.这里涉及到一个binding 起初一直不知道是干嘛的,后来参考了其他文章.就相当于在asp.net 中如:<as ...
- 使用JSF框架过程中的若干典型问题及其解决方案
1.commandXxx点击后,不调用action中的方法: 原因1:xhtml后缀名的文件,最终也会转化为普通的html文件(这是熟悉JSF框架的关键.),commandXxx点击后不调用后台act ...
- jsf初学解决GlassFish Server 无法启动
由于公司需要用JSF开发项目.公司同事都不熟悉,本人C# 转JSf.开发工具 netbeans GlassFish. 遇到GlassFish 非常 纠结的问题.搞了好一段时间,, 装好GlassFis ...
- JSF标签的使用2
n 事件监听器是用于解决只影响用户界面的事件 Ø 特别地,在beans的form数据被加载和触发验证前被调用 • 用immediate=“true”指明这个行为不触发验证 Ø 在监听器调用 ...
- JSF 与 HTML 标签的联系
*页面的开头 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ t ...
- 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 ...
- JSF中使用jquery拦截ajax请求
jsf提供一个内置的jsf.ajax.request方法给我们使用,如果在jquery中使用,则需要做一些更改. 此处因为使用jquery,所以可以不必在控件中添加onclick方法了,可以给控件配 ...
随机推荐
- C#中控件的CheckState和Checked属性区别?
Checked 和CheckState都是检查控件选中状态,都能判断是否选中控件. 只是Checked 通过布尔判断(true & false): CheckState 通过枚举判断. che ...
- 【HDOJ】4080 Stammering Aliens
1. 题目描述给定一个长为$n \in [1, 4000]$的字符串,求其中长度最长的子串,并且该子串在原串中出现至少$m$次,并求最右起始位置. 2. 基本思路两种方法:二分+后缀数组,或者二分+哈 ...
- 迷宫问题(bfs的应用)
问题描述: 定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, ...
- Android log日志
LOG是用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用. androidsdk中提供了l ...
- Bitset小结 (POJ2443 & HDU4920)
学了下bitset用法,从网上找的一些bitset用法,并从中调出一些常用的用法. 构造函数bitset<n> b; b有n位,每位都为0.参数n可以为一个表达式.如bitset<5 ...
- uvaIrrelevant Elements
唯一分解定理. 可以看出在最后每个a的系数是杨辉三角的第n行. 但是不能递推,否则会tle. 就从C(n-1,0)开始乘n-k再除以k.记录下每个的系数,如果该项系数小于m就代表和答案有关. 代码里的 ...
- uva 10047 The Monocycle(搜索)
好复杂的样子..其实就是纸老虎,多了方向.颜色两个状态罢了,依旧是bfs. 更新的时候注意处理好就行了,vis[][][][]要勇敢地开. 不过这个代码交了十几遍的submission error,手 ...
- cocos2d-x 滚动文字(二)
http://blog.csdn.net/kuovane/article/details/8131789 首先送上demo,下载地址为:demo下载地址 一,怎么在文字前面空两隔?只需在xml里的文字 ...
- LeetCode Binary Tree Preorder Traversal 先根遍历
题意:给一棵树,求其先根遍历的结果. 思路: (1)深搜法: /** * Definition for a binary tree node. * struct TreeNode { * int va ...
- Tomcat 调优总结
一. jvm参数调优 常见的生产环境tomcat启动脚本里常见如下的参数,我们依次来解释各个参数意义. export JAVA_OPTS="-server -Xms1400M -Xmx140 ...