简体   繁体   中英

Rendering elements in MyFaces 1.1.1

I am trying to create a simple jsf page where I have a dropdown whose value determines which label to render. Initially all the labels' render is set as false through the backing bean constructor. But I have called submit onchange which sets the respective values to true for the labels. I have set the scope of the backing bean as session so that the value being set does not get removed onchange. However the label does not get rendered onchange. Below is the code snippet for the jsf page:

<h:form>

    <h:panelGroup>
    <h:outputLabel styleClass="captionOutputField" value="Select Report Type:" />
<h:selectOneMenu id="selectedMenu" onchange="submit()" valueChangeListener="#{ReportHealth.typeSelectDropDownChange}">
        <f:selectItem itemLabel="" itemValue="empty" />
        <f:selectItem itemLabel="daily" itemValue="daily" />
        <f:selectItem itemLabel="weekly" itemValue="weekly" />
        <f:selectItem itemLabel="monthly" itemValue="monthly" />
</h:selectOneMenu>
<h:panelGroup rendered="#{ReportHealth.daily}">
    <h3>MENU 0</h3>
</h:panelGroup>
<h:panelGroup rendered="#{ReportHealth.weekly}">
    <h3>MENU 1</h3>
</h:panelGroup>
    <h:panelGroup rendered="#{ReportHealth.monthly}">
    <h3>MENU 2</h3>
</h:panelGroup>

Here is the backing bean:

public class ReportHealth implements Serializable{

private static final long serialVersionUID = 1L;

private boolean weekly;
private boolean monthly;
private boolean daily;
private String menuValue;



public ReportHealth() {
    weekly = false;
    monthly = false;
    daily = false;
}

public String getMenuValue() {
    return menuValue;
}

public void setMenuValue(String menuValue) {
    this.menuValue = menuValue;
}

public boolean isWeekly() {
    return weekly;
}

public void setWeekly(boolean weekly) {
    this.weekly = weekly;
}

public boolean isMonthly() {
    return monthly;
}

public void setMonthly(boolean monthly) {
    this.monthly = monthly;
}

public boolean isDaily() {
    return daily;
}

public void setDaily(boolean daily) {
    this.daily = daily;
}

public void typeSelectDropDownChange(ValueChangeEvent e)
{
    String typeSelectVal = e.getNewValue().toString();
    if(typeSelectVal!=null && typeSelectVal.equalsIgnoreCase("daily"))
    {
        setDaily(true);
        setWeekly(false);
        setMonthly(false);
    }
    else if(typeSelectVal!=null && typeSelectVal.equalsIgnoreCase("weekly"))
    {
        setDaily(false);
        setWeekly(true);
        setMonthly(false);
    }
    else if(typeSelectVal!=null && typeSelectVal.equalsIgnoreCase("monthly"))
    {
        setDaily(false);
        setWeekly(false);
        setMonthly(true);
    }
    else
    {
        setDaily(false);
        setWeekly(false);
        setMonthly(false);
    }
}


}

I dont understand why you are using so complicated code for simple task. Here is what you need

<h:form>
 <h:panelGroup>
 <h:outputLabel styleClass="captionOutputField" value="Select Report Type:"/>
<h:selectOneMenu id="selectedMenu" value="#{reportHealth.menuValue}">
    <f:selectItem itemLabel="" itemValue="empty" />
    <f:selectItem itemLabel="daily" itemValue="daily" />
    <f:selectItem itemLabel="weekly" itemValue="weekly" />
    <f:selectItem itemLabel="monthly" itemValue="monthly" />
    <f:ajax render="@form">
                    </f:ajax>
 </h:selectOneMenu>
 <h:panelGroup rendered="#{reportHealth.menuValue eq 'daily'}">
  <h3>MENU 0</h3>
 </h:panelGroup>
 <h:panelGroup rendered="#{reportHealth.menuValue eq 'weekly'}">
  <h3>MENU 1</h3>
 </h:panelGroup>
 <h:panelGroup rendered="#{reportHealth.menuValue eq 'monthly'}">
  <h3>MENU 2</h3>
 </h:panelGroup>
 </h:panelGroup>
</h:form>

and Bean will be

@ManagedBean
@ViewScoped
public class ReportHealth implements Serializable{

 private static final long serialVersionUID = 1L;

 private String menuValue;

 public String getMenuValue() {
    return menuValue;
 }

 public void setMenuValue(String menuValue) {
    this.menuValue = menuValue;
 }

}

I found out what was wrong with my code. Instead of putting the labels in <H3> tags. I needed to put it in <h:outputText> tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM