やっぱ作らなきゃ、ダメ?

何だかんだで、やっぱりActionとActionFormは書かないとダメっぽいんで簡単なヤツを書くことにする。
まずはパッケージを掘って、新規作成でActionを継承させたGetAllUnitDataActionクラスを生成する。
今回はS2DAOの実験で作成したDAOを取得して表示したいというのがお題。
DAOはコンテナにAuto-wireでインジェクションして欲しいのでクラスメンバにUnitDataDaoを置き、コンストラクタを定義してみた。
んでstrutsならばexecute()をオーバーライドして処理を書くのが常套なのでメソッドのオーバーライド。
DAO経由で全データのリストを取得してきてセッションにくっつけるだけ。↓
redrisefirm.seasar.s2struts.action.GetAllUnitDataAction.java

public class GetAllUnitDataAction extends Action {

  private UnitDataDao unitDataDao = null;

  public GetAllUnitDataAction(UnitDataDao dao) {
    this.unitDataDao = dao;
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession();
    session.setAttribute("unitDataList", unitDataDao.getAllEntity(););
    return mapping.findForward("allUnitData");
  }
}

次はActionFormクラス。特にフォームデータを保持する必要もないので、何も考えなしにサクっと↓
redrisefirm.seasar.s2struts.actionForm.GetAllUnitDataActionForm.java

public class GetAllUnitDataActionForm extends ActionForm {

  public GetAllUnitDataActionForm() {
    super();
  }
}

お次は表示用のJSPページを書く。とりあえずJSPをまとめておくためにJSPページ用のフォルダを作成する。んでその中にallUnitData.jspを作成して書く。↓

<%@ page contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic" %>
<html:html>
<head><title>全ユニットリスト</title></head>
<body>
<table>
<th>ユニット名</th>
<logic:iterate id="unitData" name="unitDataList" scope="session">
<tr><td><bean:write name="unitData" property="name"/></td>></tr>
</logic:iterate>
</table>
</body>
</html:html>

ま、とりあえずなんで超絶手抜きで(^_^)

さて、Actionクラス、ActionFormクラス、JSPページを書いたらstruts-config.xmlに登録しなければいけない。具体的に今回のケースでは以下のような記述が必要になる。

<form-beans>
  <form-bean name="allUnitData" type="redrisefirm.seasar.s2struts.actionForm.GetAllUnitDataActionForm" />
</form-beans>
<action-mappings>
  <action path="/allUnitDataList" type="redrisefirm.seasar.s2struts.action.GetAllUnitDataAction" name="allUnitData"
     <forward name="allUnitData" path="/jsp/allUnitData.jsp" />
  />
</action-mappings>

Strutsの面倒くさいのはココでクラスが増えるとxml地獄になる。そこの対応は後ほどやるべし。