第45夜 JAX-RSをWebアプリのフロントエンドとして利用する その2

好評だった第43夜の続き。

あの時はパラメータとして渡すためにHttpServletRequestを使った。

それを使わない方法がある。

ソースを見れば一発でわかる。第43夜と比べてほしい。

@Path("calc")
public class CalcResource {
    @Path("{a}/{b}")
    @GET
    public Viewable add(@PathParam("a")int a, @PathParam("b")int b) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("a", a);
        params.put("b", b);
        params.put("c", a + b);

        return new Viewable("/index.jsp", params);
    }
}


テンプレート側

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1>${it.a} + ${it.b} = ${it.c}</h1>
    </body>
</html>


名前として「it」が設定される。それだけ注意すればよい。
サーブレットAPIが消えてよりシンプルになった!