多くの開発者は、すべてのStruts関連のもの(アクション、フォーム)を単一のStruts設定ファイルに入れることを好みます。最初の開発では高速ですが、将来のメンテナンスには悪く、開発者がStrutsを意識していない可能性があります。
__6年前、私は20モジュールを含む大規模なStruts開発プロジェクトに参加しました。残念ながら、以前の開発者はすべてのStruts関連のもの(アクション、フォームなど)を単一のStruts設定ファイル(
struts-config.xml
)に入れました。
struts-config.xml
は非常に速く成長し続け、最終的には20 MBに達するでしょう。この設定ファイルへのすべてのアップデートは数分かかるでしょうし、Eclipse IDEでの1回のデバッグ展開でも30分かかります。これは重大なパフォーマンス上の問題であり、プロジェクトが製造日を遅らせる原因となります。何が良いStruts開発経験。
Strutsの設定の詳細を別のモジュールに分割してください。Strutsは簡単に処理できます。
Struts複数の設定ファイルの例
これはデモンストレーションのサンプルプロジェクト構造です。
1.単一モジュール
1つのモジュールが複数のStruts設定ファイルをサポートします。
This is Page 1
-
page2.jsp **
This is Page 2
-
struts-config-1.xml **
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config__1__3.dtd"> <struts-config> <action-mappings> <action path="/Page1" type="org.apache.struts.actions.ForwardAction" parameter="/pages/page1.jsp"/> </action-mappings> </struts-config>
-
struts-config-2.xml **
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config__1__3.dtd"> <struts-config> <action-mappings> <action path="/Page2" type="org.apache.struts.actions.ForwardAction" parameter="/pages/page2.jsp"/> </action-mappings> </struts-config>
web.xmlでは、複数のStruts設定ファイルをカンマ「
、
」で区切ることができます。
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app__2__3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <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-1.xml,/WEB-INF/struts-config-2.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>** .do</url-pattern> </servlet-mapping> </web-app>
テストする
page1.jspが表示されます。
。
http://localhost:8080/StrutsExample/common/Welcome.do
page2.jspが表示されます。
両方のStruts設定はロードされたプロパティです。
2.複数のモジュール
複数のモジュールはそれぞれ独自のStruts設定ファイルを持っています。
-
admin/welcome.jsp **
Welcome to admin page
-
common/welcome.jsp **
Welcome to common page
”
struts-config-admin.xml
“ファイルと ”
struts-config-admin.xml
“ファイルには同じ設定が含まれているため、web.xmlの ”
config
“パラメータ値を使用してStrutsが区別できます。
Struts 2では、 ”
Namespace
“がこの ”
config parameter
“設定を置き換えるより効率的な方法です。
-
struts-config-admin.xml、struts-config-admin.xml **
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config__1__3.dtd"> <struts-config> <action-mappings> <action path="/Welcome" type="org.apache.struts.actions.ForwardAction" parameter="/welcome.jsp"/> </action-mappings> </struts-config>
-
web.xml **
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app__2__3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <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-1.xml,/WEB-INF/struts-config-2.xml </param-value> </init-param> <init-param> <param-name>config/admin</param-name> <param-value> /WEB-INF/struts-config-admin.xml </param-value> </init-param> <init-param> <param-name>config/common</param-name> <param-value> /WEB-INF/struts-config-common.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>** .do</url-pattern> </servlet-mapping> </web-app>
テストする
http://localhost:8080/StrutsExample/admin/”
config/admin
“はこのURLパターンに一致します。http://localhost:8080/StrutsExample/common/
admin/welcome.jspが表示されます
。
http://localhost:8080/StrutsExample/common/Welcome.do
common/welcome.jspが表示されます
各モジュールには独自のStruts設定ファイルがあります。