根据条件显示JSF值

时间:2016-08-15 15:38:14

标签: jsf-2 primefaces

我有<p:accordionPanel value="#{bean.list}" var="entry" dynamic="true" cache="true"> <p:tab title="#{entry.alias}"> <h:panelGrid columns="2" cellpadding="10"> <h:outputText value="#{entry.text}" /> </h:panelGrid> </p:tab> </p:accordionPanel> 如下:

public void init(){
    acceuil = 1;
    salles = salleService.listSalles();
    appareils = appareilService.list();
}

在我的Managed Bean中,我初始化了两个列表:

acceuil

我正在使用1变量,所以当我将其设置为salles时,我会在我的accordionPanel中使用2列表另一方面,如果我将其设置为appareils,我将使用accordionPanel中的alias列表(请注意,服饰和salle对象具有相同的属性textvalue="#{bean.acceuil == 1 ? bean.salles : bean.appareils}" 但他们没有共同的课程,他们从那里延伸。

所以在我的jsf页面中我想做这样的事情:

{{1}}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

  

所以在我的jsf页面中我想做这样的事情:

<h:form>
    <h:outputText value="foobar is: #{myBean.foobar}" />
    <p:accordionPanel value="#{myBean.foobar == 1 ? myBean.fooList : myBean.barList}" 
                      var="entry" dynamic="true" cache="true">
        <p:tab title="#{entry.foo}">
            <h:panelGrid columns="2" cellpadding="10">
                <h:outputText value="#{entry.bar}" />
            </h:panelGrid>
        </p:tab>
    </p:accordionPanel>
    <p:commandButton actionListener="#{myBean.toggle()}" update="@form"/>
</h:form>
     

我该怎么做?

一种方式就是你提到的。这种方法没有错。如果这对你不起作用,那么其他一些东西必须出错。您甚至可以在“运行时”在两个列表之间切换。请参阅以下示例:

JSF表格:

@Named
@ViewScoped
public class MyBean implements Serializable {

    @Getter private List<Foo> fooList = new ArrayList<>();
    @Getter private List<Bar> barList = new ArrayList<>();
    @Getter private int foobar;

    @PostConstruct
    private void init() {
        foobar = 1;
        fooList.add(new Foo());
        fooList.add(new Foo());
        fooList.add(new Foo());
        barList.add(new Bar());
        barList.add(new Bar());
    }

    public void toggle() {
        if(foobar == 1) {
            foobar = 2;
        } else {
            foobar =1;
        }
    }
}

支持Bean:

@Data
public class Foo {
    String foo = "Foo: Foo";
    String bar = "Foo: Bar";
}

@Data
public class Bar {
    String foo = "Bar: Bar";
    String bar = "Bar: FOO";
}

的POJO:

{{1}}

结果:

enter image description here

点击按钮后:

enter image description here

但是,我更喜欢@XtremeBiker在其他答案中建议的解决方案。您应该将演示文稿中的逻辑降至最低。但是如果你不允许或者能够在这里编写适当的代码,那么这些小事情可能会好。