我正在创建我的第一个facelets/JSF应用程序.在我的第一页中,我添加了一个facelet模板:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view>
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<body>
<ui:insert name='top'>
<ui:include src="/templates/template_a.xhtml"></ui:include>
</ui:insert>
</body>
</f:view>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的模板页面:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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: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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<body>
<ui:composition template="/inwert_a.xhtml">
<ui:define name="top">
<h2>naglowek</h2>
</ui:define>
</ui:composition>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但是当我在网络浏览器中查看页面源代码时,我看到了:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h2>naglowek</h2>
</body>
</html>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为什么JSF在我使用时会创建一个正文和其他HTML标记两次<ui:composition>?
更新后,我的代码看起来像主页面:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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: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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<f:subview id="a">
<ui:insert name="top">
<ui:include src="aaa.xhtml" ></ui:include>
</ui:insert>
</f:subview>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
模板内容:
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="/mainpage.xhtml"
>
<ui:define name="top">
<h2> aaaaaaaaa </h2>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
这是源视图:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title></head>
<body>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h2>aaaaaaaaa</h2>
</body>
</html>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
你基本上是以错误的方式使用模板.你基本上是按照它应该如何完成的方式"反过来"使用它们.您调用"我的模板页面"的页面应该通过浏览器中的URL打开,而不是您称为"面板模板"的页面.最好防止将来出现这种错误,将您在<ui:composition template>(和<ui:include src>)/WEB-INF文件夹中指定的页面存储在文件夹中,这样它们就不会被意外(或黑客)直接打开.
这是一个适用于您案例的启动示例:
/WEB-INF/templates/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:head>
<h:body>
<ui:insert name="top">
<ui:include src="/WEB-INF/templates/top.xhtml">
</ui:insert>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
/WEB-INF/templates/top.xhtml:
<ui:composition
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">
<p>Some default content for top</p>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
/page.xhtml
<ui:composition template="/WEB-INF/templates/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="top">
<h2>naglowek</h2>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
现在,您应该在浏览器中导航到/page.xhtml(因此不是模板文件).
| 归档时间: |
|
| 查看次数: |
2172 次 |
| 最近记录: |