Jsf-parameters-tag

提供:Dev Guides
移動先:案内検索

JSF-ui:paramタグ

_ui:param tag_を使用して、テンプレートファイルまたはインクルードファイルにパラメーターを渡すことができます。

JSF-template tagsの章では、テンプレートタグを作成して使用する方法を学びました。 ヘッダー、フッター、コンテンツ、すべてのセクションを組み合わせたテンプレートなど、さまざまなセクションを定義しました。

今、私たちは学びます-

  • テンプレートのさまざまなセクションにパラメーターを渡す方法 *パラメータをテンプレートに渡す方法

テンプレートのセクションのパラメーター

パラメーターの作成:common.xhtml

ui:includeタグにパラメーターを追加します。* ui:param *タグを使用して、Headerセクションに渡す値を含むパラメーターを定義します。

<ui:insert name = "header" >
   <ui:include src = "/templates/header.xhtml" >
      <ui:param name = "defaultHeader" value = "Default Header"/>
   </ui:include>
</ui:insert>

パラメーターを使用:header.xhtml

<ui:composition>
   <h1>#{defaultHeader}</h1>
</ui:composition>

テンプレートへのパラメーター

パラメーターの作成:home.xhtml

ui:compositionタグにパラメーターを追加します。 ui:param タグを使用して、テンプレートに渡す値を含むパラメーターを定義します。

<ui:composition template = "templates/common.xhtml">
   <ui:param name = "title" value = "Home"/>
</ui:composition>

パラメーターを使用:common.xhtml

<h:body>
   <h2>#{title}</h2>
</h:body>

応用例

テストJSFアプリケーションを作成して、JSFのテンプレートタグをテストしましょう。

Step Description
1 Create a project with a name helloworld under a package com.finddevguides.test as explained in the JSF - Templates Tag chapter.
2 Modify header.xhtml,and common.xhtml files under src → main → webapp → templates folder. Modify them as explained below.
3 Modify home.xhtml as explained below. Keep rest of the files unchanged.
4 Compile and run the application to make sure business logic is working as per the requirements.
5 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
6 Launch your web application using appropriate URL as explained below in the last step.

header.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:ui = "http://java.sun.com/jsf/facelets">

   <body>
      <ui:composition>
         <h1>#{defaultHeader}</h1>
      </ui:composition>
   </body>
</html>

common.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:head>
   <h:body>
      <h2>#{title}</h2>

      <div style = "border-width:2px; border-color:green; border-style:solid;">
         <ui:insert name = "header" >
            <ui:include src = "/templates/header.xhtml" >
               <ui:param name = "defaultHeader" value = "Default Header"/>
            </ui:include>
         </ui:insert>
      </div>
      <br/>

      <div style = "border-width:2px; border-color:black; border-style:solid;">
         <ui:insert name = "content" >
            <ui:include src = "/templates/contents.xhtml"/>
         </ui:insert>
      </div>
      <br/>

      <div style = "border-width:2px; border-color:red; border-style:solid;">
         <ui:insert name = "footer" >
            <ui:include src = "/templates/footer.xhtml"/>
         </ui:insert>
      </div>

   </h:body>
</html>

home.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:body>
      <ui:composition template = "templates/common.xhtml">
         <ui:param name = "title" value = "Home"/>

         <ui:define name = "content">
            <br/><br/>
             <h:link value = "Page 1" outcome = "page1"/>
             <h:link value = "Page 2" outcome = "page2"/>
            <br/><br/>
         </ui:define>
      </ui:composition>
   </h:body>
</html>

すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。

JSF ui:param result