2013-10-18 18 views
6

Sto provando a configurare una semplice applicazione con Jersey 2.3 che serve una pagina jsp in un Tomcat indipendente. Ho provato un sacco di howto dal web ma la maggior parte di loro sta spiegando usando Jersey con Grizzly e non Tomcat. Quindi non sono riuscito a trovare una soluzione/spiegazione per il mio problema, perché il jsp non è servito dalla mia applicazione. Qualcuno ha un'idea di cosa c'è che non va o manca qui? Si prega di trovare sotto la mia domanda.Jersey con modelli MVC e 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> 

risposta

6

Stai usando vecchia proprietà (com.sun.jersey.config.property.JSPTemplatesBasePath) nome per base-path. Provare a utilizzare una nuova

jersey.config.server.mvc.templateBasePath.jsp 

(vedi immobili a JspMvcFeature e MvcFeature).

L'altra proprietà (com.sun.jersey.config.property.WebPageContentRegex) non è supportata in Jersey 2.x al momento.

+0

Questo link è morto. Il nuovo è https://jersey.java.net/nonav/apidocs/latest/jersey/ –

+0

Grazie, risolto anche nella risposta. –

2

Ecco alcuni init filtro Jersey params si potrebbe desiderare di dare un'occhiata a (sto usando Jersey 2.5 all'interno Tomcat 7 - il resto della mia web.xml sembra simile al tuo):

<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> 

Il parametro JspMvcFeature potrebbe essere utile nella tua situazione. Puoi anche vedere la configurazione del contenuto statico e la traccia che dovresti assolutamente trovare utile ad un certo punto.

+0

Sto cercando di restituire un JSP dal mio servizio ReST, ma dopo aver eseguito tutta la configurazione in 'web.xml', non riesco a ottenere un JSP nella risposta. Dopo aver colpito l'URL, ho potuto vedere solo '{templateName": "/ TerstJSP", "model": [], "resolvingClass": null, "templateNameAbsolute": true} 'come risposta. –