如何将通用内容包含到多级模板页面中 [英] How to include common content into multiple level template page
问题描述
我正在尝试将通用页面包含到模板中,但是我得到的只是空白页面而没有错误.
I am trying include a common page into a template but all I get is a blank page without error.
common.xhtml实际上具有template.xhtml中指示的内容.似乎template.xhtml无法识别两个级别的包含.
common.xhtml actually has the content that indicate in the template.xhtml. It seems the template.xhtml doesn't recognize the two level include.
template.xhtml
template.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:ub="http://jboss.com/products/seam/ub-taglib"
xmlns:rich="http://richfaces.ajax4jsf.org/rich">
<head>
<ui:insert name="css" />
<ui:insert name="header" />
</head>
<body>
<ui:insert name="body" />
</body>
</html>
custom.xhtml
custom.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.ajax4jsf.org/rich"
xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
xmlns:c="http://java.sun.com/jstl/core"
template="template.xhtml">
<ui:define name="css">
<link rel="stylesheet" type="text/css" href="/custom.css/>
</ui:define>
<ui:include src="common.xhtml" />
</ui:composition>
common.xhtml
common.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.ajax4jsf.org/rich"
xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
xmlns:c="http://java.sun.com/jstl/core"
template="template.xhtml">
<ui:define name="header">
<h1>header</h1>
</ui:define>
<ui:define name="body">
<table><tr><td>Table</td></tr></table>
</ui:define>
</ui:composition>
推荐答案
这确实不起作用. <ui:define>
应该用于模板客户端(即具有<ui:composition template="...">
的页面),而不用于包含文件(即具有<ui:composition>
而没有template
的页面).但是,您可以仅从现有的主模板中扩展".
This indeed won't work. The <ui:define>
is supposed to be used in a template client (i.e. a page with <ui:composition template="...">
), not in an include file (i.e. a page with <ui:composition>
without template
). You can however just "extend" from existing master templates.
从custom.xhtml
删除:
<ui:include src="common.xhtml" />
common.xhtml
template="custom.xhtml"
然后在浏览器中打开common.xhtml
而不是custom.xhtml
.
And open common.xhtml
instead of custom.xhtml
in browser.
无关与具体问题无关,为防止最终用户表单能够直接在浏览器中打开custom.xhtml
或template.xhtml
,建议将它们移到/WEB-INF
文件夹中.此外,您知道<h:head>
和<h:outputStylesheet>
组件吗?我建议利用它们.同样,使<h1>
最终以<head>
结尾是没有意义的.也许您是说<ui:insert name="header">
位于<body>
内部?此外,您可以轻松地将<h1>
放在模板中,这样就不必在每个模板客户端中都重复它们.
Unrelated to the concrete problem, to prevent the enduser form being able to open custom.xhtml
or template.xhtml
directly in browser, it's recommended to move them into the /WEB-INF
folder. Further, are you aware of the <h:head>
and <h:outputStylesheet>
components? I suggest to make use of them. Also, having a <h1>
to ultimately end up in <head>
makes no sense. Perhaps you meant the <ui:insert name="header">
to be inside <body>
? Further, you could easily put that <h1>
in the template so that you don't need to repeat them in every template client.
/WEB-INF/templates/template.xhtml
<html ...>
<h:head>
</h:head>
<h:body>
<ui:insert name="header" />
<ui:insert name="body" />
</body>
</html>
/WEB-INF/templates/custom.xhtml
(CSS文件位于/resources
文件夹中)
/WEB-INF/templates/custom.xhtml
(CSS file is placed in /resources
folder)
<ui:composition ... template="/WEB-INF/templates/template.xhtml">
<ui:define name="header">
<h1><ui:insert name="custom-header" /></h1>
</ui:define>
<ui:define name="body">
<h:outputStylesheet name="custom.css" target="head" />
<ui:insert name="custom-body" />
</ui:define>
</ui:composition>
/page.xhtml
<ui:composition ... template="/WEB-INF/templates/custom.xhtml">
<ui:define name="custom-header">
header
</ui:define>
<ui:define name="custom-body">
<table><tr><td>Table</td></tr></table>
</ui:define>
</ui:composition>
另请参见:
- 哪个我需要将XHTML文件放在/WEB-INF中,哪些不需要?
- 如何在Facelets中引用CSS/JS/图像资源模板?
- Which XHTML files do I need to put in /WEB-INF and which not?
- How to reference CSS / JS / image resource in Facelets template?
See also:
这篇关于如何将通用内容包含到多级模板页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!