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.

  1. //JSF...
  2. <h:selectOneRadio value="#{user.favColor1}">
  3. <f:selectItem itemValue="Red" itemLabel="Color1 - Red" />
  4. <f:selectItem itemValue="Green" itemLabel="Color1 - Green" />
  5. <f:selectItem itemValue="Blue" itemLabel="Color1 - Blue" />
  6. </h:selectOneRadio>
  1. //HTML output...
  2. <table>
  3. <tr>
  4. <td>
  5. <input type="radio" name="j_idt6:j_idt8" id="j_idt6:j_idt8:0" value="Red" />
  6. <label for="j_idt6:j_idt8:0"> Color1 - Red</label></td>
  7. <td>
  8. <input type="radio" name="j_idt6:j_idt8" id="j_idt6:j_idt8:1" value="Green" />
  9. <label for="j_idt6:j_idt8:1"> Color1 - Green</label></td>
  10. <td>
  11. <input type="radio" name="j_idt6:j_idt8" id="j_idt6:j_idt8:2" value="Blue" />
  12. <label for="j_idt6:j_idt8:2"> Color1 - Blue</label>
  13. </td>
  14. </tr>
  15. </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.

  1. package com.mkyong;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import javax.faces.bean.ManagedBean;
  5. import javax.faces.bean.SessionScoped;
  6. @ManagedBean(name="user")
  7. @SessionScoped
  8. public class UserBean{
  9. public String favColor1;
  10. public String favColor2;
  11. public String favColor3;
  12. //getter and setter methods
  13. //Generated by Map
  14. private static Map<String,Object> color2Value;
  15. static{
  16. color2Value = new LinkedHashMap<String,Object>();
  17. color2Value.put("Color2 - Red", "Red"); //label, value
  18. color2Value.put("Color2 - Green", "Green");
  19. color2Value.put("Color3 - Blue", "Blue");
  20. }
  21. public Map<String,Object> getFavColor2Value() {
  22. return color2Value;
  23. }
  24. //Generated by Object array
  25. public static class Color{
  26. public String colorLabel;
  27. public String colorValue;
  28. public Color(String colorLabel, String colorValue){
  29. this.colorLabel = colorLabel;
  30. this.colorValue = colorValue;
  31. }
  32. public String getColorLabel(){
  33. return colorLabel;
  34. }
  35. public String getColorValue(){
  36. return colorValue;
  37. }
  38. }
  39. public Color[] color3List;
  40. public Color[] getFavColor3Value() {
  41. color3List = new Color[3];
  42. color3List[0] = new Color("Color3 - Red", "Red");
  43. color3List[1] = new Color("Color3 - Green", "Green");
  44. color3List[2] = new Color("Color3 - Blue", "Blue");
  45. return color3List;
  46. }
  47. }

2. JSF Page

A JSF page to demonstrate the use “h:selectOneRadio” tag.

  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 radio button example</h1>
  10. <h:form>
  11. 1. Hard-coded with "f:selectItem" :
  12. <h:selectOneRadio value="#{user.favColor1}">
  13. <f:selectItem itemValue="Red" itemLabel="Color1 - Red" />
  14. <f:selectItem itemValue="Green" itemLabel="Color1 - Green" />
  15. <f:selectItem itemValue="Blue" itemLabel="Color1 - Blue" />
  16. </h:selectOneRadio>
  17. <br />
  18. 2. Generated by Map :
  19. <h:selectOneRadio value="#{user.favColor2}">
  20. <f:selectItems value="#{user.favColor2Value}" />
  21. </h:selectOneRadio>
  22. <br />
  23. 3. Generated by Object array and iterate with var :
  24. <h:selectOneRadio value="#{user.favColor3}">
  25. <f:selectItems value="#{user.favColor3Value}" var="c"
  26. itemLabel="#{c.colorLabel}" itemValue="#{c.colorValue}" />
  27. </h:selectOneRadio>
  28. <br />
  29. <h:commandButton value="Submit" action="result" />
  30. <h:commandButton value="Reset" type="reset" />
  31. </h:form>
  32. </h:body>
  33. </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 radio button example</h1>
  9. <h2>result.xhtml</h2>
  10. <ol>
  11. <li>user.favColor1 : #{user.favColor1}</li>
  12. <li>user.favColor2 : #{user.favColor2}</li>
  13. <li>user.favColor3 : #{user.favColor3}</li>
  14. </ol>
  15. </h:body>
  16. </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” :

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

The “favColor2″ radio button, “Red” option is selected by default.

JSF 2 radio buttons example的更多相关文章

  1. [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 ...

  2. mfc Radio Buttons

    添加单选按钮 关联变量 调试宏TRACE BOOL类型 一.添加一组单选按钮 二.添加第二组单选按钮 三.关联变量 四.单选按钮运用 void CMY_Dialog::OnBnClickedButto ...

  3. Reading CheckBoxes and Radio Buttons

    Input tags with the type attribute checkbox can be grouped like radio buttons so that several checkb ...

  4. playwright-python 处理Text input、Checkboxs 和 radio buttons(三)

    Text input 输入框输入元素,直接用fill方法即可,支持 <input>,<textarea>, [contenteditable] 和<label>这些 ...

  5. 前端:Jquery 处理同一Name的Radio组时,绑定checked属性异常的问题.(已解决)

    将: $("input[type=radio][name=optionsContractGroup][value=201]").attr("checked",t ...

  6. 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 ...

  7. 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 ...

  8. [label][翻译][JavaScript]如何使用JavaScript操纵radio和check boxes

    Radio 和 check boxes是form表单中的一部分,允许用户通过鼠标简单点击就可以选择.当与<textarea>元素的一般JavaScript操纵相比较,这些表单控件(form ...

  9. HTML基础

    HTML指的是超文本标记语言 (Hyper Text Markup Language), HTML不是一种编程语言,而是一种标记语言 (markup language) ,HTML使用标记标签来描述网 ...

随机推荐

  1. Error Curves(2010成都现场赛题)

    F - Error Curves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

  2. basicjava

    .完数 . 第一个完全数是6,它有约数1.2.3.6,除去它本身6外,其余3个数相加,1+2+3=6.第二个完全数是28,它有约数1.2.4.7.14. 28,除去它本身28外,其余5个数相加,1+2 ...

  3. java中的clone

    .clone 要实现cloneable接口: .深度clone和浅度clone .对象.clone() 1. Clone&Copy      假设现在有一个Employee对象,Employe ...

  4. JAVA使用原始HttpURLConnection发送POST数据

    package com.newflypig.demo; /** * 使用jdk自带的HttpURLConnection向URL发送POST请求并输出响应结果 * 参数使用流传递,并且硬编码为字符串&q ...

  5. 怎样衡量一个组员在团队中的Performance

    My Attitude: 我认为评价一个团队的成员要看贡献,一切的Personal Ability, Attitude都要以这个为前提. Principal: 公平的原则+推动团队的发展 二者在大部分 ...

  6. wince和window mobile winphone

    windows mobile是微软在2000年左右推出的针对移动平台的操作系统,这个系统一直使用到三年前,微软开始启用metro界面,将windows mobile改名为windows phone. ...

  7. ASP.NET中MEMCACHED

    一,准备        你需要有一下软件:       VS.NET(05/08)       SQLSERVER       memcached服务器端以及客户端类库(开源软件,下载即可)其中,客户 ...

  8. 264分析两大利器:264VISA和Elecard StreamEye Tools

    学了264有将近3个月有余,好多时候都在学习老毕的书和反复看JM86的代码,最近才找到264分析两大利器:264VISA和Elecard StreamEye Tools.不由得感叹,恨不逢同时. 简单 ...

  9. poj 2762 Going from u to v or from v to u?

    题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...

  10. 【转】有趣的Autolayout示例-Masonry实现

    原文网址:http://tutuge.me/2015/05/23/autolayout-example-with-masonry/ 好久没有写Blog了,这段时间有点忙啊=.=本文举了3个比较有“特点 ...