4月 7th, 2008at 14:36

Tags: ,

Struts2.0.9で処理結果を次の画面で使用する

一覧画面に遷移するときなどは、遷移するときにデータを取得し、次に遷移して遷移先JSPで取得したデータを表示・・・という手順を踏むことが多い。

Action内でデータを取得して、次の画面でそのデータを利用する方法をメモ。

一覧画面へ遷移するリンク作成

リンクをクリックしたら一覧画面に遷移する実装とする。

 <s:url id="userListUrl" namespace="/userlist" action="userlist"></s:url> <s:a href="%{userListUrl}" >一覧画面</s:a>

UserListAction作成

データを取得するアクションを作成する。今回は5件のデータを作成して設定している。

//UserListAction.javapublic class UserListAction implements Action {

   private List<UserVO> userList;

   @Override   public String execute() throws Exception {

       AppLogger.debug("UserListAction.execute");

       //ユーザ一覧を取得(今回はダミーデータを作ってごまかす)       this.userList = new ArrayList<UserVO>();       for(int cnt = 0; cnt < 5; cnt++){           UserVO vo = new UserVO();           vo.setUserName("userName" + cnt);           vo.setPassword("password" + cnt);           this.userList.add(vo);       }

       return "success";   }

   public List<UserVO> getUserList() {       return userList;   }

   public void setUserList(List<UserVO> userList) {       this.userList = userList;   }}

struts.xmlは以下の通り。

<action name="userlist"        class="com.daipresents.struts209.action.UserListAction">  <result name="success">/WEB-INF/jsp/userlist.jsp</result>  <result name="error">/error.jsp</result></action>

結果を表示するJSP作成

userListの値を判定して表示する。

 <s:if test="%{userList != null && !userList.isEmpty()}">   <table border="1">     <tr>       <th>ユーザ名</th>       <th>パスワード</th>     </tr>

     <s:iterator value="userList">       <tr>         <td><s:property value="userName" /></td>         <td><s:property value="password" /></td>       </tr>     </s:iterator>   </table> </s:if>