• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get selected RadioButton value into beans

 
Greenhorn
Posts: 14
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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> <h:outputText 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> <h:outputText 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";

}
}
 
Greenhorn
Posts: 11
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic