2

我正在创建我的第一个 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>

这是我的模板页面:

  <?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>

但是当我在我的网络浏览器中查看页面源时,我看到了这个:

<?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>

为什么 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>

模板内容:

<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>

这是源视图:

<?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>
4

1 回答 1

7

你基本上使用了错误的方式模板。您基本上是在“以相反的方式”使用它们,而不是应该如何完成。您称为“我的模板页面”的页面应在浏览器中通过其 URL 打开,而不是您称为“facelet 模板”的页面。以后最好防止这种错误的方法是将您指定的页面存储在<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>

/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>

/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>

现在,您应该在浏览器中导航到/page.xhtml(而不是模板文件)。

也可以看看:

于 2012-12-09T11:43:32.777 回答