JSF 表单单选按钮示例

2018-02-20 13:43 更新

JSF教程 - JSF表单单选按钮示例


以下部分显示如何使用JSF中的创建单选按钮。

h:selectOneRadio标签呈现一组类型为“radio”的HTML输入元素,并使用HTML表格和标签标签对其进行格式化。

下面的JSF代码

<h:selectOneRadio value="#{userData.data}">
   <f:selectItem itemValue="1" itemLabel="Item 1" />
   <f:selectItem itemValue="2" itemLabel="Item 2" />           
</h:selectOneRadio>

被渲染成以下HTML代码

<table>
   <tr>
      <td><input type="radio" checked="checked" name="j_idt6:j_idt8" 
            id="j_idt6:j_idt8:0" value="1" />
         <label for="j_idt6:j_idt8:0"> Item 1</label></td>
      <td><input type="radio" name="j_idt6:j_idt8" 
            id="j_idt6:j_idt8:1" value="2" />
         <label for="j_idt6:j_idt8:1"> Item 2</label></td>
   </tr>
</table>

标签属性

属性描述
id标签的标识
binding引用在backing bean中使用的组件
rendered布尔值; false将抑制渲染
styleClass级联样式表(CSS)类名称
value值绑定
valueChangeListener响应值更改的方法绑定
converter转换器类名
validator附加到组件的验证器的类名
required布尔值; 如果为true,则根据需要标记标签
accesskey给予一个元素的焦点
accept表单的内容类型的逗号分隔列表
accept-charset表单的字符编码的逗号或空格分隔列表。
alt非文字元素(例如图片)的替代文字
border元素的边框宽度的像素值
charset链接资源的字符编码
coords形状为矩形,圆形或多边形的元素的坐标
dir文本的方向。 有效值为 ltr (从左到右)和 rtl (从右到左)。
disabled输入元素或按钮的禁用状态
hreflang使用 href 属性指定的资源的基本语言;
lang元素的属性和文本的基本语言
maxlength文本字段的最大字符数
readonly输入字段的只读状态
style内联样式信息
tabindex指定制表符索引的数值
target打开文档的框架的名称
title用于辅助功能的标题。 浏览器通常为标题的值创建工具提示
type链接类型; 例如样式表
width元素的宽度
onblur失去焦点的事件处理程序
onchange值更改的事件处理程序
onclick鼠标按钮的事件处理程序点击该元素
ondblclick双击鼠标按钮的事件处理程序
onfocus元素接收焦点的事件处理程序
onkeydown按键的事件处理程序
onkeypress键按下并释放的事件处理程序
onkeyupKey的事件处理程序发布
onmousedown鼠标按钮的事件处理程序
onmousemove鼠标移动的事件处理程序
onmouseout鼠标左的事件处理程序
onmouseover鼠标移动到的事件处理程序
onmouseup释放鼠标按钮的事件处理程序
onreset表单重置的事件处理程序
onselect选择文本的事件处理程序
immediate在生命周期的早期进行过程验证

硬编码RadioButton

以下代码来自demo.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"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
      <h:form>
        Hard-coded with "f:selectItem": 
       <h:selectOneRadio value="#{user.item}">
         <f:selectItem itemValue="Red" itemLabel="Color - Red" />
         <f:selectItem itemValue="Green" itemLabel="Color - Green" />
         <f:selectItem itemValue="Blue" itemLabel="Color - Blue" />
       </h:selectOneRadio>

    <br /><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>
  <p>Selected: #{user.item}</p>
</h:body>
</html>

下面的代码来自UserBean.java。

package cn.w3cschool.common;

import java.io.Serializable;
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 implements Serializable{
  public String item;
  
  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }
}

下载 Form_RadioButton_Hardcode.zip

RadioButton内部项目

以下代码来自demo.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"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
      <h:form>
        Generated by Object array and iterate with var :
       <h:selectOneRadio value="#{user.item}">
         <f:selectItems value="#{user.itemValue}" var="c"
         itemLabel="#{c.label}" itemValue="#{c.value}" />
       </h:selectOneRadio>

    <br /><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>
  <p>Selected: #{user.item}</p>
</h:body>
</html>

下面的代码来自UserBean.java。

package cn.w3cschool.common;

import java.io.Serializable;
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 implements Serializable{
  public String item;
  
  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }
  //Generated by Object array
  public static class Item{
    public String label;
    public String value;
    
    public Item(String l, String v){
      this.label = l;
      this.value = v;
    }
    
    public String getLabel(){
      return label;
    }
    
    public String getValue(){
      return value;
    }
  }
  public Item[] itemList;
  
  public Item[] getItemValue() {
    
    itemList = new Item[3];
    itemList[0] = new Item("Color - Red", "Red");
    itemList[1] = new Item("Color - Green", "Green");
    itemList[2] = new Item("Color - Blue", "Blue");
    
    return itemList;
  }  
}

下载 Form_RadioButton_Inner_Item.zip

映射生成的单选按钮

下面的代码来自UserBean.java。

package cn.w3cschool.common;

import java.io.Serializable;
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 implements Serializable{
  public String item;
  
  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }
  //Generated by Map
  private static Map<String,Object> itemValue;
  static{
    itemValue = new LinkedHashMap<String,Object>();
    itemValue.put("Color - Red", "Red"); //label, value
    itemValue.put("Color - Green", "Green");
    itemValue.put("Color - Blue", "Blue");
  }
  
  public Map<String,Object> getItemValue() {
    return itemValue;
  }  
}

以下代码来自demo.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"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
      <h:form>
        Generated by Map :
       <h:selectOneRadio value="#{user.item}">
         <f:selectItems value="#{user.itemValue}" />
       </h:selectOneRadio>

    <br /><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>
  <p>Selected: #{user.item}</p>
</h:body>
</html>
下载 Form_RadioButton_Generated_By_Map.zip

运行

将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。

Tomcat完成启动后,在浏览器地址栏中键入以下URL。

http://localhost:8080/simple-webapp/demo.xhtml
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号