Question :
- How do I expose my
css/
,images/
,js/
and other static files? - How do I return a JSP page in my controller (not a String method) for my index view?
Problems :
- In efforts to solve question #1 other projects use a filter
jersey.config.servlet.filter.staticContentRegex
(as seen here Stackoverflow Question) I haven't been able to find dependencies that work correctly/at all with my project setup. - In efforts to solve question #2 I attempted to introduce dependencies to use
Viewable
. Problem - transitive dependencies adversely affect the webapp from using the appropriate classes for Spring & Jersey (snowballs into a rabbit hole of nebulous errors)
Complete Project > Github Project
Dependencies > Complete 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 ??
}
}
The
jersey.config.servlet.filter.staticContentRegex
property should work, but you need to specify all the possible patterns for all the types of files you want to serve. The easier way is to just use the propertyjersey.config.servlet.filter.forwardOn404
, as pointed out in this answer. And yes with both choices, you need configure Jersey as a filter, rather than a servlet. So your web.xml might look likeFirst thing you need is the jsp mvc dependency
Then you need to register the feature
Then you need to specify the template base path for all your jsp pages. For example I used
/WEB-INF/jsp
So for this case, all the jsp files should be located in
/WEB-INF/jsp
directory.Example jsp and controller
index.jsp
Controller
Here the
/index
in theViewable
points theindex.jsp
(we don't need the.jsp
extension). Jersey knows how to find the file from the property we set above.See Also: