Seasar2勉強中 - SAStrutsで画面遷移

とりあえず、ブランクの環境で画面遷移できるところまでやってみたのでメモ。
環境はチュートリアルを試した環境と同じ。なので、プラグインやらTomcatの設定ファイルの編集はやってある前提。




1、ダウンロード
ブランクをダウンロード。sa-struts-blank-1.0.3-rc1.zip。解凍。

2、ブランクをインポート
チュートリアル同じやり方

プロジェクトsa-struts-blankを右クリックし、「Tomcat プロジェクト」→「コンテキスト定義を更新」を実行しておく。

3、convention.diconを編集
convention.diconは、ルートパッケージ名を指定するものであるらしい。src/main/resourcesにあるので、今回はaddRootPackageNameの値をcyberarchitectに変更。



<components>
<component class="org.seasar.framework.convention.impl.NamingConventionImpl">
<initMethod name="addRootPackageName">
<arg>"cyberarchitect"</arg>
</initMethod>
</component>
<component class="org.seasar.framework.convention.impl.PersistenceConventionImpl"/>
</components>

4、パッケージを作成
機能リファレンスによれば、SAStrutsではアクションをルートパッケージ.actionに、フォームをルートパッケージ.formに、エンティティをルートパッケージ.entityに格納するらしい。
とりあえず、以下のパッケージを作成。


cyberarchitect.action
cyberarchitect.form

5、クラスを作る
フォームとアクションをつくる。
http://host/project/xxx/のアクションは、XxxActionという名称になり、XxxActionのフォームは、XxxFormにする必要があるらしい。
今回はhttp://localhost:8080/sa-struts-blank/hello/としたいので、アクションはHelloAction、フォームはHelloForm。



package cyberarchitect.form;

public class HelloForm {

public String name;

}




package cyberarchitect.action;

import javax.annotation.Resource;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import cyberarchitect.form.HelloForm;

public class HelloAction {

@ActionForm
@Resource
protected HelloForm helloForm;

@Execute(validator = false)
public String index() {
return "input.jsp";
}

@Execute(validator = false)
public String submit() {
return "result.jsp";
}

}



6、ビューをつくる
機能リファレンスによれば、JSPはアクションに対応するディレクトリに格納すると吉とのこと。WEB-INF/view/hello/の下にビューを作成。


input.jsp

<%@ page pageEncoding="UTF-8"%>
<html>
<head>
<title>入力画面</title>
</head>
<body>
<h3>あなたの名前を入力してください</h3>
<s:form>
<html:text property="name"/>
<input type="submit" name="submit" value="送信"/>
</s:form>
</body>
</html>


result.jsp

<%@ page pageEncoding="UTF-8"%>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<h2>はいさい、 ${f:h(name)}</h2><br>
</body>
</html>


7、アクセスしてみる
Tomcat起動してなかったら起動してアクセス。
http://localhost:8080/sa-struts-blank/hello/


おお、動いた。クロスサイトスクリプティングの問題を避けるためには、f:h()を使いなさいとのこと。


大丈夫みたい。でもURLにセッションIDが入っているなあ。POSTにするにはどうしたらいいんだろうか。


とりあえず動いた。今回、struts-config.xml一切触ってない。これは慣れたらすごい便利かも。
まだhot deployの設定とか動作がよくわからないので、次はそのあたりを調べてみよう。