我想在会话中传入选定的单选按钮值。我的代码不起作用。
这是视图(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"; } ...} ... }
有人可以告诉我这里我做错了什么吗?
提前致谢。
答案 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";
} ...} ... }