我有一个 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
.
注意:在SO中关于这个问题的所有答案中,没有一个是这个问题的解决方案(所以我做了我的作业)。
最佳答案
<h:outputStylesheet>
和<h:outputScript>
是特殊组件。他们将在 View 构建期间通过 UIViewRoot#addComponentResource()
将声明的样式表或脚本资源添加到 View 中。 (see Mojarra source code here)。
这与rendered
无关组件或其父组件的状况。实际上,它们基本上被重新定位到 <h:head>
的最末尾。或<h:body>
,取决于 target
的值属性,导致它们不坐在 <ui:fragment>
内不再了。
然后,在渲染过程中,只有自己的rendered
属性被考虑(实际上也是 <h:head>
,但这毫无意义)。
您有 2 个选择:
使用 View 构建时间标记有条件地将它们添加到 View 中。无论如何,项目阶段条件是应用范围的。
<!-- 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>
检查组件资源自身的情况
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}" />
另请参阅:
关于jsf - 为什么<ui :fragment rendered> not evaluated on <h:outputStylesheet> and <h:outputScript>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226974/