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>

しかし、Web ブラウザーでページのソースを見ると、次のように表示されます。

<?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 が body と他の HTML タグを 2 回作成するのはなぜ<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

あなたは基本的に間違った方法でテンプレートを使用しています。基本的に、本来の方法とは「逆に」それらを使用しています。「マイ テンプレート ページ」と呼ばれるページは、「facelet テンプレート」と呼ばれるページではなく、ブラウザの 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>

/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 に答える