我有一个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>当将它部署到Wildflix9.0.1Final中时,我看到我的所有<ui:fragment>属性都会被呈现出来。为什么会这样呢?我正在使用JSF2.2框架进行开发。
我的脸项目阶段是Development。
注:在所有关于这个问题的答案中,没有一个是这个问题的解决方案(所以我做了我的作业)。
发布于 2015-08-27 07:33:01
<h:outputStylesheet>和<h:outputScript>是特殊的组件。他们将在视图构建时通过UIViewRoot#addComponentResource() (参见此处的Mojarra源代码)将声明的样式表或脚本资源添加到视图中。
这与组件或其父级的rendered条件无关。实际上,它们基本上被重新定位到<h:head>或<h:body>的末尾,这取决于target属性的值,从而使它们不再停留在<ui:fragment>中。
然后,在呈现过程中,只考虑它们自己的rendered属性(实际上也考虑<h:head>属性,但这是没有意义的)。
你有两个选择:
rendered属性中的条件。如果必要的话使用<c:set>来创建一个短的EL变量。
https://stackoverflow.com/questions/32226974
复制相似问题