JSF 2.0 and How can I pass the selected radio button in the session scoped?

Go To StackoverFlow.com

0

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.

2012-04-04 17:12
by Lukem


1

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"; 
} ...} ... }
2012-04-04 19:04
by Oscar Castiblanco
Thank you ojota for your answer. Your suggestion worked. Now I want to use JavaScript to activate and deactivate the corresponding inputText. For example, if the radio button FachID is selected, the inputText FachID is activated, if the radio button FachID is not selected, the inputText FachID is disabled. Can you please tell me how does it work - Lukem 2012-04-10 12:49
you don't need javascript.Inputtext has a property called rendered, so you just do rendered = "#{controller.VNGWSelected}" and in the button you have to add an f:ajax that updates the input tex - Oscar Castiblanco 2012-04-10 13:11
Ok. Can you please send me an example? Thanks in advance - Lukem 2012-04-10 15:15
Can you create a new thread with your questio - Oscar Castiblanco 2012-04-10 15:46
Ads