エンタープライズ:特集 | 2003/10/06 18:00:00 更新 |

特集:第1回 フレームワーク「Struts」の基礎を知る (7/8)
Strutsの設定と動作を理解する
Fig.6は、index.jspファイルの実行結果だ。index.jspファイルの内容は、List 1のようになっている。
One Point: |
index.jspファイルは、パス名の終わりが「.do」ではないので、ActiveServletではなく、JSPコンテナによって直接実行される。 |
List 1■index.jspファイルの内容
1: <%@ taglib uri="/tags/struts-logic" prefix="logic" %> 2: <logic:redirect forward="welcome"/> 3: 4: <%-- 5: 6: Redirect default requests to Welcome global ActionForward. 7: By using a redirect, the user-agent will change address to match the path of our 8: Welcome ActionForward. 9: 10: --%> |
上記List 1のindex.jspファイル内で注目したいのは、次の2点だ。
1. タグライブラリの定義
1行目にある次の行は、このJSPファイルで用いるタグライブラリの定義だ。
1: <%@ taglib uri="/tags/struts-logic" prefix="logic" %> |
prefixは、カスタムタグのプレフィックス名を定義する個所だ。ここでは、「prefix="logic"」と記述されているため、このJSPファイル内で、<logic:任意名>のカスタムタグが使えるようになる。uriの個所は、タグライブラリを識別するための名前だ。ここでは、「uri="/tags/struts-logic"」と記述されている。
なお、タグライブラリの関連付けは、web.xmlファイル内で行われる。実際に、web.xmlファイルを確認すると、次の定義が確認できる。
<taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib> |
上記「<taglib>」の要素は、JSPページ内に記述された「<%@ taglib %>」のマッピングを処理する。ここでは、<taglib-uri>要素に「/tags/struts-logic」が、<taglib-location>要素には「/WEB-INF/struts-logic.tld」が指定されている。
この定義により、JSPページ内に、「<%@ taglib uri="/tags/struts-logic" %>」と記述した際、JSPコンテナは、WEB-INF/ディレクトリのstruts-logic.tldというファイルを読み込む。つまりstruts-logic.tldのように拡張子.tldをもつファイル(以下TLDファイルと呼ぶ)が、カスタムタグの定義ファイルとなる。TLDファイル内では、いずれかのタグが記述された場合、どのようなクラスファイルが処理するのかが定義されている。Strutsで用意されているカスタムタグをTable 1-1に示す。
Table 1-1■Strutsで用意されているカスタムタグ
|
One Point: |
Table 1-1に示したuriは、struts-blank.warファイルを展開した際に用意するweb.xmlファイル内で定義されている |
2. カスタムタグの利用
List 1の2行目が、実際にカスタムタグを利用している個所だ。
2: <logic:redirect forward="welcome"/> |
すでに「1.」項で説明したように、<logic:任意名>は、web.xmlファイルの<taglib>要素によって、struts-logic.tldにマッピングされている。struts-logic.tldファイルには、次のような定義がある。
<tag> <name>redirect</name> <tagclass>org.apache.struts.taglib.logic.RedirectTag</tagclass> <attribute> <name>anchor</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>forward</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> 〜以下、幾つか<attribute>が続くが省略… </tag> |
この定義により、<logic:redirect>と記述すると、「org.apache.struts.taglib.logic.RedirectTag」というクラスがカスタムタグの処理をする。この「RedirectTag」クラスは、Strutsによって提供されるタグライブラリである。RedirectTagクラスは、任意の場所にリダイレクトする機能を提供するものだ。ここでは、「forward="welcome"」となっているため、welcomeという名前の定義にリダイレクトされることが分かる。
また、welcomeという名前のリダイレクト定義は、struts-config.xmlファイルに記述されている。さらにstruts-config.xmlファイルには、次の表記が見られる。
<global-forwards> <!-- Default forward to "Welcome" action --> <!-- Demonstrates using index.jsp to forward --> <forward name="welcome" path="/Welcome.do"/> </global-forwards> |
この定義では、<logic:redirect forward="welcome">と記述した場合のリダイレクト先は、Welcome.doというパスになる。この結果、クライアントは「http://サーバー名:8080/struts-keiji/Welcome.do」というURLに再アクセスする。
すでに説明したように、「.do」で終わるパス(ファイル)指定は、ActionServletが処理を担う。なぜ「.do」で終わるパスをActionServletが実行するかというと、その根元はweb.xmlファイル内にある。web.xmlファイルには、次のようなサーブレットのマッピング定義があり、URLとして「任意名.do」が呼び出された際には、ActionServletが動作するよう仕組まれているのだ。当然、このマッピングの設定を変更すれば、パス名の後ろを「.do」以外のものに変更することも可能だ。
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> |
さて、ActionServletは、「任意名.do」というURLが要求された際、「任意名」の部分をstruts-config.xmlファイル内の<action-mappings>要素の中から探し出す仕様になっている。struts-blank.warファイルを展開した場合に用意するstruts-config.xmlファイルには、次のようにURLのパス「Welcome」に対する定義がある。
<action-mappings> <!-- Default "Welcome" action --> <!-- Forwards to Welcome.jsp --> <action path="/Welcome" type="org.apache.struts.actions.ForwardAction" parameter="/pages/Welcome.jsp"/> </action-mappings> |
この定義によって、org.apache.struts.actions.ForwardActionというクラスが実行される。ForwardActionクラスは、Actionクラスのサブクラスであり、Strutsによって用意されている。また、このForwardActionクラスは、parameterで指定された内容に処理をフォワードする機能を持つ。すなわち、この定義によってpages/ディレクトリ内のWelcome.jspというファイルが実行された結果がクライアントに送信される。実際にWelcome.jspファイルの内容を見ると、List 2のように、Fig.6の出力を得るためのJSPファイルとなっている。
List 2■Welcome.jspファイル
1: <%@ taglib uri="/tags/struts-bean" prefix="bean" %> 2: <%@ taglib uri="/tags/struts-html" prefix="html" %> 3: <%@ taglib uri="/tags/struts-logic" prefix="logic" %> 4: 5: <html:html locale="true"> 6: <head> 7: <title><bean:message key="welcome.title"/></title> 8: <html:base/> 9: </head> 10: <body bgcolor="white"> 11: 12: <logic:notPresent name="org.apache.struts.action.MESSAGE" scope="application"> 13: <font color="red"> 14: ERROR: Application resources not loaded -- check servlet container 15: logs for error messages. 16: </font> 17: </logic:notPresent> 18: 19: <h3><bean:message key="welcome.heading"/></h3> 20: <p><bean:message key="welcome.message"/></p> 21: 22: </body> 23: </html:html> |
Welcome.jspファイルでは、幾つかのカスタムタグが使われている。ここでは、7行目にある「<bean:message>」のカスタムタグについて見てみよう。
7: <title><bean:message key="welcome.title"/></title> |
ここで使われている<bean:message>は、アプリケーションのリソースからメッセージを取得し、その内容を取り込むという機能を持つものだ。
One Point: |
<bean:message>のようにプレフィックスがbeanであるのは、1行目の<%@ taglib %>で定義されているからである。プレフィックス名は、<%@ taglib %>の定義で、自由に変更できる。 |
リソースの定義は、struts-config.xmlファイルの<messages-resources>要素で定められる。struts-config.xmlファイルには、次の<message resources>要素が見られる。
<message-resources parameter="resources.application"/> |
この定義によって、<bean:message>は、WEB-INF/classes/ディレクトリ下のresources/サブディレクトリ内「application.properties」ファイルのリソースを示すようになる(ピリオドはディレクトリの階層を示す)。また、WEB-INF/classes/resources/ディレクトリに置かれた「application.properties」ファイル内は、List 3のようになっている。
List 3■application.propertiesファイル
1: # -- welcome -- 2: welcome.title=Struts Blank Application 3: welcome.heading=Welcome! 4: welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.) |
2行目を見ると、welcome.titleが次のように定義されていることが分かる。
2: welcome.title=Struts Blank Application |
これにより、<bean:message key="welcome.tile"/>の個所では、「Struts Blank Application」という文字列に置き換えられることになる。メッセージをJSPに記述せず、リソースとして「application.properties」ファイルに記述する理由は、国際化対応にある。
JSPファイル中に直接メッセージを埋め込むと、ほかの言語に移植するのが大変だが、「application.properties」ファイルにまとめておけば、このファイル内容を各言語対応に変更するだけで対応することができるためだ。
前のページ | 1 2 3 4 5 6 7 8 | 次のページ
[大澤文孝,ITmedia]