アプリケーションのモジュール化

Struts1.1では、大きなアプリケーションを複数のサブアプリケーションにわけるのが簡単にできるようになりましたよね。 ステップとしてはこんな感じです:

  1. それぞれのモジュール(サブアプリケーション)のconfigファイルを作成する
  2. web.xmlファイルの中にモジュールを追加する
  3. forwardかSwitchActionを使ってモジュール間を行き来する

web.xml:


...
<web-app>
<servlet>
...
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/conf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/agent</param-name>
<param-value>/WEB-INF/conf/struts-config-agent.xml</param-value>
</init-param>
...
それぞれのモジュールをスイッチする方法は主に2つあります。

  1. forward(global/local)を使いcontextRelative="true"にセットする
  2. org.apache.struts.actions.SwitchActionを使う

forward(global-forwardの場合):


<global-forwards>
<forward name="agent"
contextRelative="true"
path="/agent/index.do"
redirect="true" />
</global-forwards>

SwitchActionを使う場合:


<action-mappings>
<action path="/switchModule"
type="org.apache.struts.actions.SwitchAction"/>
...
</action-mappings>
そして、agentにスイッチする時は次のようなURIを使います。
http://localhost:8080/switchModule.do?prefix=/agent&page=/index.do
ディフォルトのモジュールに戻る時はこんなURIになります。
http://localhost:8080/switchModule.do?prefix=&page=/welcome.do
みればわかると思いますが、prefixがモジュールの名前、pageはmodule-relative URI(そのモジュールのパス)です。どちらも"/"で始まりますが、ディフォルトのモジュールに戻る時はprefixに空文字を指定します。