JSF – 22 – JSF HTML Tag – 06 – Selection Tags – 02 – Radio Button

Merhaba Arkadaslar
Bu bolumde h:selectOneRadio tag’ini inceleyecegiz. h:selectOneRadio tag’i HTML input type radio element’ine donusturulur.

h:selectOneRadio ve f:selectItem tag’larini kullanarak hardcoded olarak radio button ekleyebiliriz.

radiobutton.xhtml

...
<h1>h:selectOneRadio examples</h1>

<h2>f:selectItem</h2>
<h:selectOneRadio value="#{radioButtonBean.gender}">
   <f:selectItem itemValue="Man" itemLabel="Mr" />
   <f:selectItem itemValue="Woman" itemLabel="Mrs" />
</h:selectOneRadio>
...

h:selectOneRadio tag’i HTML input type radio elementine donusur.

<table>
<tbody>
<tr>
   <td>
       <input type="radio" name="j_idt6" id="j_idt6:0" value="Man"><label for="j_idt6:0"> Mr</label>
    </td>
   <td>
       <input type="radio" name="j_idt6" id="j_idt6:1" value="Woman"><label for="j_idt6:1"> Mrs</label>
   </td>
 </tr>
</tbody>
</table>

RadioButtonBean.java

package _26.radiobutton;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;


@ManagedBean
public class RadioButtonBean {

	private String gender;

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}
...
}

f:selectItems tag’ini array ile kullanabiliriz. Benzer ornegi checkbox ornegi ile onceki bolumde yapmistik. f:selectItems taginda #{radioButtonBean.preparedMaritalStatus} seklinde kullandik boylelikle getPreparedMaritalStatus metodumu calisacaktir. Bu metot geriye String[] arrayi dondurmektedir.

radiobutton.xhtml

...
<h2>f:selectItems with array</h2>

<h:selectOneRadio value="#{radioButtonBean.maritalStatus}">
    <f:selectItems value="#{radioButtonBean.preparedMaritalStatus}" />
</h:selectOneRadio>
...

RadioButtonBean.java

      private String maritalStatus;
	
	public String getMaritalStatus() {
		return maritalStatus;
	}

	public void setMaritalStatus(String maritalStatus) {
		this.maritalStatus = maritalStatus;
	}

	public String[] getPreparedMaritalStatus() {
		return new String[]{"Married", "Engaged", "Single", "Java Man :)"};
	}

f:selectItems tag’inda array kullandigimiz gibi map de kullanabiliyorduk.

radiobutton.xhtml

<h2>f:selectItems with map</h2>

<h:selectOneRadio value="#{radioButtonBean.job}">
   <f:selectItems value="#{radioButtonBean.preparedJobs}" />
</h:selectOneRadio>

RadioButtonBean.java

	private String job;

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public Map<String, String> getPreparedJobs() {
		Map<String, String> jobsMap = new LinkedHashMap<String, String>();
		jobsMap.put("Engineer", "Software Engineer");
		jobsMap.put("Architect", "Decorator");
		return jobsMap;
	}

Son olarak f:selectItem Object dizisi kullanabiliriz. Oncelikle label ve value karsilik gelecek bir model olusturalim.

RadioButtonModel.java

package _26.radiobutton;

public class RadioButtonModel {

	private String radioLabel;
	private String radioValue;
	// getters and setters
	
        public RadioButtonModel(String radioLabel, String radioValue) {
		super();
		this.radioLabel = radioLabel;
		this.radioValue = radioValue;
	}

radiobutton.xhtml

f:selectItems tag’inda value olarak kullandigimiz attribute geriye RadioButtonModel[] array’i dondurmektedir. itemLabel ve itemValue attribute’leri kullanarak radio button icin label ve value degerlerini ayarlariz.Bunun icin RadioButtonModel sinifinda yer alan property alanlarini kullanabiliriz.

<h2>f:selectItems with Object</h2>

<h:selectOneRadio value="#{radioButtonBean.department}">
    <f:selectItems value="#{radioButtonBean.preparedDepartments}"
    var="dept" itemLabel="#{dept.radioLabel}" itemValue="#{dept.radioValue}" />
</h:selectOneRadio>

RadioButtonBean.java

	private String department;

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public RadioButtonModel[] getPreparedDepartments() {
		RadioButtonModel[] departmentRadioButtonModel = new RadioButtonModel[3];
		departmentRadioButtonModel[0] = new RadioButtonModel("IT", "Software Dept");
		departmentRadioButtonModel[1] = new RadioButtonModel("Director", "Manager");
		departmentRadioButtonModel[2] = new RadioButtonModel("Accountant", "Accountant");
		return departmentRadioButtonModel;
	}

Dosya iceriklerini toplu olarak inceleyecek olursak ;

radiobutton.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:head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>radiobutton components</title>
</h:head>

<h:body>
   <h:form prependId="false" id="formId1">

      <h1>h:selectOneRadio examples</h1>

      <h2>f:selectItem</h2>
      <h:selectOneRadio value="#{radioButtonBean.gender}">
         <f:selectItem itemValue="Man" itemLabel="Mr" />
         <f:selectItem itemValue="Woman" itemLabel="Mrs" />
      </h:selectOneRadio>

      <h2>f:selectItems with array</h2>

      <h:selectOneRadio value="#{radioButtonBean.maritalStatus}">
         <f:selectItems value="#{radioButtonBean.preparedMaritalStatus}" />
      </h:selectOneRadio>


      <h2>f:selectItems with map</h2>

      <h:selectOneRadio value="#{radioButtonBean.job}">
         <f:selectItems value="#{radioButtonBean.preparedJobs}" />
      </h:selectOneRadio>

      <h2>f:selectItems with Object</h2>
      
      <h:selectOneRadio value="#{radioButtonBean.department}">
         <f:selectItems value="#{radioButtonBean.preparedDepartments}"
            var="dept" itemLabel="#{dept.radioLabel}"
            itemValue="#{dept.radioValue}" />
      </h:selectOneRadio>

      <p>
         <h:commandButton value="result.xhtml" action="result" />
      </p>

   </h:form>
</h:body>
</html>

result.xhtml

<!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:c="http://java.sun.com/jstl/core">
<h:head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>results</title>
</h:head>
<h:body>
   
   <h1>h:selectOneRadio examples</h1>
   
   <h2>f:selectItem</h2>
   
   #{radioButtonBean.gender}  
   
   <h2> f:selectItems with array</h2>
   #{radioButtonBean.maritalStatus}
   
   <h2> f:selectItem with map</h2>
   #{radioButtonBean.job}
   
   <h2> f:selectItem with Object</h2>
   #{radioButtonBean.department}
   


</h:body>
</html>

RadioButtonBean.java

package _26.radiobutton;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class RadioButtonBean {

	private String gender;

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	private String maritalStatus;

	public String getMaritalStatus() {
		return maritalStatus;
	}

	public void setMaritalStatus(String maritalStatus) {
		this.maritalStatus = maritalStatus;
	}

	public String[] getPreparedMaritalStatus() {
		return new String[]{"Married", "Engaged", "Single", "Java Man :)"};
	}

	private String job;

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public Map<String, String> getPreparedJobs() {
		Map<String, String> jobsMap = new LinkedHashMap<String, String>();
		jobsMap.put("Engineer", "Software Engineer");
		jobsMap.put("Architect", "Decorator");
		return jobsMap;
	}

	private String department;

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public RadioButtonModel[] getPreparedDepartments() {
		RadioButtonModel[] departmentRadioButtonModel = new RadioButtonModel[3];
		departmentRadioButtonModel[0] = new RadioButtonModel("IT", "Software Dept");
		departmentRadioButtonModel[1] = new RadioButtonModel("Director", "Manager");
		departmentRadioButtonModel[2] = new RadioButtonModel("Accountant", "Accountant");
		return departmentRadioButtonModel;
	}

}

RadioButtonModel.java

package _26.radiobutton;

public class RadioButtonModel {

	private String radioLabel;
	private String radioValue;
	
	public RadioButtonModel(String radioLabel, String radioValue) {
		super();
		this.radioLabel = radioLabel;
		this.radioValue = radioValue;
	}
	public String getRadioLabel() {
		return radioLabel;
	}
	public void setRadioLabel(String radioLabel) {
		this.radioLabel = radioLabel;
	}
	public String getRadioValue() {
		return radioValue;
	}
	public void setRadioValue(String radioValue) {
		this.radioValue = radioValue;
	}
	

}

Ornegimizi calistirdigimizda ;

h:selectOneRadio

Radio buttonlari diledigimiz gibi secip butona tikladigimizda ;

h:selectOneRadio result

Kaynak kodlar : Injavawetrust.jsf
Yazimi burada sonlandiriyorum.
Herkese bol Javali gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *