Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey with MVC Templates and Tomcat

I'm trying to set up an simple application with Jersey 2.3 serving a jsp page in a standalone Tomcat. I've tried a lot of howto's from the web but the most of them are explaining using Jersey with Grizzly and not Tomcat. So I couldn't find a solution / explanation for my problem why the jsp is not served by my application. Does somebody has an idea what's wrong or missing here? Please find below my application.

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>
like image 584
ddankovics Avatar asked Oct 18 '13 20:10

ddankovics


2 Answers

You're using old property (com.sun.jersey.config.property.JSPTemplatesBasePath) name for base-path. Try using new one

jersey.config.server.mvc.templateBasePath.jsp

(see properties in JspMvcFeature and MvcFeature).

The other property (com.sun.jersey.config.property.WebPageContentRegex) is not supported in Jersey 2.x at the moment.

like image 139
Michal Gajdos Avatar answered Oct 24 '22 08:10

Michal Gajdos


Here are some Jersey filter init params you might want to take a look at (I'm using Jersey 2.5 inside Tomcat 7 - the rest of my web.xml looks similar to yours):

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

The JspMvcFeature param might be useful in your situation. You can also see the static content configuration as well as tracing which you should definitely find useful at some point.

like image 32
Russ Jackson Avatar answered Oct 24 '22 08:10

Russ Jackson