アプリケーションのモジュール化
Struts1.1では、大きなアプリケーションを複数のサブアプリケーションにわけるのが簡単にできるようになりましたよね。 ステップとしてはこんな感じです:
- それぞれのモジュール(サブアプリケーション)のconfigファイルを作成する
- web.xmlファイルの中にモジュールを追加する
- 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>
...
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>
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に空文字を指定します。