BLOGTIMES
2012/01/12

Jersey のレスポンスに JSP を使う

  java  programming  restful 
このエントリーをはてなブックマークに追加

昨日に続き Jersey でアプリを書いています。
ちょっとした View の部分を JSP で作りたかったので、レスポンスを JSP に Forward する方法を調べてみました。

Jersey JAX-RS (JSR-311) のメモ 来栖川電算

JSPへ処理を中継する
ViewableをEntityとして返却すればOK。
Viewableコンストラクタの第2引数には任意のオブジェクトを渡すことができ、JSPからitとして参照できる。

上記のような情報を基に、 com.sun.jersey.api.view.Viewable を返すようにしてみたのですが一筋縄ではいきませんでした。
以下、作業メモ。

返り値を Viewable に書き換える

情報に従って返り値をViewableに書き換えて、 jsp を置いてみたのですがうまく動きませんでした。
どうやら web.xml の Servlet のマッピングが /* になっているのが良くないようで jspが上手く呼び出せません。

jaxrsexample/Hello3.java

package jaxrsexample; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.sun.jersey.api.view.Viewable; @Path("/hello3") public class Hello3 { @GET @Produces(MediaType.TEXT_HTML) public Viewable sayHello() { return new Viewable("/hello.jsp", "Param"); } }

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> HelloWorld! (JSP)<br /> Param: ${it} </body> </html>

いろいろ調べてみたところ下記で Servlet としてでなく Filter として使うとこの問題が回避できるという情報を見つけたので、これを試してみたところビンゴでした。

HATEOAS by Example Zienit

Normally the Jersey servlet is configured as a servlet (what’s in a name?). However, here it is configured as a filter! Jersey intercepts all url’s under the context root (pattern /* at line 20). However, if Jersey is configured as a filter, it is possible exclude specific urls from being handled by Jersey. In effect, those url’s are regular web application url’s. In this example, I’ve specified /xsltforms and /WEB-INF/jsp to be excluded from Jersey’s handling (lines 9-12). Also, the path /WEB-INF/jsp is specified as the root of the jsp templates (lines 13-16).

web.xml を下記のように書き換えます。

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>jaxrsexample</display-name> <filter> <filter-name>Jersey</filter-name> <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class> <init-param> <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name> <param-value>/WEB-INF/jsp/.*</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name> <param-value>/WEB-INF/jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>Jersey</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

これでプログラム中の /hello.jsp というパス指定で、 WEB-INF/jsp/hello.jsp が読み込まれるようになりました。
具体的には、 http://localhost:8080/jaxrsexample/hello3 にアクセスすると下記のような html が帰ってきます。

<!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>Insert title here</title> </head> <body> HelloWorld! (JSP)<br /> Param: Param </body> </html>

ここまでできると簡単なアプリを書く分には問題なさそうです。


    トラックバックについて
    Trackback URL:
    お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
    このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/4702
    Trackbacks
    このエントリにトラックバックはありません
    Comments
    愛のあるツッコミをお気軽にどうぞ。[policy]
    古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
    コメントはありません
    Comments Form

    コメントは承認後の表示となります。
    OpenIDでログインすると、即時に公開されます。

    OpenID を使ってログインすることができます。

    Identity URL: Yahoo! JAPAN IDでログイン