【问题标题】:Rendering elements in MyFaces 1.1.1MyFaces 1.1.1 中的渲染元素
【发布时间】:2016-05-31 17:49:28
【问题描述】:

我正在尝试创建一个简单的 jsf 页面,其中有一个下拉列表,其值决定了要呈现的标签。最初,所有标签的渲染都通过支持 bean 构造函数设置为 false。但是我调用了 submit onchange ,它将标签的相应值设置为 true。我已将支持 bean 的范围设置为 session,以便设置的值不会在更改时被删除。但是,标签不会在更改时呈现。下面是jsf页面的sn-p代码:

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

这是支持 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);
    }
}


}

【问题讨论】:

    标签: jsf valuechangelistener rendered-attribute conditional-rendering jsf-1.1


    【解决方案1】:

    我不明白你为什么使用如此复杂的代码来完成简单的任务。 这就是你需要的

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

    而 Bean 会是

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

    【讨论】:

    • OP 正在使用侏罗纪 JSF 1.x。 和 @ViewScoped 在 JSF 1.x 中不存在。所以你的答案根本不会在 JSF 1.x 中运行。对于其余部分,我同意那些额外的布尔属性不必要地过于复杂。
    【解决方案2】:

    我发现我的代码出了什么问题。而不是将标签放在&lt;H3&gt;tags 中。我需要把它放在&lt;h:outputText&gt; 标签中。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2022-11-01
      • 2011-12-18
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2018-02-25
      相关资源
      最近更新 更多