File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get selected RadioButton value into beans

 
shree lk
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic