如何在 JSF 2.0 中访问文件 css
我在访问文件 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)
但是两种变体都不起作用。 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>
有人可以帮助我吗?
I have troubles with getting access to file css.
My directory structure:
/resources/style/style.css
/resources/faces/template/baseLayout.xhtml
/WEB-INF/web.xml
/WEB-INF/faces-config.xml
I tried 2 ways to getting access from template page 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" />
But both variants doesn't work.
Servlet-mapping in Web.xml
<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>
Could somebody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论,您的具体问题最终归结为:
考虑到您确实有
,唯一的原因可能是模板组合中的错误。例如,当您将
放置在的
与
的外部时,就会发生这种情况template
。如果没有看到您的实际作文,就很难查明问题的真正原因,但下面的启动示例应该会给您带来新的见解并为您解决问题。主模板
/WEB-INF/template.xhtml
:请注意,它引用
内的
因此适用于使用此主模板的所有模板客户端。/resources/style/template.css
模板客户端
/page.xhtml
:请注意,它引用
内的/resources/style/page.css
,其中无论如何,都会自动以生成的结束。模板客户端中不应有
。(是的,我对 CSS 文件使用了不同的名称,但这只是为了显示您应该将
组件放置在何处。是的,我删除了Library
属性,因为它实际上应该代表一个“主题”,而不仅仅是“css”、“js”等内容类型,上面的示例假设“默认”库/主题)更新:正如猜测的那样,您确实错误地使用了模板组合。您的问题是由主模板的
中的
引起的。您需要删除它。
定义模板组合的根组件。
之外的任何内容都将在输出中被忽略。另请参阅:
Based on the comments, your concrete problem ultimately boils down to:
Considering the fact that you do have a
<h:head>
, the only cause could be a mistake in the template composition. This can for example happen when you have placed the<h:outputStylesheet>
outside the<ui:define>
of an<ui:composition>
withtemplate
. Without seeing your actual compositions, it's hard to pinpoint the real cause of your problem, but the following kickoff example should give new insights and work out for you.The master template,
/WEB-INF/template.xhtml
:Note that it refers
/resources/style/template.css
inside the<h:head>
which thus applies to all template clients using this master template.The template client,
/page.xhtml
:Note that it refers
/resources/style/page.css
inside<ui:define>
which will automatically end up in generated<head>
anyway. You shouldn't have a<h:head>
in the template client.(Yes, I used different names for CSS files, but that is just to show where exactly you should have placed the
<h:outputStylesheet>
components. Yes, I removed thelibrary
attribute because that should actually represent a "theme", not just the content type like "css", "js", etc, the above examples assume the "default" library/theme)Update: as guessed, you're indeed using the template composition incorrectly. Your problem is caused by having an
<ui:composition>
in the<body>
of the master template. You need to remove it. The<ui:composition>
definies the root component of a template composition. Anything outside the<ui:composition>
will be ignored in the output.See also: