我需要帮助来渲染一个组件,该组件在渲染之后将进行ajax调用,但没有成功。 我有以下代码,但不起作用。我不知道我是否遗漏了什么。任何帮助都非常感谢。
<h:selectOneMenu id="gender" value="#{bean.gender}"
class="form-control"
valueChangeListener="#{bean.updateGenderValue}"
onchange="submit()">
<f:selectItems value="#{bean.genders}" var="gender" itemLabel="#
{gender}" itemValue="#{gender}"/>
</h:selectOneMenu>
<h:selectOneMenu value="#{bean.pregnancyStatus}"
class="form-control"
rendered="#{bean.gender eq 'Female'}">
<f:selectItems value="#{bean.options}"
var="pregnancyStatus" itemLabel="#{pregnancyStatus}"
itemValue="#{pregnancyStatus}"/>
<f:ajax listener="#{bean.updatePregancyValue}"
execute="@this" render="@this"/>
</h:selectOneMenu>
public void updateGenderValue(ValueChangeEvent event) throws IOException {
gender = (String) event.getNewValue();
}
public void updatePregancyValue(AjaxBehaviorEvent event) throws IOException {
System.out.println(":( == " + pregnancyStatus);
}
妊娠状态值永远不会更新。
答案 0 :(得分:1)
你错过了<h:form>
吗?我将代码复制并粘贴到我的项目中
我的代码:
<div style="height:500px">
<h:form>
<h:selectOneMenu id="gender" value="#{bean.gender}"
class="form-control"
valueChangeListener="#{bean.updateGenderValue}"
onchange="submit()">
<f:selectItems value="#{bean.genders}" var="gender" itemLabel="#
{gender}" itemValue="#{gender}"/>
</h:selectOneMenu>
<h:selectOneMenu value="#{bean.pregnancyStatus}"
class="form-control"
rendered="#{bean.gender eq 'Female'}">
<f:selectItems value="#{bean.options}"
var="pregnancyStatus" itemLabel="#{pregnancyStatus}"
itemValue="#{pregnancyStatus}"/>
<f:ajax listener="#{bean.updatePregancyValue}"
execute="@this" render="@this"/>
</h:selectOneMenu>
让我们检查控制台:
:( == 2
:( == 3
豆:
@ManagedBean(name = "bean")
@ViewScoped
public class Bean {
private String pregnancyStatus;
private List<SelectItem> options;
private List<SelectItem> genders;
private String gender;
@PostConstruct
public void initBean(){
options = new ArrayList<>();
genders = new ArrayList<>();
options.add(new SelectItem("1"));
options.add(new SelectItem("2"));
options.add(new SelectItem("3"));
genders.add(new SelectItem("Male"));
genders.add(new SelectItem("Female"));
genders.add(new SelectItem("third"));
}
public String getPregnancyStatus() {
return pregnancyStatus;
}
public void setPregnancyStatus(String pregnancyStatus) {
this.pregnancyStatus = pregnancyStatus;
}
public void updateGenderValue(ValueChangeEvent event) throws IOException {
gender = (String) event.getNewValue();
}
public void updatePregancyValue(AjaxBehaviorEvent event) throws IOException {
System.out.println(":( == " + pregnancyStatus);
}
}