为什么渲染不适用于面板

时间:2015-04-09 14:04:28

标签: jsf panel rendered-attribute

如何动态显示或隐藏面板?

根据下面给出的示例,当我点击btn-1时,panel-2显示并panel-1隐藏。但是当我点击btn-2时没有任何事情发生。

ManagedBean

@ManagedBean
@ViewScoped
public class SampleController implements Serializable {
    private Boolean showPanel;

    public void button1() {
        showPanel = Boolean.FALSE;
    }

    public void button2() {
        showPanel = Boolean.TRUE;
    }

    public Boolean showPanel1(){
        return showPanel;
    }

    public Boolean showPanel2(){
        return ! showPanel;
    }
}

XHTML

<h:form id="form">
    <h:panelGroup id="container-panel-1">
          <h:panelGroup id="panel-1" rendered="#{sampleController.showPanel1()}">
              <p:commandButton id="btn-1"
                   actionListener="#{sampleController.button1}" 
                   update=":form:container-panel-1, :form:container-panel-2" />
          </h:panelGroup>
    </h:panelGroup>
    <h:panelGroup id="container-panel-2">
          <h:panelGroup id="panel-2" rendered="#{sampleController.showPanel2()}">
              <p:commandButton id="btn-2"
                   actionListener="#{sampleController.button2}" 
                   update=":form:container-panel-1, :form:container-panel-2" />
          </h:panelGroup>
    </h:panelGroup>
</h:form>

我希望你能帮助我。

谢谢你, 钟

1 个答案:

答案 0 :(得分:0)

此代码有效:

查看:

<h:form id="form">

    <h:panelGroup id="container-panel-1">
        <h:panelGroup id="panel-1" rendered="#{sampleController.showPanel1()}">
            <p:commandButton id="btn-1" value="btn1"
               actionListener="#{sampleController.button1}" 
               update=":form:container-panel-1, :form:container-panel-2" />
        </h:panelGroup>
    </h:panelGroup>

    <h:panelGroup id="container-panel-2">
        <h:panelGroup id="panel-2" rendered="#{sampleController.showPanel2()}">
            <p:commandButton id="btn-2" value="btn2"
               actionListener="#{sampleController.button2}" 
               update=":form:container-panel-1, :form:container-panel-2" />
        </h:panelGroup>
    </h:panelGroup>
</h:form>

豆:

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class SampleController implements Serializable {

    private boolean showPanel;

    public void button1() {
        showPanel = false;
    }

    public void button2() {
        showPanel = true;
    }

    public boolean showPanel1(){
        return showPanel;
    }

    public boolean showPanel2(){
        return ! showPanel;
    }
}