いろんなJava Webフレームワークで同じ画面を作ってみる(T2framework編)
いろんなJava Webフレームワークで同じ画面を作ってみる(T2framework編)です。
お題は、こちら。
追加で、T2farameworkとPlay!をやってみたいと思います。
- 公式サイト: t-2 - The WEB Connector -simple annotation based web framework for real developer- - Google Project Hosting
- バージョン: 0.6.3-ga
最初、T2はバリデーションの機能を持っていなかったので外してましたが、コメント頂きましたので作ってみました。
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>t2</filter-name> <filter-class>org.t2framework.t2.filter.T2Filter</filter-class> <init-param> <param-name>t2.rootpackage</param-name> <param-value>webapp.page</param-value> </init-param> </filter> <filter-mapping> <filter-name>t2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
t2.rootpackage でベースとなるパッケージを指定します。
AddPage.java
そしてページクラスです。
package webapp.page; import java.util.List; import java.util.Map; import org.t2framework.commons.util.StringUtil; import org.t2framework.t2.annotation.core.ActionParam; import org.t2framework.t2.annotation.core.Default; import org.t2framework.t2.annotation.core.Page; import org.t2framework.t2.annotation.core.RequestParam; import org.t2framework.t2.contexts.Request; import org.t2framework.t2.navigation.Forward; import org.t2framework.t2.spi.Navigation; import static org.t2framework.commons.util.CollectionsUtil.*; @Page("add") public class AddPage { @Default public Navigation index() { return Forward.to("/WEB-INF/jsp/input.jsp"); } @ActionParam public Navigation calculate(Request request, @RequestParam("arg1") String arg1, @RequestParam("arg2") String arg2) { Map<String, Object> message = newHashMap(); List<String> all = newArrayList(); if (StringUtil.isBlank(arg1)) { message.put("arg1", "必須です"); all.add("arg1は必須です"); } else if (isNotInteger(arg1)) { message.put("arg1", "整数で入力してください"); all.add("arg1は整数で入力してください"); } if (StringUtil.isBlank(arg2)) { message.put("arg2", "必須です"); all.add("arg1は必須です"); } else if (isNotInteger(arg2)) { message.put("arg2", "整数で入力してください"); all.add("arg2は整数で入力してください"); } if (!message.isEmpty()) { message.put("all", all); request.setAttribute("message", message); return Forward.to("/WEB-INF/jsp/input.jsp"); } request.setAttribute("result", Integer.parseInt(arg1) + Integer.parseInt(arg2)); return Forward.to("/WEB-INF/jsp/result.jsp"); } private boolean isNotInteger(String string) { try { Integer.parseInt(string); } catch (NumberFormatException e) { return true; } return false; } }
最初、FormアノテーションとErrorInfoクラスを使って作っていたのですが、バリデーション周りがまとまらなくなってきたので、RequestParamアノテーションを使うパターンに変更しました。RequestParamアノテーションは、valueが必須です。Play!だとjavassistで引数名を拾ってくれるらしいですが。
Pageアノテーションは、valueを省略するとデフォルトでクラス名を使ってくれます。ActionParamアノテーションもvalueを省略するとメソッド名を使ってくれます。
newHashMap, newArrayListとか地味だけどステキです。ジェネリクスが複雑になったときもスッキリ書けます。
input.jsp
<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="t" uri="http://www.t2framework.org/web/t2/functions"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>input</title> </head> <body> <c:forEach var="v" items="${message.all}"> ${t:out(v)}<br/> </c:forEach> <form> <input type="text" name="arg1" value="${t:out(arg1)}" />${t:out(message.arg1)}<br/> + <br/> <input type="text" name="arg2" value="${t:out(arg2)}" />${t:out(message.arg2)}<br/> <input type="submit" name="calculate" /><br/> </form> </body> </html>
ELファンクションが提供されています。
result.jsp
<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="t" uri="http://www.t2framework.org/web/t2/functions"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>result</title> </head> <body> result: ${t:out(result)} </body> </html>
たぶんこのコードだとT2の良さが伝わらない感じがしますが、AjaxやFlexと組み合わせると良さがでるかと思います。
以上