JSF 在“ui:include src="#{bean.pagePath}"中动态包含 src

人气:633 发布:2022-10-16 标签: jsf tabview primefaces uiinclude

问题描述

我尝试在不同的选项卡中使用 ui:include 标签包含多个源页面路径.问题是,当我将源页面路径设为静态时,意味着该页面将被显示,但如果从支持 bean 中提供源页面路径,则意味着它不会包含该页面.

I tried to include multiple source page path using ui:include tag in different tabs. The problem is when i gave the source page path as static one means that page will be shown but if give the source page path from backing bean mean it won't include the page.

这是我的代码

template.xhtml:

    <p:layoutUnit position="center" id="layoutCenter">
                <h:form id="tempFormId">
                    <p:tabView value="#{multiTabBean.tabsList}" var="useCase"
                        activeIndex="#{multiTabBean.activeTabIndex}">
                        <p:tab title="#{useCase.title}" closable="true">
                            <f:subview>
                                <h:panelGroup id="mainTempPanelGroupId" layout="block">
                                    <ui:include src="#{useCase.path}" />
                                </h:panelGroup>

                            </f:subview>
                        </p:tab>
                    </p:tabView>
                </h:form>
            </p:layoutUnit>

豆:

public String menuAction() {                    
    menuBtnRendered = true;
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    selectedModuleViewId = externalContext.getRequestParameterMap().get(
            "moduleViewId");

    tabsList.add(new Tab(getTabId(selectedModuleViewId),
                selectedModuleViewId, getModulePath(selectedModuleViewId)));        

    return null;
}

我正在使用 @ViewScoped.

推荐答案

在视图构建期间运行(当 XHTML 变成 JSF 组件树时).<p:tabView> 在视图渲染时运行(当 JSF 组件树需要生成 HTML 时).

<ui:include> runs during view build time (when XHTML is turned into JSF component tree). <p:tabView> runs during view render time (when JSF component tree needs to produce HTML).

因此, 运行时不可用.这个问题在这个答案中有详细说明:JSF2 Facelets中的JSTL ...有意义吗?( 是一个标记处理程序,因此与 JSTL 标记具有相同的生命周期).

So, <p:tabView var> isn't available when <ui:include> runs. This problem is detailed in this answer: JSTL in JSF2 Facelets... makes sense? (<ui:include> is a taghandler and has hence same lifecycle as JSTL tags).

您可以通过使用 来生成 s 而不是

来在一定程度上解决这个问题:tabView 值>.

You can solve this to a certain degree by using <c:forEach> to produce <p:tab>s instead of <p:tabView value>.

<p:tabView activeIndex="#{multiTabBean.activeTabIndex}">
    <c:forEach items="#{multiTabBean.tabsList}" var="useCase" varStatus="loop">
        <p:tab title="#{useCase.title}" closable="true">
            <f:subview id="tab_#{loop.index}">
                <h:panelGroup id="mainTempPanelGroupId" layout="block">
                    <ui:include src="#{useCase.path}" />
                </h:panelGroup>
            </f:subview>
        </p:tab>
    </c:forEach>
</p:tabView>

顺便说一句,有趣的是您在最初的尝试中以某种方式使用了 <f:subview> 这在那里完全没用(<p 已经考虑到了:tabView var>),但它实际上在这里很有用(它在选项卡内容周围创建了一个新的 NamingContainer).

It's by the way interesting to see that you somehow used <f:subview> in your initial attempt which is completely useless over there (already taken into account by <p:tabView var>), but it's actually helpful in here (it creates a new NamingContainer around the tab content).

328