【问题标题】:Conditional jsf include有条件的 jsf 包括
【发布时间】:2013-03-04 09:09:32
【问题描述】:

如何在运行时有条件地包含 jsf facelets 文件? 所需的示例功能是

if ( add button click) {

ui:include src="Add.xhtml"
}

if ( update button click) {

ui:include src="Update.xhtml"
}

上面的语法只是指示性的......

Mojarra 2.1.1 / Apache Tomcat 7.0.22 / PrimeFaces 3.4

【问题讨论】:

    标签: dynamic jsf-2 user-interface include


    【解决方案1】:

    ui:include 没有rendered 属性,因此您必须将其封装在其他一些组件中。此外,您将在单击按钮时在服务器上设置一些属性。

    <h:form>
      <p:commandButton value="Add" update=":includeContainer">
        <f:setPropertyActionListener value="add" target="#{myBean.action}"/>
      </p:commandButton>
      <p:commandButton value="Update" update=":includeContainer">
        <f:setPropertyActionListener value="update" target="#{myBean.action}"/>
      </p:commandButton>
    </h:form>
    
    <h:panelGroup id="includeContainer">
      <h:panelGroup rendered="#{myBean.action == 'add'}">
        <ui:include src="add.xhtml"/>
      </h:panelGroup>
      <h:panelGroup rendered="#{myBean.action == 'update'}">
        <ui:include src="update.xhtml"/>
      </h:panelGroup>
    </h:panelGroup>
    

    在支持 bean 中,您将拥有 getter 和 setter:

    public void setAction(String action) {
      this.action = action;
    }
    
    public String getAction() {
      return action;
    }
    

    【讨论】:

    • 对此的替代问题是,如果它是条件包含,那么为什么在调用主文件时会处理 xhtml...我通过在 add.xhtml 中包含 f:event prerenderview 标记来检查这一点例如文件...我通过侦听器方法打印system.out ...是的,视图范围的bean没有被实例化,但是xhtml 被解析并触发 prerenderview 事件......为什么它是条件包含时会这样?
    • 这是因为ui:include在构建视图阶段执行,渲染属性是在渲染视图阶段稍后计算的。这就是 JSF 中的工作方式。如果这是一个问题,您必须考虑其他解决方案。考虑this answer 寻求解决方案。
    • @ᴠɪɴᴄᴇɴᴛ 已修复。谢谢。
    【解决方案2】:

    我重新发布 partlov 的答案,因为有一些错误,我纠正了这个错误并放入了这个 pos,并祝贺 partlov 得到了很好的答案

    我补充你的答案 首先这是一个带有素数面孔的 xhtml 页面,如果你想使用 p: 添加这个框架或其他。

    如果你不下载这个框架或者在这里下载Download prime Framework for JSF

    并以此方式导入

    xmlns:p="http://primefaces.org/ui"
    

    select.XHTML

    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
      <h:form>
        <p:commandButton value="Add" update="panel">
          <f:setPropertyActionListener value="add" target="#{myBean.action}"/>
        </p:commandButton>
        <p:commandButton value="Update" update="panel">
          <f:setPropertyActionListener value="update" target="#{myBean.action}"/>
        </p:commandButton>
    
    
        <h:panelGroup id="panel">
          <h:panelGroup rendered="#{myBean.action == 'add'}">
            <ui:include src="headerLogin.xhtml"/>
          </h:panelGroup>
          <h:panelGroup rendered="#{myBean.action == 'update'}">
            <ui:include src="Pajax.xhtml"/>
          </h:panelGroup>
        </h:panelGroup>
      </h:form>
    </h:body>
    

    下一步是创建一个 clas myBean 并使用这个字符串来选择你想要渲染的 UI myBean.java

    在 bean 中使用这个导入

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    

    在一个类中使用这个鳕鱼

    public class myBean {
    String action;
    public void setAction(String action) {
    
        this.action = action;
    }
    
    public String getAction() {
      return action;
    }
    

    }

    但记得把这行放在一个类中

    @ManagedBean
    @SessionScoped
    

    【讨论】:

    • 这个例子有效吗?我复制粘贴了代码并尝试了,但它没有按预期工作。我需要刷新页面才能继续下一个选项。任何帮助表示赞赏。
    猜你喜欢
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多