Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing both REST resources and static resources with Jersey

I would like to use some static resources (images, js, css) from JSP pages served as Viewable

I don't know how to use the WebPageContentRegex

If I use servlet, I can execute resources but I can't access static files
If I use filter, I can access static files but I can't execute resources

Could you please help me to access both?

Here is my files:

webapp
  |____ resources
  |       |____ css
  |       |____ images
  |       |____ js
  |
  |____ WEB-INF
          |____ jsp

Here is my web.xml file:

<?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" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>test</display-name>

<servlet>
<servlet-name>REST</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>my packages</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
  <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
  <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
  <param-value>/WEB-INF/jsp</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
  <param-value>(/resources/(css|images|js)/.*)|(/WEB-INF/jsp/.*\.jsp)</param-value>
</init-param>
</servlet>
<servlet-mapping>
  <servlet-name>REST</servlet-name>
  <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

</webapp>
like image 335
futoshita Avatar asked Dec 12 '12 14:12

futoshita


1 Answers

can you verify that you are in jersey 1.x ? In case that your are in 2.x, you should replace : com.sun.jersey.config.property.WebPageContentRegex by jersey.config.servlet.filter.staticContentRegex

May I ask you also to simplify your regexp,in a first time to serve only *.html ? (Might be an issue with the regexp!)

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>test</display-name>

    <servlet>
        <servlet-name>REST</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>my packages</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/jsp/*.jsp</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/resources/*.html </param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>REST</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>

</webapp>
like image 56
jeorfevre Avatar answered Jan 03 '23 20:01

jeorfevre