単一のアクションクラスでStruts2で複数のアクションを作成するには? 例を教えてください。インターネットの検索で次のコードを見つけましたが、エラーが発生しています OR リクエストごとに個別のアクションクラスを作成する必要がありますか?
3 に答える
@Quaternionが言ったことに加えて、S2アクションはそれ自体がJavaクラスであり、あなたが何を求めているのかわかりません(単一アクションクラスのStruts2の複数のアクション)。
単一の S2 アクション クラスを使用する方法はいくつかあります。
単一のアクション クラスを異なるエイリアスのようにマップします。
<action name="A" class="MyActionClass">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="B" class="MyActionClass">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
しかし、異なるアクション リクエストを異なるアクション メソッドにマッピングしたいと考えています。S2 は任意の数のメソッドを定義する方法を提供し、UI からどのアクション クラスのどのメソッドを呼び出すかを S2 に指示できます。
たとえば、次のようなユーザー インタラクションの処理を担当する UserAction クラスがあるとします。
- ログイン
- ログアウト
- 登録
このため、さまざまな Action クラスを作成する必要はありませんが、UserAction という単一のアクション クラスを作成し、その中でさまざまなメソッドを定義し、S2 を次のようなさまざまなメソッドを呼び出すように構成できます。
<action name="Logon" class="UserAction" method="logon">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="Logout" class="UserAction" method="logout">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="Register" class="tUserAction" methood="register">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
これがあなたの疑問を解消するのに役立つことを願っています
上記の使用例MyActionClass
では、2 つのエイリアス A と B がマッピングされており、任意の番号にマッピングできます。
単一の場所でコードを追加/編集/削除するなど、選択したアクションに基づいて、単一のアクションで複数のリダイレクトを記述したいという質問がありました。要件に合ったDispatchActionを探す必要があります。
以下はあなたが見ることができるいくつかの例です、すべてはDispatchActionを実装する方法を提供します。
Another method:
Here is the action class with 3 actions (execute, action1, action2)
public class MyAction extends ActionSupport{
public String execute(){
return SUCCESS;
}
public String action1(){
return SUCCESS;
}
public String action2(){
return SUCCESS;
}
}
Here is config:
<action name="myAction" class="MyAction">
<result>showMe.jsp</result>
<result name="input">showMe.jsp</result>
</action>
Action "execute" will be called by default. To call action "action1" or "action2" you must put parameter in your request whith name "method:action1" or "method:action2".
- Call default action (execute): /path_to_action/myAction.action
- Call action1: /path_to_action/myAction.action?method:action1
- Call action2: /path_to_action/myAction.action?method:action2
Your can change default method:
<action name="myAction" class="MyAction" method="action1">
<result>showMe.jsp</result>
<result name="input">showMe.jsp</result>
</action>
So, when your call /path_to_action/myAction.action, action1 will be executed.