I want to pass in the session scoped the selected radio button value. My code will not work.
Here is the view (searchPerson.xhtml):
<h:form>
<h:selectOneRadio id="searchRadio" value="#{controller.radioButtonFlag}" valueChangeListener="#{controller.selectRadioButton}" >
<f:selectItem itemValue="fachid" itemLabel="#{msg.fachId}">
<f:attribute name="radioButtonField" value="fachid" />
</f:selectItem>
<f:selectItem itemValue="vngw" itemLabel="#{msg.VNGW}">
<f:attribute name="radioButtonField" value="vngw" />
</f:selectItem>
</h:selectOneRadio>
... <h:commandButton action="#{controller.search}" value="#{msg.search}"></h:commandButton>
Here is the managed bean:
@ManagedBean @SessionScoped public class Controller { private String radioButtonField = "fachid"; private boolean VNGWSelected = false; public void selectRadioButton(ValueChangeEvent event){
String select = (String) event.getComponent().getAttributes().get("radioButtonField");
if(select.equals("vngw"))
VNGWSelected = true;
} public String search() {... if(!VNGWSelected){logger.info("FachID RadioButton selected."); else{... logger.info("VNGW RadioButton selected."); ...} return "personsearch"; } ...} ... }
Can someone please tell me what am I doing wrong here?
Thanks in advance.
Try this:
<h:selectOneRadio id="searchRadio" value="#{controller.select}" >
<f:selectItem itemValue="fachid" itemLabel="#{msg.fachId}" />
<f:selectItem itemValue="vngw" itemLabel="#{msg.VNGW}" />
</h:selectOneRadio>
<h:commandButton action="#{controller.search}" value="#{msg.search}"></h:commandButton>
I am not sure of what are the semantics of your bean or what is the goal of the controller but it is better to use an attribute and getters and setters.
@ManagedBean
@SessionScoped
public class Controller {
private String radioButtonField = "fachid";
private boolean VNGWSelected = false;
private String select;
public void setSelect(String select){
this.select = select;
if(select.equals("vngw"))
VNGWSelected = true;
}
public String getSelect(){
return select
}
public String search() {...
if(!VNGWSelected){
logger.info("FachID RadioButton selected.");
else{...
logger.info("VNGW RadioButton selected."); ...}
return "personsearch";
} ...} ... }