【问题标题】:How to use the IncludeHandler inside my custom component?如何在我的自定义组件中使用 IncludeHandler?
【发布时间】:2023-03-04 00:10:01
【问题描述】:

有问题: The class behind ui:include JSF tag 发现需要使用IncludeHandler来使用

<ui:include>

以编程方式。 但是,构造函数需要一个“配置”参数,我不知道如何设置它。
请举例说明如何使用 IncludeHandler 进行简单的包含,例如

<ui:include src="include.xhtml" />

我的 jsf 组件目前是以编程方式构建的,但我想包含一些写为“.xhtml”的部分。所以最后一个网页设计师只需要一个像

这样的组件
<fg:generator></fg:generator>

和一些“.xhtml”文件来玩弄样式。如果有比 IncludeHandler 更好的方法(仍然需要使用 Java),请告诉我:)

【问题讨论】:

    标签: java jsf-2 include facelets custom-component


    【解决方案1】:

    如果您的唯一目的是以编程方式使用&lt;ui:include&gt;,那么您应该改用FaceletContext#includeFacelet()。假设您在自定义组件中:

    FacesContext facesContext = FacesContext.getCurrentInstance();
    FaceletContext faceletContext = (FaceletContext) facesContext.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    faceletContext.includeFacelet(this, "include.xhtml"); // this is your current UIComponent.
    

    这是另一个演示通过命令按钮动态包含的启动示例:

    <h:form>
        <h:commandButton value="include" action="#{bean.include}" />
    </h:form>
    <h:panelGroup id="include" />
    

    与

    public void include() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FaceletContext faceletContext = (FaceletContext) facesContext.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
        faceletContext.includeFacelet(facesContext.getViewRoot().findComponent("foo"), "include.xhtml");
    }
    

    【讨论】:

    • 好吧,如果可以的话,我会为此投票 50 次。第一次尝试工作。非常感谢!
    • 这帮助了我...谢谢!对于那些感兴趣的人:要添加一些
    猜你喜欢
    • 2019-09-04
    • 2018-01-15
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多