aspose file tools*
The moose likes JSF and the fly likes How to get selected RadioButton value into beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to get selected RadioButton value into beans" Watch "How to get selected RadioButton value into beans" New topic
Author

How to get selected RadioButton value into beans

shree lk
Greenhorn

Joined: Mar 16, 2011
Posts: 14

Hi all...

I have added 3 radio buttons on my JSF from. I want to store value of selected radio button into bean so that I can display it on the JSF from. Can anybody please suggest how to get it??


Also with calender, When i display the selected date on the JSF from it shows previous day, not the selected day. But when i display in console it shows the selected day without problem..




Following is code I used in the JSF.

<h:form id="calendarForm">

<style type="text/css">

.yearRowStyle {
background-color: #A8D1E8;
color: red;
text-align: center;
font-weight: bold;
font-style:italic;
}
.weekRowStyle {
background-color: #D6EBFC;
}
.selectedDayCellStyle {
background-color: #ECD5D2;
}

</style>

<h3> <hutputText value="Select Date" style="color:blue"/> </h3>

<t:inputCalendar id="MyCalendar" monthYearRowClass="yearRowStyle"
weekRowClass="weekRowStyle"
currentDayCellClass="selectedDayCellStyle"
value="#{calendarBean.selectedDate}" />

<br><br>


<t:selectOneRadio id="SR" value="SI"
layout="spread"
border="1"
style="font-weight:bold" binding="#{calendarBean.radioSelect}" >

<f:selectItem id="sel1" itemLabel="Public Holiday" itemValue="ph" />
<f:selectItem id="sel2" itemLabel="General Holiday" itemValue="gh" />
<f:selectItem id="sel3" itemLabel="Festival Holiday" itemValue="fh" />

</t:selectOneRadio>

<h3> <hutputText value="Select Holiday Type" style="color:blue"/> </h3>

<t:panelGrid columns="1" width="20%" style="color:red;"
cellpadding="0" cellspacing="5">

<t:panelGroup>
<t:radio for="SR" index="0"></t:radio>
</t:panelGroup>

<t:panelGroup>
<t:radio for="SR" index="1"></t:radio>
</t:panelGroup>

<t:panelGroup>
<t:radio for="SR" index="2"></t:radio>
</t:panelGroup>

</t:panelGrid>

<br><br>

<h:commandButton id="dateBtn" value="Submit" action="#{calendarBean.onDate}" />

</h:form>



AND my bean java file is..

package com.calendar.web;

import java.util.Date;

public class Calendar
{

Date selectedDate;
String radioSelect;


public Date getSelectedDate() {
return selectedDate;
}


public void setSelectedDate(Date selectedDate) {
this.selectedDate = selectedDate;
}


public String getRadioSelect() {
return radioSelect;
}


public void setRadioSelect(String radioSelect) {
this.radioSelect = radioSelect;
}




public String onDate()
{

String a=selectedDate.toString();

System.out.println(a);
System.out.println(radioSelect);



return "dateSelect";

}
}
Navanethan Muthusamy
Greenhorn

Joined: Apr 06, 2009
Posts: 6

Hi,

From your code what I understood is there is no like between JSP and bean in selectOneRadio tag.

Please try the following sample code:

JSP code:
----------

<h:selectOneRadio value="#{helloWorldBacking.test}">
<f:selectItem itemLabel="Test1" itemValue="Test1" />
<f:selectItem itemLabel="Test2" itemValue="Test2" />
</h:selectOneRadio>

Bean code:
--------------

String test;

and setter getter methods for string test.

examples-config.xml
------------------------
<managed-bean>
<managed-bean-name>helloWorldBacking</managed-bean-name>
<managed-bean-class>org.apache.myfaces.blank.HelloWorldBacking</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>



The above code work well. when I have selected first radio button i got 'Test1' in bean.


Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

There's a "Code" button on the Forum editor. You can use that to mark code and other pre-formatted content such as XML and make it easier to read.

You don't have to do anything complicated to get the select radiobutton value into your bean. In fact, you're already too complicated. Get rid of the "binding" on the radioButton. People use that attribute too freely (I think there's some obsolete documentation that makes them think they need to do so when they don't). Plus you didn't actually do the binding right anyway.

Just code 'value="#{calendarBean.radioSelect}"' on your radiobutton control instead of "binding=" and the "setRadioSelect" method will be invoked and passed the value of the selected button when you fire the Action Method (in other words, when you submit the form).


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get selected RadioButton value into beans