在我非常简单的jsf学习游乐场中,我试图根据用户事先输入的参数显示某些内容。
我设法确定了在性别选择期间发生第一个ajax调用时,由于某种原因,类中的sex
的值不会持续存在,因此当条件渲染发生在单击命令按钮,性别设置为null,因此什么也没发生。
<h:body>
<h:form id="surveyForm">
<h:panelGrid columns="2">
Sex:
<h:selectOneRadio id="sexSelect" value="#{survey.sex}" required="true">
<f:selectItem itemValue="m" itemLabel="Male"/>
<f:selectItem itemValue="f" itemLabel="Female"/>
<f:ajax event="change" render="q1"/>
</h:selectOneRadio>
<h:panelGroup id="q1">
<h:panelGrid columns="2">
<h:outputText value="Bust:" rendered="#{survey.sex.equals('f')}"/>
<h:inputText id="bustInputText" value="#{survey.bust}"
rendered="#{survey.sex.equals('f')}"/>
<h:outputText value="Cup size:" rendered="#{survey.sex.equals('f')}"/>
<h:outputText value="Waist:" rendered="#{survey.sex.equals('m')}"/>
<h:inputText id="waistMaleInputText" value="#{survey.waistM}"
rendered="#{survey.sex.equals('m')}"/>
</h:panelGrid>
</h:panelGroup>
<h:commandButton id="submitButton" value="Send">
<f:ajax event="click" render="q2" listener="#{survey.click()}"/>
</h:commandButton>
<h:panelGroup id="q2">
<h:panelGrid rendered="#{survey.valid}" columns="1">
</h:selectManyCheckbox>
<h:outputText value="Which kind of clothes do you prefer?"/>
<h:selectManyCheckbox value="clothesM" rendered="#{survey.sex.equals('m')}">
<f:selectItem itemValue="trousers" itemLabel="trousers" />
<f:selectItem itemValue="shorts" itemLabel="shorts" />
</h:selectManyCheckbox>
<h:selectManyCheckbox value="clothesF" rendered="#{survey.sex.equals('f')}">
<f:selectItem itemValue="jackets" itemLabel="jackets" />
<f:selectItem itemValue="dresses" itemLabel="dresses" />
</h:selectManyCheckbox>
</h:selectManyCheckbox>
</h:panelGrid>
</h:panelGroup>
</h:panelGrid>
</h:form>
</h:body>
</html>
@ManagedBean(name = "survey")
@RequestScoped
public class SurveyBean {
String sex;
Boolean valid = false;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Boolean getValid() {
return valid;
}
public void setValid(Boolean valid) {
this.valid = valid;
}
public void click(){
setValid(true);
}
}