Struts – ワイルドカードの例
この例をダウンロードする –
Struts-Wildcards-Example.zip
Strutsワイルドカードは、Strutsプロジェクトがいくつかの通常のファイル構造に従っている限り、struts-config.xmlファイルの繰り返しを減らすのに役立ちます。たとえば、ユーザモジュールでCRUD関数を実装するには、struts-config.xmlは次のようになります
1.ワイルドカードなし
各リストに対して4つのアクションマッピングを作成し、関数を追加、削除、更新し、繰り返しをたくさん行う必要があります。
<?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="/ListUserAction"
type="com.mkyong.common.action.UserAction"
parameter="ListUser"
>
<forward name="success" path="/pages/ListUser.jsp"/>
</action>
<action
path="/AddUserAction"
type="com.mkyong.common.action.UserAction"
parameter="AddUser"
>
<forward name="success" path="/pages/AddUser.jsp"/>
</action>
<action
path="/EditUserAction"
type="com.mkyong.common.action.UserAction"
parameter="EditUser"
>
<forward name="success" path="/pages/EditUser.jsp"/>
</action>
<action
path="/DeleteUserAction"
type="com.mkyong.common.action.UserAction"
parameter="DeleteUser"
>
<forward name="success" path="/pages/DeleteUser.jsp"/>
</action>
</action-mappings>
</struts-config>
2.ワイルドカード付き
Strutsのワイルドカード機能を使用すると、struts-config.xmlを1つのアクションマッピングに絞り込むことができます。
-
struts-config.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="/** UserAction"
type="com.mkyong.common.action.UserAction"
parameter="{1}User"
>
<forward name="success" path="/pages/{1}User.jsp"/>
</action>
</action-mappings>
</struts-config>
ユースケースを見て、アクセスを試してみましょう
http://localhost:8080/StrutsExample/EditUserAction.do
。ザ
”
EditUserAction.do
“は ”
/
UserAction
“パターンと一致し、
にマッチした文字列 ”
Edit
“は後で使用するために
\ {1}
で表されます。
上記の場合、ワイルドカードのアクションマッピングは
<アクション
パス= "/** UserAction"
type = "com.mkyong.common.action.UserAction"
パラメータ= "{1}ユーザ"
>
<forward name = "success" path = "/pages/{1} User.jsp"/>
</action>
に
<アクション path = "/EditUserAction" type = "com.mkyong.common.action.UserAction" パラメータ= "EditUser" > <forward name = "success" path = "/pages/EditUser.jsp"/> </action>
===結論
どちらのstruts-config.xmlサンプルも同じ機能を持ちますが、
ワイルドカードのサポートの回数を減らします。ただし、これを酷使しないでください**
プロジェクトのワイルドカード機能は、通常よりも扱いにくい
宣言。