JSF 2 radio buttons example
In JSF, “h:selectOneRadio
” tag is used to render a set of HTML input element of type “radio
“, and format it with HTML table and label tag.
//JSF...
<h:selectOneRadio value="#{user.favColor1}">
<f:selectItem itemValue="Red" itemLabel="Color1 - Red" />
<f:selectItem itemValue="Green" itemLabel="Color1 - Green" />
<f:selectItem itemValue="Blue" itemLabel="Color1 - Blue" />
</h:selectOneRadio>
//HTML output...
<table>
<tr>
<td>
<input type="radio" name="j_idt6:j_idt8" id="j_idt6:j_idt8:0" value="Red" />
<label for="j_idt6:j_idt8:0"> Color1 - Red</label></td>
<td>
<input type="radio" name="j_idt6:j_idt8" id="j_idt6:j_idt8:1" value="Green" />
<label for="j_idt6:j_idt8:1"> Color1 - Green</label></td>
<td>
<input type="radio" name="j_idt6:j_idt8" id="j_idt6:j_idt8:2" value="Blue" />
<label for="j_idt6:j_idt8:2"> Color1 - Blue</label>
</td>
</tr>
</table>
JSF 2.0 “h:selectOneRadio” example
A JSF 2.0 example to show the use of “h:selectOneRadio
” tag to render radio buttons, and populate the data in 3 different ways :
- Hardcoded values in “
f:selectItem
” 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 a “var
” attribute.
1. Backing Bean
A backing bean to hold the submitted data.
package com.mkyong;
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 String favColor1;
public String favColor2;
public String favColor3;
//getter and setter methods
//Generated by Map
private static Map<String,Object> color2Value;
static{
color2Value = new LinkedHashMap<String,Object>();
color2Value.put("Color2 - Red", "Red"); //label, value
color2Value.put("Color2 - Green", "Green");
color2Value.put("Color3 - Blue", "Blue");
}
public Map<String,Object> getFavColor2Value() {
return color2Value;
}
//Generated by Object array
public static class Color{
public String colorLabel;
public String colorValue;
public Color(String colorLabel, String colorValue){
this.colorLabel = colorLabel;
this.colorValue = colorValue;
}
public String getColorLabel(){
return colorLabel;
}
public String getColorValue(){
return colorValue;
}
}
public Color[] color3List;
public Color[] getFavColor3Value() {
color3List = new Color[3];
color3List[0] = new Color("Color3 - Red", "Red");
color3List[1] = new Color("Color3 - Green", "Green");
color3List[2] = new Color("Color3 - Blue", "Blue");
return color3List;
}
}
2. JSF Page
A JSF page to demonstrate the use “h:selectOneRadio
” tag.
<?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 radio button example</h1>
<h:form>
1. Hard-coded with "f:selectItem" :
<h:selectOneRadio value="#{user.favColor1}">
<f:selectItem itemValue="Red" itemLabel="Color1 - Red" />
<f:selectItem itemValue="Green" itemLabel="Color1 - Green" />
<f:selectItem itemValue="Blue" itemLabel="Color1 - Blue" />
</h:selectOneRadio>
<br />
2. Generated by Map :
<h:selectOneRadio value="#{user.favColor2}">
<f:selectItems value="#{user.favColor2Value}" />
</h:selectOneRadio>
<br />
3. Generated by Object array and iterate with var :
<h:selectOneRadio value="#{user.favColor3}">
<f:selectItems value="#{user.favColor3Value}" var="c"
itemLabel="#{c.colorLabel}" itemValue="#{c.colorValue}" />
</h:selectOneRadio>
<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 radio button example</h1>
<h2>result.xhtml</h2>
<ol>
<li>user.favColor1 : #{user.favColor1}</li>
<li>user.favColor2 : #{user.favColor2}</li>
<li>user.favColor3 : #{user.favColor3}</li>
</ol>
</h:body>
</html>
3. Demo
When “submit” button is clicked, link to “result.xhtml” and display the submitted radio button values.
How to select radio button value by default?
In JSF, the radio button values of “f:selectItems
” tag is selected if it matched to the “value” of “h:selectOneRadio
” tag. In above example, if you set favColor2 to “Red” :
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String[] favColor = "Red";
//...
The “favColor2″ radio button, “Red” option is selected by default.
JSF 2 radio buttons example的更多相关文章
- [Angular2 Form] Create Radio Buttons for Angular 2 Forms
Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...
- mfc Radio Buttons
添加单选按钮 关联变量 调试宏TRACE BOOL类型 一.添加一组单选按钮 二.添加第二组单选按钮 三.关联变量 四.单选按钮运用 void CMY_Dialog::OnBnClickedButto ...
- Reading CheckBoxes and Radio Buttons
Input tags with the type attribute checkbox can be grouped like radio buttons so that several checkb ...
- playwright-python 处理Text input、Checkboxs 和 radio buttons(三)
Text input 输入框输入元素,直接用fill方法即可,支持 <input>,<textarea>, [contenteditable] 和<label>这些 ...
- 前端:Jquery 处理同一Name的Radio组时,绑定checked属性异常的问题.(已解决)
将: $("input[type=radio][name=optionsContractGroup][value=201]").attr("checked",t ...
- How to get the value of a form element : check box and radio button
Getting a radio element and it’s checked value Radio buttons in a form can be grouped together using ...
- Javascript Get or Set Checked Radio Value
Description This pair of Javascript function can get or set the checked value of a group of radio bu ...
- [label][翻译][JavaScript]如何使用JavaScript操纵radio和check boxes
Radio 和 check boxes是form表单中的一部分,允许用户通过鼠标简单点击就可以选择.当与<textarea>元素的一般JavaScript操纵相比较,这些表单控件(form ...
- HTML基础
HTML指的是超文本标记语言 (Hyper Text Markup Language), HTML不是一种编程语言,而是一种标记语言 (markup language) ,HTML使用标记标签来描述网 ...
随机推荐
- apk反编译(1)用apktool破解apk
1,下载 http://ibotpeaches.github.io/Apktool/ 2,破解 把下载的apktool_2.0.3.jar 和 weixin638android680.apk 拷贝到 ...
- WinCE启动次数的记录
最近一周一直在忙于测试NAND文件系统的稳定性和可靠性,今天终于有所进展.测试组所有同事齐上阵,加上小高和我,测试了一天,都未发现问题.虽然还不能保证完全OK,但至少有所改善了. 测试组今天主要做了文 ...
- C#开发BHO插件UrlTrack
最近忽然突发奇想,想统计一下我最经常上的网站是哪些,并且在这些网站上都停留了多久.为此决定写一个BHO插件来做这件事. BHO(Browser Help Objects)是实现了特定接口(IObjec ...
- Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝
一.为驳回编译器自动提供的机能,可将相应成员函数声明为private并且不予实现.(如果你仅仅是自己不实现的话,编译器会帮你实现) 如: class A { public: A(const strin ...
- 【转】那些不能错过的Xcode插件 -- 不错不错
原文网址:http://www.cocoachina.com/industry/20130918/7022.html 古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战斗力的绝佳 ...
- 是时候学习Android分屏开发了
今年Google发布了Android N,Android N新增了不少功能,最受关注的自然就是分屏了. 这一功能对国内的很多手机用户并不陌生,其实很多第三方系统早已经实现了这一功能,如EMUI,Fly ...
- [Papers]NSE, $u_3$, Lebesgue space [Cao-Titi, IUMJ, 2008]
$$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{2}{3}+\frac{2}{3q},\quad \fra ...
- HDU5715 XOR 游戏 二分+字典树+dp
当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...
- SDUT 3568 Rock Paper Scissors 状压统计
就是改成把一个字符串改成三进制状压,然后分成前5位,后5位统计, 然后直接统计 f[i][j][k]代表,后5局状压为k的,前5局比和j状态比输了5局的有多少个人 复杂度是O(T*30000*25*m ...
- bzoj 1001: [BeiJing2006]狼抓兔子 平面图最小割
平面图跑最大流 可以转换为其对偶图跑最短路 一个环对应一个割 找到最小环(即最短路)极为所求,注意辅助边的建立 加入读入优化 不过时间还是一般 估计是dij写的不好 大神勿喷~~~ /*** ...