我无法访问文件 css。 我的目录结构:
/resources/style/style.css
/resources/faces/template/baseLayout.xhtml
/WEB-INF/web.xml
/WEB-INF/faces-config.xml
我尝试了两种方法从模板页面 baseLayout.xhtml 获取访问权限:
1)
<link
href="#{facesContext.externalContext.requestContextPath}/resources/style/style.css"
rel="stylesheet" type="text/css" />
2) <h:outputStylesheet library="style" name="style.css" />
但这两种变体都不起作用。 Web.xml 中的 Servlet 映射
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
baseLayout.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<h:outputStylesheet library="style" name="style.css"></h:outputStylesheet>
</h:head>
<body id="newsManagement">
<ui:composition>
<div id="allContent">
<div id="header">
<ui:insert name="header">
<ui:include src="header.xhtml"></ui:include>
</ui:insert>
</div>
<div id="menu">
<ui:insert name="menu">
<ui:include src="menu.xhtml"></ui:include>
</ui:insert>
</div>
<div id="body">
<span id="newsLoc">
<span id="newsLocWord">
<h:outputText value="#{msg['body.news']}"> </h:outputText>
</span>
<h:outputText value="#{msg['body.signs']}"> </h:outputText>
</span>
<ui:insert name="body" />
</div>
</div>
</ui:composition>
</body>
</html>
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<head>
</head>
<body>
<ui:composition template="/resources/faces/template/baseLayout.xhtml"> </ui:composition>
</body>
</html>
有人能帮帮我吗?
最佳答案
根据评论,您的具体问题最终归结为:
The
<h:outputStylesheet>
doesn't render anything into the HTML output.
考虑到您确实有一个 <h:head>
,唯一的原因可能是模板组合中的错误。例如,当您放置 <h:outputStylesheet>
时,可能会发生这种情况。 在 <ui:define>
的 <ui:composition>
与 template
.在没有看到您的实际作品的情况下,很难确定问题的真正原因,但以下启动示例应该会为您提供新的见解并解决问题。
主模板,/WEB-INF/template.xhtml
:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title><ui:insert name="title">Default title</ui:insert></title>
<h:outputStylesheet name="style/template.css" />
</h:head>
<h:body>
<div id="header">Header</div>
<div id="menu">Menu</div>
<div id="content"><ui:insert name="content">Default content</ui:insert></div>
<div id="footer">Footer</div>
</h:body>
</html>
注意它指的是/resources/style/template.css
在<h:head>
里面因此适用于所有使用此主模板的模板客户端。
模板客户端,/page.xhtml
:
<ui:composition template="/WEB-INF/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:define name="title">
New page title here
</ui:define>
<ui:define name="content">
<h:outputStylesheet name="style/page.css" />
<h1>New content here</h1>
<p>Blah blah</p>
</ui:define>
</ui:composition>
注意它指的是/resources/style/page.css
里面<ui:define>
这将自动结束生成 <head>
反正。你不应该有 <h:head>
在模板客户端中。
(是的,我为 CSS 文件使用了不同的名称,但这只是为了显示您应该放置 <h:outputStylesheet>
组件的确切位置。是的,我删除了 library
属性,因为它实际上应该代表一个“主题”,而不仅仅是像“css”、“js”等内容类型,上面的例子假设了“默认”库/主题)
更新:如您所料,您确实错误地使用了模板组合。您的问题是由 <ui:composition>
引起的在<body>
的主模板。您需要删除它。 <ui:composition>
定义模板组合的根组件。 <ui:composition>
之外的任何内容将在输出中被忽略。
另见:
关于java - 如何在 JSF 2.0 中访问文件 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8286924/