JSF 2.0 <h:selectoneradio>和<f:selectitem>如何在会话作用域中传递选定的单选按钮?</f:selectitem> </h:selectoneradio>

时间:2012-04-04 17:12:26

标签: jsf-2

我想在会话中传入选定的单选按钮值。我的代码不起作用。

这是视图(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>

这是托管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"; } ...} ... }

有人可以告诉我这里我做错了什么吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

<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>

我不确定你的bean的语义是什么,或者控制器的目标是什么,但最好使用属性和getter和setter。

@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"; 
} ...} ... }