SAStruts
基本形
sastruts_test.zip(984)
HelloWorld
ルートパッケージ名の設定
WEB-INF/classes/convention.dicon
の
<initMethod name="addRootPackageName"> <arg>"hogehoge"</arg> </initMethod>
でルートパッケージ名を設定
プログラムの配置
以下赤字のファイル名、パス名を合わせることでアクション、フォーム、ビューが結びつく。
アクションクラス
WEB-INF/src/ルートパッケージ/action/HelloWorldAction.java
フォームクラス
WEB-INF/src/ルートパッケージ/form/HelloWorldForm.java
ビュー
WEB-INF/view/helloWorld/hoge.jsp
※helloWorld の最初の1文字が小文字になることに注意!
ソース
アクションクラス
1 |
package hogehoge.action;
import java.util.*;
import javax.annotation.Resource;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import hogehoge.form.HelloWorldForm;
public class HelloWorldAction {
public Integer result;
@ActionForm
@Resource
protected HelloWorldForm helloWorldForm;
@Execute(validator = false)
public String index() {
return "hoge.jsp";
}
} |
フォームクラス
1 |
package hogehoge.form;
import org.seasar.struts.annotation.IntegerType;
import org.seasar.struts.annotation.Required;
import java.util.*;
import org.seasar.framework.beans.util.BeanMap;
public class HelloWorldForm {
@Required
public String hello;
} |
ビュー
helloWorld/hoge.jsp
1 |
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<title>HelloWorld</title>
<link rel="stylesheet" type="text/css" href="${f:url('/css/sa.css')}" />
</head>
<body>
<h1>HelloWorld</h1>
<html:errors/>
<s:form>
<html:text property="hello"/>
= ${f:h(hello)}<br />
<input type="submit" name="index" value="サブミット"/>
</s:form>
</body>
</html> |
注意
IllegalAutoBindingPropertyRuntimeException
が出る場合は、クラス名とバインドする変数の名前があっていないことがある。
HelloWorldForm helloWorldForm;
のように、クラス名の最初の1文字を小文字にした変数名を使う。
デプロイの種類と設定
デプロイの設定は
env.txt
で行う。
デフォルトは、ct。
| 種類 | 動作 | 用途 |
|---|---|---|
| ct | リクエストのたびに、必要なコンポーネント「だけ」再デプロイ | 結合テスト |
| ut | warm deploy。アプリの変更をhotには認識せず、且つアプリ起動時に全デプロイも行わない。 | 単体テスト |
| it | cool deploy。アプリ起動時に全デプロイを完了する | 本番 |
| env.txtなし | it と同じ。 | itと同じ |
ログ
http://www.nurs.or.jp/~sug/soft/log4j/log4j2.htm
http://sastruts.seasar.org/fileReference.html
WEB-INF/classes/log4j.properties ファイルでログの出力を決める。
log4j.category.org.seasar=DEBUG, C log4j.additivity.org.seasar=false log4j.category.tutorial=DEBUG, C log4j.additivity.tutorial=false
テスト用は DEBUG になっている。
リリース時は、
warn
が無難かな。
本番と開発環境
本番と開発環境で以下の設定が違うはずなので気をつける
| 設定 | 開発環境 | 本番環境 |
|---|---|---|
| デプロイの種類 | ct、ut | it |
| ログ | DEBUG | WARN |
| DB接続先 | テスト環境 | 本番環境 |
tomcat起動時に一回だけ処理をする
http://treeapps.hatenablog.com/entry/20110831/p1
http://blog.mikuriya.biz/archives/171
app.diconに実行するクラスを追記する
<!-- tomcat起動後の初期処理 --> <component name="initService" class="treeshop.s2.init.InitService"> <initMethod name="init"> </initMethod> </component>
1 |
public class InitService {
public void init(){
}
} |
引数が欲しい場合は
<!-- tomcat起動後の初期処理 -->
<component name="initService" class="treeshop.s2.init.InitService">
<initMethod name="init">
<arg>"/home/hoge/sample/property/sys.properties"</arg>
</initMethod>
</component>
1 |
public class InitService {
public void init(String propertyFile){
}
} |
ファイルをダウンロードさせる
パターン1
http://piyopiyocs.blog115.fc2.com/blog-entry-231.html
1 |
public String download() {
try {
ResponseUtil.download(new String(
"サンプル.txt".getBytes("Shift_JIS"),
"ISO-8859-1"), "こんにちは".getBytes("Shift_JIS"));
} catch (IOException e) {
throw new IORuntimeException(e);
}
return null;
} |
パターン2
http://d.hatena.ne.jp/ooharak/20080911/1221144337
ResponseUtil.download(String,InputStream[,length])を使う
1 |
@Execute(validator = false)
public String download() {
File file = new File("c:/temp/large.txt");
try {
ResponseUtil.download("hello.txt",
new BufferedInputStream(
new FileInputStream(file)), (int)file.length);
//ダウンロード中のエラーは
} catch (IORuntimeException ire) {
//あえて無視
} catch (IOException e) {
throw new IORuntimeException(e);
}
return null;
} |
パターン3
http://piyopiyocs.blog115.fc2.com/blog-entry-231.html
1 |
/** HTTPレスポンス */
@Resource
protected HttpServletResponse response;
(省略)
/**
* CSVファイル出力
*
*/
private void ouputCsv(){
try{
//---------------------------
// HTTPヘッダ出力
//---------------------------
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment; filename="hoge.csv");
//---------------------------
// CSV出力
//---------------------------
PrintWriter out = new PrintWriter(
new OutputStreamWriter(response.getOutputStream(),
"Shift_JIS"));
for(i=0; i < 100000; i++) {
out.println("a,b,c," + i);
}
out.close();
} catch (IOException e) {
throw new IORuntimeException(e);
} |
[カテゴリ: プログラミング言語 > Servlet & Struts]
[通知用URL]
Tweet
最終更新時間:2013年06月02日 23時12分22秒