<?xml version="1.0" encoding="Shift_JIS"?>
<document>

  <properties>
    <author email="jon@latchkey.com">Jon S. Stevens</author>
    <title>You make the decision - Saying Hello</title>
    <translator>熊坂祐二</translator>
    <translator>高橋達男</translator>
    <translator>羽生田恒永</translator>
    <original>ymtd/ymtd-saying-hello</original>
  </properties>

<body>

<section name="Saying Hello" alias="Saying Hello">

<primary>
<p>
Ok, lets start off with some easy examples. These examples really do not
even touch on the basics of correct design. However, they still make
good examples because correct design is often harder than showing a
simple example. We will show better examples further along in this
essay.
</p>
</primary>
<p>
では、簡単な例からはじめましょう。
ここで示す例は正しい設計にもとづくものではありませんが、
それでも良い例にはなります。
なぜなら多くの場合、正しい設計を行うことは単純な例を示すのより難しいからです。
このエッセイではさらに良い例を示して行こうと思います。
</p>

<primary>
<p>
For the first example, we show that there are two different approaches
of doing the same exact thing using both JSP and Velocity. This is an
example of printing out a parameter with JSP:
</p>
</primary>
<p>
最初の例では、JSP と Velocity という異なる2つのやり方で、
まったく同じことを行なってみます。
これは、JSP でパラメータを出力する例です。
</p>

<source><![CDATA[
<html>
<head><title>Hello</title></head>
<body>
<h1>
<%
if (request.getParameter("name") == null) {
   out.println("Hello World");
}
else {
  out.println("Hello, " + request.getParameter("name"));
}
%>
</h1>
</body></html>]]></source>

<primary>
<p>
This is an example of doing the same thing with Velocity:
</p>
</primary>
<p>
これは、Velocityで同じことをする例です。
</p>

<source><![CDATA[
<html>
<head><title>Hello</title></head>
<body>
<h1>
#if ($request.getParameter("name") == null)
   Hello World
#else
   Hello, $request.getParameter("name")
#end
</h1>
</body></html>]]></source>

<primary>
<ul>
<li><a href="./images/hello-jsp.gif" target="newWindow">Hello JSP Screen shot</a></li>
<li><a href="./images/hello-velocity.gif" target="newWindow">Hello Velocity Screen shot</a></li>
</ul>
</primary>
<ul>
<li><a href="./images/hello-jsp.gif" target="newWindow">Hello JSP スクリーンショット</a></li>
<li><a href="./images/hello-velocity.gif" target="newWindow">Hello Velocity スクリーンショット</a></li>
</ul>

<primary>
<p>
<font size="-1">[ These two screen shots demonstrate the idea that one
cannot easily look at what the code is doing in a browser when using
JSP. ]</font>
</p>
</primary>
<p>
<font size="-1">[ この2つの画面ショットで示しているのは、JSPを使うと、
コードがやっていることをブラウザでは簡単に見ることができないということです。 ]</font>
</p>

<hr noshade="true" size="1"/>

<primary>
<p>
The primary difference between the two is the way that output is
performed. With JSP, one needs to embed "code" within <code>&lt;%
%&gt;</code> tags and for Velocity, one does not need to embed "code"
within tags. One can simply use the Velocity Template Language (VTL)
directly in any portion of the template.
</p>
</primary>
<p>
この2つの方法の主な違いは、出力の実行方法です。
JSPでは、<code>&lt;% %&gt;</code>タグ内に「コード」を埋め込む必要があるのに対し、
Velocityではそうする必要がありません。
単に Velocity Template Language (VTL) を使って、
テンプレートに直接書き込むだけです。
</p>

<primary>
<p>
The benefit (and detriment) of the embedded code is that the JSP code
within a page will not show up when the file is simply loaded into
the browser. On the other hand, there might be a case where one may
desire it to show up (for example, in debugging).
</p>
</primary>
<p>
ファイルがそのままブラウザにロードされるときに
ページ内のJSPコードが表示されないということは、
埋め込まれたコードの長所でもあり短所でもあります。短所でもあるというのは、
コードを表示して欲しいと思う場合もあるかもしれないからです
(例えばデバッグの時など)。
</p>

<primary>
<p>
Another issue with JSP is the fact that even the most basic examples
start to blow the whole MVC paradigm right out of the water. The reason
is that embedding HTML code within Java code is a bad design decision
because it makes it more difficult to modify the look and feel of an
application at a later date. It also destroys the concept of MVC
separation where the View (ie: HTML) display of the page is separated
from the Model and Controller. For example, if just the word "Hello"
needed to be in bold, we would need to embed <code>&lt;b&gt;
&lt;/b&gt;</code> tags into the <code>out.println()</code> statement.
</p>
</primary>
<p>
JSPのもう一つの問題は、この最も基本的な例でさえ、MVC (Model-View-Controller)
パラダイム全体がすでに破壊されはじめていることです。
というのは、Javaコード内に HTML コードを埋め込むのは、
後でアプリケーションのルック＆フィールを変えにくくなるため、
設計としてはよくないからです。
また、この場合、ページの View (つまり HTML) 表示は Model と
Controller から切り離すという、MVC 分離という概念が破壊されています。
例えば、「Hello」という語だけを強調したい場合、
<code>out.println()</code>ステートメントに
<code>&lt;b&gt;&lt;/b&gt;</code>タグを埋め込む必要があるでしょう。
</p>

<primary>
<p>
Of course, <i>people in the know</i>, would recommend that we write JSP like
this:
</p>
</primary>
<p>
もちろん、<i>事情通</i>なら、以下のように JSP を書くことを勧めるでしょう。
</p>

<source><![CDATA[
<html>
<head><title>Hello</title></head>
<body>
<h1>
<% if (request.getParameter("name") == null) %>
          Hello World
<% else %>
          Hello, <% request.getParameter("name"); %>
</h1>
</body></html>]]></source>

<primary>
<p>
Or with Struts:
</p>
</primary>
<p>
または Struts なら以下のようになるでしょう。
</p>

<source><![CDATA[
<html>
<head><title>Hello</title></head>
<body>
<h1>
<logic:notPresent parameter="name">
          Hello World
</logic:notPresent>
<logic:present parameter="name">
          <bean:parameter id="name" name="name"/>
          Hello, <bean:write name="name"/>
</logic:present>
</h1>
</body></html>
]]></source>

<primary>
<ul>
<li><a href="./images/hello-jsp2.gif" target="newWindow">Hello JSP Screen shot</a></li>
</ul>
</primary>
<ul>
<li><a href="./images/hello-jsp2.gif" target="newWindow">Hello JSP
スクリーンショット</a></li>
</ul>

<primary>
<p>
<font size="-1">[ This is the new JSP screen shot showing the above
example when loaded directly into the browser. ]</font>
</p>
</primary>
<p>
<font size="-1">[ これは、上記の例を直接ブラウザにロードしたときの新しいJSPのスクリーンショットです。 ]</font>
</p>

<hr noshade="true" size="1"/>

<primary>
<p>
The point that needs to be made is that in order to make JSP "pure", one
really needs to jump through hoops. The example above looks very similar
to the Velocity example. However, one still needs to embed the necessary
<code>&lt;% %&gt;</code> tags everywhere. More typing == more chances
for mistakes! There is also a bit of a disconnect as to when the ";"
needs to be added and when it does not. With Velocity, the rule is that
you place a # before a directive and a $ before a member in the Context.
</p>
</primary>
<p>
ここで主張したいことは、JSP を「純粋に」するためには厳しい試練を受けなければならないということです。
上記の例は、Velocity の例と同じように見えます。しかし、JSP では、ここでも
至る所に必要な<code>&lt;% %&gt;</code>タグを埋め込む必要があります。
「==」をタイプすることが増えると、間違いの可能性も増えるのです!
また、「;」を加える必要があるのとそうでないのとでは、ささいですが違っています。
Velocity でのルールは、[単純に言えば] 指示子の前には「#」をつけ、
コンテキストのメンバーの前には「$」をつけるというもの[だけ] です。
</p>

<primary>
<p>
As you can see from the example image, there is now a bit more
information in the displayed page, except that it is also missing all of
the logic which was used to build the page. If one views the equivalent
Velocity template directly in the browser, without it being rendered
first, all of the logic in the template remains visible. The advantage
is that it is easier to debug problems.
</p>
</primary>
<p>
すぐ上の例のイメージからお分かりの通り、表示されたページでは [最初の例に比べると]
表示されている情報が増えていますが、
ページを構築するのに使われるロジックはまったく抜けています。
もし同じ表示を行う Velocity のテンプレートをブラウザで直接見ると、
最初にテンプレート処理がされなければ、テンプレートのロジックはすべて見ることができます。
その利点はデバッグするのが容易であるということです。
</p>

<primary>
<p>
You make the decision.
</p>
</primary>
<p>
[どちらを選ぶかは] あなたが判断してください。
</p>

<primary>
<p>
<strong>[ <a href="ymtd.html">Home</a> &lt;- Previous | 
    Next -&gt; <a href="./ymtd-generation.html">Generation?</a> ]
</strong></p>
</primary>
<p>
<strong>[ <a href="ymtd.html">YMTDトップ</a> &lt;- 前 | 
    次 -&gt; <a href="./ymtd-generation.html">生成?</a> ]
</strong></p>

</section>

</body>
</document>

