Spring-Jersey: How to return static content?

Question:

  • How do I show my css/

    , images/

    , js/

    and other static files?
  • How do I return a JSP page to my controller (not a String method) for my index view?

Problems:

  • In attempts to address question # 1, other projects use a filter jersey.config.servlet.filter.staticContentRegex

    (see stack question here ) I couldn't find dependencies that work correctly / at all with my project setup.
  • In an attempt to address question # 2, I tried to inject dependencies to use Viewable

    . Problem - transitive dependencies negatively impact webapp usage when using appropriate classes for Spring and Jersey (snowballs in a rabbit hole of foggy bugs).

Full Project > Github Project


Dependencies > Fill POM File

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>

      

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.component.ResourceRegister</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

      

Controller > SpringController.java

@Path("/")
@Component
public class SpringController {

@GET
@Produces(MediaType.TEXT_HTML)
public ?? getIndex() {

    //How do I return my index page??
    return ??
 }
}

      

+3


source to share


1 answer


"How can I open my css /, images /, js / and other static files?"

The property jersey.config.servlet.filter.staticContentRegex

should work, but you need to specify all possible patterns for all types of files you want to serve. An easier way is to just use the property jersey.config.servlet.filter.forwardOn404

as mentioned in this answer . And yes with both options, you need to set up Jersey as a filter, not a servlet. So your web.xml might look like

<filter>
    <filter-name>Jersey</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.component.ResourceRegister</param-value>
    </init-param>
    <!-- pass to next filter if Jersey/App returns 404 -->
    <init-param>
        <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

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

      

"How do I return a JSP page in my controller (not a String method) for my index view?"

The first thing you need is the jsp mvc dependency

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-mvc-jsp</artifactId>
    <version>2.25</version>
</dependency>

      

Then you need to register the function

public ResourceRegister () {
    ...
    register(JspMvcFeature.class);
}

      

Then you need to provide the base template path for all your jsp pages. For example, I used/WEB-INF/jsp

public ResourceRegister () {
    ...
    property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
}

      



So, for this case all jsp files should be in the directory /WEB-INF/jsp

.

Jsp and controller example

index.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>JSP Page</title>
        <link href="css/styles.css" rel="stylesheet">
    </head>
    <body>
        <h1>${it.hello} ${it.world}</h1>
    </body>
</html>

      

controller

@Path("/")
public class HomeController {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable index() {
        Map<String, String> model = new HashMap<>();
        model.put("hello", "Hello");
        model.put("world", "World");
        return new Viewable("/index", model);
    }
}

public ResourceRegister () {
    ...
    register(HomeController.class);
}

      

Here /index

at Viewable

indicates index.jsp

(we don't need an extension .jsp

). Jersey knows how to find the file from the property we set above.

See also:

+14


source







All Articles