[wp_ad_camp_5]
This post shows sample codes that uses JSF
html:selectOneRadio
.
Requirements
Stuff used in this post.
- JSF 2.2
- javax.faces-2.2.8.jar
- JDK 8
JSF Managed Bean
This is our managed bean annotated with @ManagedBean
. The 3 important piece of codes here are:
- @ManagedBean
- Default public no-arg constructor
- Setter and getter methods
[wp_ad_camp_4]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.turreta.jsf.bean; import javax.faces.bean.ManagedBean; @ManagedBean public class MyBean { public MyBean() {} private String letter; public String getLetter() { return letter; } public void setLetter(String letter) { this.letter = letter; } } |
First Page – Request
The first page is where the radio buttons are displayed. Users selects one item and hits the Submit
button to go to the response page.
[wp_ad_camp_3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head> <title>Please select letter</title> </h:head> <h:body> <h:form> <h:selectOneRadio value="#{myBean.letter}"> <f:selectItem itemValue="a" itemLabel="A"/> <f:selectItem itemValue="b" itemLabel="B"/> <f:selectItem itemValue="c" itemLabel="C"/> </h:selectOneRadio> <h:commandButton value="Submit" action="response"/> </h:form> </h:body> </html> |
Second Page – Response
This page displays the selected letter from the first page.
[wp_ad_camp_2]
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"> <h:head> <title>Please select letter - Response</title> </h:head> <h:body> You're letter is #{myBean.letter} </h:body> </html> |
Testing
Request Page
[wp_ad_camp_1]
Response Page