<?xml version="1.0" encoding="Shift_JIS"?>
<!--
  Copyright 2003-2004 The Apache Software Foundation.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  $Id: layoutservlet.xml,v 1.6 2005/03/18 22:34:48 nekop Exp $
-->
<document>

    <properties>
        <title>Velocity Tools - Layout Servlet</title>
        <author email="nathan@esha.com">Nathan Bubna</author>
        <translator email="nekop@programmers.jp">木村 貴由</translator>
        <translator email="nose@intellilink.co.jp">野瀬 直樹</translator>
        <projectfile>xdocs-ja/view/menu.xml</projectfile>
        <original>Velocity Tools - Layout Servlet</original>
    </properties> 

    <body> 

    <section name="VelocityLayoutServlet (VLS) Overview" alias="VelocityLayoutServlet (VLS) 概要">
        <primary>
        <p>
        This is an extension to the basic VelocityViewServlet.  It provides 
        a simple layout control and customizable error screens for Velocity 
        Tools based projects.  Velocity Tools is distributed with an example
        app demonstrating the use of this servlet.
        </p>
        </primary>
        <p>
        これはベーシックな VelocityViewServlet を拡張したものです。
        Velocity Tools をベースとしたプロジェクトにシンプルなレイアウトの制御と
        カスタマイズ可能なエラー画面を提供します。
        Velocity Tools には、このサーブレットを使用する
        アプリケーション例も含まれています。
        </p>
    </section>

    <section name="Installation" alias="インストール"> 
        <primary>
        <p>
        Since this class is an extension of the VelocityViewServlet (VVS), 
        to use it simply change the servlet-class value of the web.xml entry 
        to the following class:</p>
        </primary>

        <p>
        このクラスは VelocityViewServlet (VVS) を拡張したものなので、
        使うには web.xml の
        servlet-class の値を以下のクラスへ書き換えるだけです:</p>

        <p>
        <a href="../javadoc/org/apache/velocity/tools/view/servlet/VelocityLayoutServlet.html">org.apache.velocity.tools.view.servlet.VelocityLayoutServlet</a>
        </p>

        <subsection name="Configuration Settings" alias="設定">
          <primary>
          <p>Three settings can be added to velocity.properties to control 
          the VLS, or the following default values will be used:</p>
          </primary>
          <p>VLS を制御する3つの設定を velocity.properties に追加することができます。
          設定しない場合、以下のデフォルト値が使用されます:</p>

          <primary>
<sourcecode># Filepath for error template, 
#  relative to web application root directory
tools.view.servlet.error.template = Error.vm

# Directory for layout templates, 
#  relative to web application root directory
tools.view.servlet.layout.directory = layout/

# Filepath of the default layout template 
#  relative to the layout directory 
#  NOT relative to the root directory of the webapp!
tools.view.servlet.layout.default.template =  Default.vm</sourcecode>
          </primary>
<sourcecode># エラーテンプレートのファイルパス
#  ウェブアプリケーションのルートディレクトリからの相対パス
tools.view.servlet.error.template = Error.vm

# レイアウトテンプレートのディレクトリ
#  ウェブアプリケーションのルートディレクトリからの相対パス
tools.view.servlet.layout.directory = layout/

# デフォルトレイアウトテンプレートのファイルパス
#  レイアウトテンプレートディレクトリからの相対パス
#  ウェブアプリケーションのルートディレクトリからの相対パスではない!
tools.view.servlet.layout.default.template =  Default.vm</sourcecode>

        </subsection>

    </section>

    <section name="Layouts" alias="レイアウト">
        <primary>
        <p>
        Now, in your layout templates, the only thing you really need is the
        screen content reference.  So an acceptable layout template could be:</p>
        </primary>
        <p>
        レイアウトテンプレートでは必要なのはたった一つ、
        スクリーンの内容へのリファレンスのみです。
        最小限のレイアウトテンプレートはこうです:</p>
        <p>
<sourcecode>
$screen_content
</sourcecode>
        </p>
        <primary>
        <p>
        ...but that would make this whole thing an idiotic waste of time.
        At the least, you'll probably want to do something along these lines:
        </p>
        </primary>
        <p>
        ...でもこれだけだと単なる時間の浪費になるだけです。
        最低限、このようなことを記述したいと思っていることでしょう:
        </p>
<sourcecode>&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;$!page_title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  $screen_content
&lt;/body&gt;
&lt;/html&gt;
</sourcecode>
        <primary>
        <p>
        This saves you the trouble of doing the basic &lt;html&gt;,&lt;head&gt;, and &lt;body&gt;
        tags in every single screen.  That's the point of layouts: to save effort
        and eliminate redundancy.  Note that this still lets the inner screen
        control the title of the page.  This works because the layout template
        is blessed by the VLS with access to the same context as the screen *after*
        the screen is done with it. Just do a #set( $page_title = "Hello" ) in the
        screen.
        </p>
        </primary>
        <p>
        これは基本的な &lt;html&gt;, &lt;head&gt;, &lt;body&gt;
        タグを全てのスクリーンに書くという手間を省きます。
        冗長なものを取り除き、作業を軽減する--これがレイアウトのポイントです。
        レイアウトは内部のスクリーンに、
        ページのタイトルをコントロールさせているということ注目してください。
        レイアウトテンプレートはスクリーンを処理した *後に*
        同じコンテキストへアクセスし、 VLS によって処理されるからです。
        スクリーンの中で #set( $page_title = "Hello" ) を行うだけでよいのです。
        </p>
        <br clear="all" />
    </section>

    <section name="Alternative Layouts" alias="レイアウトの切り替え">
        <primary>
        <p>VLS provides two ways to specify an alternate 
        template for a requested page:</p>
        </primary>
        <p>VLS は要求されたページのテンプレートを切り替える
        2つの方法を提供します:</p>

        <primary>
        <p><b>1. Specify the layout in the request parameters</b></p>
        </primary>
        <p><b>1. リクエストパラメータによるレイアウトの指定</b></p>
        <primary>
        <p>
          Just add the query string "layout=MyOtherLayout.vm" to any request params
          and the VLS will find it and render your screen within that layout instead
          of the default layout.  It don't matter how you get the layout param into
          the query data, only that it's there.  If you're using the struts tools, the 
          most common will likely be:
        </p>
        </primary>
        <p>
          リクエストパラメータにクエリ文字列  "layout=MyOtherLayout.vm"
          が付いているのを VLS が見付けると、
          デフォルトレイアウトのかわりにそのレイアウトの中にスクリーンを表示します。
          レイアウトパラメータをクエリデータにどのように入れるかは関係ありません。
          もし Struts ツールを使用するなら、一般的にはこのようになるでしょう:
        </p>
        <p>
          &lt;a href="$link.setRelative('MyScreen.vm').addQueryData('layout','MyOtherLayout.vm')"&gt;
        </p>
        <primary>
        <p>
          but form post data will work just as well.
        </p>
        </primary>
        <p>
          これはフォームが POST であっても同様に動作します。
        </p>

        <primary>
        <p><b>2. Specify the layout in the requested screen.</b></p>
        </primary>
        <p><b>2. リクエストされたスクリーン内でのレイアウトの指定</b></p>
        <primary>
        <p>
          In the requested screen, put a line like this:
          <br clear="all" />
          #set( $layout = "MyOtherLayout.vm" )
        </p>
        </primary>
        <p>
          リクエストされたスクリーンで、このような行を記述:
          <br clear="all" />
          #set( $layout = "MyOtherLayout.vm" )
        </p>
        <primary>
        <p>
          This will direct the VLS to use "MyOtherLayout.vm" instead of
          "Default.vm". <i>Setting the layout in this fashion will
          override any layout set by the request parameters.</i>
        </p>
        </primary>
        <p>
          これは VLS に "Default.vm" のかわりに "MyOtherLayout.vm"
          を使うことを指示します。<i>このやり方でレイアウトの設定を行うと、
          リクエストパラメータによるレイアウトの設定は無視するようになります。</i>
        </p>
        <br clear="all" />
    </section>

    <section name="'Navigations', 'Tiles', and How" alias="'Navigations', 'Tiles', and How">
        <primary>
        <p>Those of you who are (or were) Turbine or Struts users will probably want to 
        do more than just set the layout and screen content.  You want to include
        arbitrary "tiles" or "navigations", right?  Well, thanks to Velocity's built-in
        <code>#parse</code> directive, this is easy.</p>
        </primary>
        <p>Turbine や Struts のユーザであれば、
        もっと高度なレイアウトとスクリーンの設定を望んでいるかもしれません。
        "tiles" や "navigations" がお望みですか?
        Velocity の <code>#parse</code> ディレクティブがあります。簡単ですよ。</p>

        <primary>
        <p>First, create your "tile" as a separate template file like:</p>
        </primary>
        <p>まず最初に、このような "tile" を別ファイルとして作成します:</p>

<primary>
<sourcecode>&lt;div id="footer"&gt;I made this!&lt;/div&gt;</sourcecode>
</primary>
<sourcecode>&lt;div id="footer"&gt;私がこれを作った!&lt;/div&gt;</sourcecode>

        <primary>
        <p>For creativity's sake, we'll pretend this code is in a file named "Footer.vm"
        that is located in the root of my webapp like my other non-layout templates.</p>
        </primary>
        <p>そして、他のレイアウト用ではないテンプレートと同じように、
        webapp のルートディレクトリに配置されている "Footer.vm"
        というファイルにこのコードがあるとしましょう。</p>

<sourcecode>
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;$!page_title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

$screen_content

#parse('Footer.vm')

&lt;/body&gt;
&lt;/html&gt;
</sourcecode>

        <primary>
        <p>Easy, eh?</p>
        </primary>
        <p>ね、簡単でしょう?</p>

        <primary>
        <p>Now, what if you have a lot of different "footer" files and you want your screen
        to decide which one will be used?  No problem!  Do something like this:</p>
        </primary>
        <p>もし、異なる"footer" ファイルがたくさんあって、
        スクリーンごとにどれを使うか選びたいときは?
        大丈夫! こんなふうに書けばいいのです:</p>

<sourcecode>
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;$!page_title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

$screen_content

#parse( $screen_footer )

&lt;/body&gt;
&lt;/html&gt;
</sourcecode>

        <primary>
        <p>and in your screen, just do 
        <code>#set( $screen_footer = 'FooFooter.vm' ).</code></p>
        </primary>
        <p>そしてスクリーンで、
        <code>#set( $screen_footer = 'FooFooter.vm' )</code>  と書くだけです。</p>

        <primary>
        <p>Remember, your #parsed footer template will have access to the same 
        velocity context as your layout, which gets the screen's context 
        once the screen is done with it.  This lets you set variables for 
        the layout and footer to use from your screens.</p>
        </primary>
        <p>レイアウトはスクリーン処理後にスクリーンと同じコンテキストを取得し、
        フッタテンプレートはレイアウトと同じ Velocity
        コンテキストにアクセスするということを覚えておいてください[訳注: つまり、スクリーン→レイアウト→フッタテンプレートの順に共通のコンテキストが利用されます]。
        これによって、スクリーンで設定した変数を、
        レイアウトやフッタで利用することができるのです。</p>
        <br clear="all" />
    </section>

    <section name="Error Screen" alias="エラー画面">
      <primary>
      <p>Ok, the idea here is pretty simple.  If an uncaught exception or error is thrown
      at some point during the processing of your screen and layout, the error() method
      of the VLS is called.  This overrides the default error() method of the VelocityViewServlet
      to render a template instead of hardcoded html.
      </p>
      </primary>
      <p>このアイデアは、非常にシンプルなものです。
      キャッチされない例外、
      またはエラーがスクリーンやレイアウトの処理中に発生した場合は、
      VLS の error() メソッドが呼び出されます。
      VLS はハードコードされた html のかわりにテンプレートを表示させるため、
      VelocityViewServlet の error() メソッドをオーバライドしています。
      </p>

      <primary>
      <p>This error screen will be rendered within a layout under the same rules as any other
      screen, and will have the following values placed in its context to help you debug
      the error:
      </p>
      </primary>
      <p>このエラースクリーンは他のスクリーンと同様に、
      レイアウトの中に表示されます。
      また、エラーのデバッグの手助けとなる以下の値をコンテキストに設定します:
      </p>

      <br clear="all" />

      <primary>
      <table>
        <tr>
          <th>Key available to Template</th>
          <th>Value</th>
        </tr>
        <tr>
          <td>$error_cause</td>
          <td>java.lang.Throwable that was thrown</td>
        </tr>
        <tr>
          <td>$stack_trace</td>
          <td>captured output of $error_cause.printStackTrace()</td>
        </tr>
      </table>              
      </primary>
      <table>
        <tr>
          <th>テンプレート内でのキー</th>
          <th>値</th>
        </tr>
        <tr>
          <td>$error_cause</td>
          <td>送出された java.lang.Throwable</td>
        </tr>
        <tr>
          <td>$stack_trace</td>
          <td>$error_cause.printStackTrace() の出力</td>
        </tr>
      </table>              

      <primary>
      <p>In the event that a MethodInvocationException is behind the calling of error(),
      the root cause is extracted from it and dealt with as described above.  But, since
      template reference behavior is partly at fault here, the VLS will also add the
      MethodInvocationException itself to the context as $invocation_exception.  This
      allows you to discover the reference and method call that triggered the root cause.
      To get those, do something like this in your error template:
      </p>
      </primary>
      <p>MethodInvocationException が error() の呼び出しの原因となっている場合、
      その元となる例外が取り出されて、上に記述されているように扱われます。
      しかし、ここではテンプレートのリファレンスの振る舞いが例外の原因の一部なので、
      VLSはコンテキストに $invocation_exception として
      MethodInvocationException 自体も加えます。
      これにより例外の原因となったリファレンスや、
      メソッド呼び出しを発見することができます。
      これらを取得するには、エラーテンプレート内でこのように記述してください:
      </p>
<primary>
<sourcecode>
#if( $invocation_exception )
    oh joy! it's a MethodInvocationException!

    Message: $invocation_exception.message
    Reference name: $invocation_exception.referenceName
    Method name: $invocation_exception.methodName
#end</sourcecode>
      <br clear="all" />
</primary>
<sourcecode>
#if( $invocation_exception )
    おっと! これは MethodInvocationException だ!

    メッセージ: $invocation_exception.message
    リファレンス名: $invocation_exception.referenceName
    メソッド名: $invocation_exception.methodName
#end</sourcecode>
      <br clear="all" />
    </section>

  </body>
</document>

