まずは基本。フォワードするだけ。
<action-mappings> <action path="/index" forward="/WEB-INF/jsp/index.jsp" /> </action-mappings>
と記述しておくことで、
http://hogehoge/[path]/index.do
というURLでindex.jspへアクセス可能となります。直接JSPを指定したくない場合に用いられますね。そもそもWEB-INF内のJSPには直接アクセスできないですし。
<form-beans> <form-bean name="LoginForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="userId" type="java.lang.String" /> <form-property name="pass" type="java.lang.String" /> </form-bean> </form-beans> ... <action-mappings> <action path="/login" forward="/WEB-INF/jsp/index2.jsp" name="LoginForm" /> </action-mappings>
と記述しておきます。画面(JSP)では
<html:form action="/login" focus="userId" method="post"> <div id="errmsg"><html:errors /></div> <table border="0" class="center"> <tr> <td width="20%" align="right">ユーザID</td> <td align="left"><html:text property="userId" styleClass="inputStrImeDisabled" size="40" maxlength="20" tabindex="1" /></td> </tr> <tr> <td width="20%" align="right">パスワード</td> <td align="left"><html:password property="pass" styleClass="inputStrImeDisabled" size="42" maxlength="40" tabindex="2" redisplay="false" /></td> </tr> <tr> <td><br /> </td> </tr> <tr> <td width="20%"></td> <td align="left"> <html:submit property="loginButton" value="ログイン" styleClass="fButtons" tabindex="3" /></td> </tr> </table> <html:hidden property="url" /> </html:form>
としておくことで、テキストボックス付きのformになります*1。まだ/login(つまりindex2.jsp)にフォワードするだけですが。
次はいよいよアクションをかませます。
上の定義の
<action path="/login" forward="/WEB-INF/jsp/index2.jsp" name="LoginForm" />
を
<action path="/login" type="nu.mine.kino.actions.LoginAction" name="LoginForm" > <forward name="success" path="/WEB-INF/jsp/index2.jsp" /> </action>
に変更します。すると遷移先がアクションクラスとなり、アクションクラス内で遷移先を制御することになります。たとえばアクションクラスを以下のようにしました。
package nu.mine.kino.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub System.out.println("hogehoge"); return mapping.findForward("success"); <-forward nameのsuccessにあわせてある } }
書き途中!
Strutsにはいろいろな標準Actionクラスがあります。その使い方をまとめています。 基本的に
Action | 通常のアクションです。formからhoge.doで呼ばれるAction(ビジネスロジックへのアダプタ)は基本的にはこのクラスを継承して作成します。 |
ForwardAction? | JSPをただ呼びたいときなどに、直接JSPにリンクをはるのではなく、このActionを用います。転送先ごとに(遷移させたいJSPごとに)アクションマッピングを定義しておき、論理パスでJSPを呼ぶようにします。このクラスは継承せずにそのまま使用します。 |
IncludeAction? | 勉強中。 |
DispatchAction? | リクエストパラメタによって呼ばれるメソッドを切り替えることが出来るアクションクラスです。一つのフォーム内に複数のSubmitボタンが存在するようなとき、このActionクラスをExtendsして使うことができるかなぁ。勉強中。 |
LookupDispatchAction? | http://ch.kitaguni.tv/u/1677/Struts/0000028675.html 勉強中。 |
SwitchAction? | モジュール型アプリケーションを作成したときに利用します。勉強中。 |
てのがあるみたいです。
struts-config.xml: <action-mappings> <action path="/forward" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/jsp/index.jsp"> </action> </action-mappings>
と定義しておく。んで、JSP側ではforward.doを呼ぶことでindex.jspに転送されます。JSPから直接JSPを呼ぶのではなく、必ずActionを経由することによって、プログラム同志の結合がより疎になります。またStruts1.1からモジュールの概念が導入されましたが、1.1からActionを経由しないで直接JSPを呼ぶのはNGになっているようです。
リクエストパラメタの内容で起動するメソッドを切り替えることが出来るアクションクラスです。たとえば
/hoge.do?command=fuga /hoge.do?command=foo
として、struts-config.xmlを
<action path="/hoge" type="actions.MyDispatchAction" <-このクラスは DispatchActionをextends name="hogeForm" parameter="command"> <-commandというパラメタで、メソッド名が決まります <forward name="success" path="index.jsp"/> </action>
とすると、
/hoge.do?command=fuga ではMyDispatchAction#fuga /hoge.do?command=foo ではMyDispatchAction#foo
が呼ばれる、てな具合です。ほとんど処理が同じで、一部だけ処理が違うようなビジネスロジックがあるときに、テンプレートパタンとこのアクションを用いると便利かもしれませんね。
この記事は
現在のアクセス:17622