繰り返し
今日は繰り返しを学ぶ。
例のごとく、SAStruts本家のサイトは当てにしないφ(・ω・ )
ソース
index.jsp
<%@page pageEncoding="UTF-8"%> <html> <head> <title>Tutorial: Foreach</title> <link rel="stylesheet" type="text/css" href="${f:url('/css/sa.css')}" /> </head> <body> <h1>Tutorial: Foreach</h1> <table border="1"> <c:forEach var="m" varStatus="s" items="${mapItems}"> <tr style="background-color:${s.index % 2 == 0 ? 'pink' : 'yellow'}"> <td>${f:h(m.id)}</td> <td>${f:h(m.name)}</td> <td><s:link href="result/${m.id}">結果ページへ</s:link></td> </tr> </c:forEach> </table> </body> </html>
result.jsp
<%@page pageEncoding="UTF-8"%> <html> <head> <title>Tutorial: Foreach result</title> <link rel="stylesheet" type="text/css" href="${f:url('/css/sa.css')}" /> </head> <body> <h1>Tutorial: Foreach result</h1> id: ${f:h(id)} </body> </html>
ForeachAction.java
package tutorial.action; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.seasar.struts.annotation.ActionForm; import org.seasar.struts.annotation.Execute; import tutorial.form.ForeachForm; public class ForeachAction { public List<Map<String, Object>> mapItems = new ArrayList<Map<String, Object>>(); @ActionForm @Resource protected ForeachForm foreachForm; @Execute(validator = false) public String index() { for (int i = 0; i < 10; i++) { Map<String, Object> m = new HashMap<String, Object>(); m.put("id", i); m.put("name", "name" + i); mapItems.add(m); } return "index.jsp"; } @Execute(validator = false, urlPattern = "result/{id}") public String result() { return "result.jsp"; } }
ForeachForm.java
package tutorial.form; public class ForeachForm { public String id; }
ポイント
・c:forEach
・s:link
・@Execute urlPattern属性
c:forEach
struts.wasureppoi.com
なるほどね、jspでfor文のように使う場合のタグなのね(*´ω`*)
s:link
SAStruts/JSP [俺の基地]
まぁ、これは容易に想像がつくけど、確認できて良かった。
コンテキストパスからの指定が可能なのね。
@Execute urlPattern属性
SAStruts/Action/URLとのマッピング [俺の基地]
そっか!普段はinputタグのname属性次第で、actionの中の動作するメソッドが決まるけど、urlPatternを指定することで、特定のURLを任意のメソッドで処理できるわけね。