2013-10-18 40 views
6

Jersey 2.3を使用してスタンドアロンTomcatでJSPページを提供する単純なアプリケーションをセットアップしようとしています。私はウェブからたくさんのハウツーを試しましたが、それらの大半はTomcatではなくGrizzlyでジャージーを使って説明しています。だから私はなぜ私のアプリケーションでJSPが提供されていない私の問題の解決策/説明を見つけることができませんでした。誰かがここで何が間違っているのか考えていませんか?私のアプリケーションの下で見つけてください。MVCテンプレートとTomcatを使用したジャージー

のpom.xml

... 
<dependencies> 
    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-servlet-core</artifactId> 
     <version>2.3</version> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish.jersey.ext</groupId> 
     <artifactId>jersey-mvc-jsp</artifactId> 
     <version>2.3.1</version> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.tomcat.maven</groupId> 
      <artifactId>tomcat7-maven-plugin</artifactId> 
      <version>2.0</version> 
      <executions> 
       <execution> 
        <id>tomcat-run</id> 
        <goals> 
         <goal>exec-war-only</goal> 
        </goals> 
        <phase>package</phase> 
        <configuration> 
         <path>/jhello</path> 
         <enableNaming>false</enableNaming> 
         <finalName>jhello-standalone.jar</finalName> 
         <charset>utf-8</charset> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

web.xmlの

<filter> 
    <filter-name>jhello</filter-name> 
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>com.granatasoft.playground.jersey</param-value> 
    </init-param> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name> 
     <param-value>/WEB-INF/views</param-value> 
    </init-param> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name> 
     <param-value>/(decorators|scripts|styles|resources|(WEB-INF/views))/.*</param-value> 
    </init-param> 
</filter> 

<filter-mapping> 
    <filter-name>jhello</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

HelloJersey.java

package com.granatasoft.playground.jersey; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

import org.glassfish.jersey.server.mvc.Viewable; 

@Path("/hello") 
public class HelloJersey { 
@GET 
@Produces(MediaType.APPLICATION_JSON) 
public String sayJsonHello() { 
    return "{'hello': 'jersey'}"; 
} 

@GET 
@Produces(MediaType.TEXT_HTML) 
public Viewable sayHtmlHello() { 
    return new Viewable("hello"); 
} 
} 

hello.js

<%@ 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=US-ASCII"> 
<title>Hello JSP</title> 
</head> 
<body> 
<h1>Hello JSP</h1> 
</body> 
</html> 

答えて

6

あなたは、ベースパス(com.sun.jersey.config.property.JSPTemplatesBasePath)の名​​前を古いプロパティを使用しています。新しいもの

jersey.config.server.mvc.templateBasePath.jsp 

を使用してみてください(JspMvcFeatureMvcFeatureでプロパティを参照してください)。

他のプロパティ(com.sun.jersey.config.property.WebPageContentRegex)は現在Jersey 2.xではサポートされていません。ここで

+0

リンクが無効です。新しいものはhttps://jersey.java.net/nonav/apidocs/latest/jersey/ –

+0

です。ありがとう、答えにも修正されました。 –

2

は、initは、あなたが(私はTomcatの7内部のジャージー2.5を使用しています - 私のweb.xmlの残りの部分はあなたに似ています)を見てみたいことがありますparamsは、いくつかのジャージーフィルタです:

<init-param> 
     <param-name>jersey.config.server.mvc.templateBasePath.jsp</param-name> 
     <param-value>/WEB-INF/jsp</param-value> 
    </init-param> 
    <init-param> 
     <param-name>jersey.config.server.provider.classnames</param-name> 
     <param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value> 
    </init-param> 
    <init-param> 
     <param-name>jersey.config.server.tracing</param-name> 
     <param-value>ALL</param-value> 
    </init-param> 
    <init-param> 
     <param-name>jersey.config.servlet.filter.staticContentRegex</param-name> 
     <param-value>(/index.jsp)|(/(content|(WEB-INF/jsp))/.*)</param-value> 
    </init-param> 

JspMvcFeatureパラメーターは、状況に応じて役立ちます。また、静的なコンテンツの設定やトレースを見ることもできます。

+0

私はReSTサービスからJSPを返そうとしていますが、 'web.xml'のすべての設定を行った後、応答でJSPを取得できません。 URLを打った後、私は '{" templateName ":"/TerstJSP "、" model ":[]、" resolvingClass ":null、" templateNameAbsolute ":true}'のみを応答として見ることができました。 –

関連する問題