※自分用の備忘録
前提条件
・j2sdk1.4系がインストール済み
・tomcat4系がインストール済み
・eclipse3系がインストール済み
1.tomcatプロジェクトの作成
・ファイル→新規→プロジェクト→tomcatプロジェクトを作成
プロジェクト名:struts_test
パッケージ名:hoge
※
EclipseでHello Struts
を参考にstruts-blankを利用してstruts.jarなどをライブラリパスに通しておく
2.struts-config.xmlの編集
<form-beans>~</form-beans>の間にActionFormのエントリーを追加する
<form-beans>
<form-bean name="helloForm" type="hoge.HelloForm" />
</form-beans>
<action-mappings>~</action-mappings>の間にActionクラスのエントリーを追加する
<action-mappings>
<action path="/hello" type="hoge.HelloAction" name="helloForm">
<forward name="success" path="/hello.jsp"/>
</action>
</action-mappings>
3.Actionクラスの作成
まず、パッケージhogeをつくり、その下にActionクラスを作成
HelloAction.java
package hoge;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class HelloAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request, HttpServletResponse response) {
HelloForm helloForm = (HelloForm)form;
String name = helloForm.getName();
System.out.println("name:" + name);
// デフォルトのsessionスコープを使っているのでホントは不要
//request.setAttribute("name",name);
return mapping.findForward("success");
}
}
4.ActionFormクラスの作成
次に、ActionFormクラスを次のように作成
HelloForm.java
package hoge;
import org.apache.struts.action.ActionForm;
public class HelloForm extends ActionForm {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5.jspの作成
jspを次のように作成する。
hello.jsp;
<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html>
<body>
<bean:write name="helloForm" property="name"/>
</body>
</html>
6.動作確認
http://localhost:8080/struts_test/hello.do?name=aiueo をブラウザから
リクエストし表示できればOK