2

我有一个 JSF 2 主模板,如下所示:

<!DOCTYPE html>
<html lang="#{localeManager.language}" 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">

    <f:view locale="#{localeManager.locale}">
        <h:head>
            <meta charset="UTF-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <title><ui:insert name="title">Default Title.</ui:insert></title>

            <!-- Development Stylesheets -->
            <ui:fragment rendered="#{facesContext.application.projectStage eq 'Development'}">
                <h:outputStylesheet name="css/bootstrap.css" library="bootstrap" />
                <h:outputStylesheet name="css/font-awesome.css" library="fontawesome" />
            </ui:fragment>

            <h:outputStylesheet name="css/main.css" library="core" />
            <ui:insert name="header-stylesheet" />

            <!-- Production Stylesheets -->
            <ui:fragment rendered="#{facesContext.application.projectStage eq 'Production'}">
                <h:outputStylesheet name="css/bootstrap.min.css" library="bootstrap" />
                <h:outputStylesheet name="css/font-awesome.min.css" library="fontawesome" />
            </ui:fragment>

            <ui:insert name="header-script" />
        </h:head>

        <h:body>
            <div id="wrapper">
                <div id="header">
                    <ui:insert name="header">Default content</ui:insert>
                </div>

                <div id="body">
                    <ui:insert name="body">Default body</ui:insert>
                </div>

                <div id="footer">
                    <ui:insert name="footer">Default footer</ui:insert>
                </div>
            </div>

            <!-- Development Scripts -->
            <ui:fragment rendered="#{facesContext.application.projectStage eq 'Development'}">
                <h:outputScript name="jquery-2.1.4.js" library="jquery" />
                <h:outputScript name="js/bootstrap.js" library="bootstrap" />
            </ui:fragment>

            <!-- Production Scripts -->
            <ui:fragment rendered="#{facesContext.application.projectStage eq 'Production'}">
                <h:outputScript name="jquery-2.1.4.min.js" library="jquery" />
                <h:outputScript name="js/bootstrap.min.js" library="bootstrap" />
            </ui:fragment>
            <ui:insert name="body-script" />
        </h:body>
    </f:view>
</html>

在 Wildfly 9.0.1 Final 中部署它时,我看到我的所有<ui:fragment>属性都被渲染了。这是为什么?我正在使用 JSF 2.2 框架进行开发。

我的面孔项目阶段是Development.

注意:在关于这个问题的所有答案中,没有一个是这个问题的解决方案(所以我做了功课)。

4

1 回答 1

2

和是<h:outputStylesheet>特殊<h:outputScript>组件。他们将在视图构建期间已经将声明的样式表或脚本资源添加到视图中UIViewRoot#addComponentResource()请参阅此处的 Mojarra 源代码)。

rendered这与组件或其父项的条件无关。实际上,它们基本上被重新定位到<h:head>或 的最后<h:body>,这取决于target属性的值,导致它们不再位于内部<ui:fragment>

然后,在渲染过程中,只考虑它们自己的rendered属性(实际上也是 of <h:head>,但这没有意义)。

您有 2 个选项:

  1. 使用视图构建时间标记有条件地将它们添加到视图中。项目阶段条件无论如何都是应用范围。

    <!-- Development Stylesheets -->
    <c:if test="#{facesContext.application.projectStage eq 'Development'}">
        <h:outputStylesheet ... />
        <h:outputStylesheet ... />
    </c:if>
    
    <!-- Production Stylesheets -->
    <c:if test="#{facesContext.application.projectStage eq 'Production'}">
        <h:outputStylesheet ... />
        <h:outputStylesheet ... />
    </c:if>
    
  2. 检查组件资源自身rendered属性中的条件。如有必要<c:set>,可用于创建短 EL 变量。

    <c:set var="dev" value="#{facesContext.application.projectStage eq 'Development'}" scope="application" />
    
    <!-- Development Stylesheets -->
    <h:outputStylesheet ... rendered="#{dev}" />
    <h:outputStylesheet ... rendered="#{dev}" />
    
    <!-- Production Stylesheets -->
    <h:outputStylesheet ... rendered="#{not dev}" />
    <h:outputStylesheet ... rendered="#{not dev}" />
    
于 2015-08-27T07:33:01.840 回答