0

I'm trying to add Bootstrap to an existing Jersey 2/JSP sample project, using this tutorial as a guide. However, in Chrome console I keep getting 404 errors for Bootstrap's CSS, and the styles won't be loaded.

Error in Google console

I tried adding a property in my ResourceConfig:

property(ServletProperties.FILTER_STATIC_CONTENT_REGEX,
            // "/(images|css)/.*");
            "/css/*")

to no avail.

Here's my custom ResourceConfig

package com.xxx.jersey2.config;

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
import org.glassfish.jersey.servlet.ServletProperties;

public class CustomResourceConfig extends ResourceConfig {
    public CustomResourceConfig() {
        packages("com.xxx");
        register(LoggingFilter.class);
        register(JspMvcFeature.class);
        // it may be also jersey.config.server.mvc.templateBasePath
        // property("jersey.config.server.mvc.templateBasePath", "/WEB-INF/jsp");
        property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
        // CSS and JS
        // property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "/(static|(WEB-INF/css))/");
        property(ServletProperties.FILTER_STATIC_CONTENT_REGEX,
                // "/(images|css)/.*");
                "/css/*");
    }
}

My web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <!--<display-name>Archetype Created Web Application</display-name>-->

  <filter>
    <filter-name>jersey</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.xxx.sample-jersey2</param-value>
    </init-param>
    -->
    <init-param>
      <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.xxx.jersey2.config.CustomResourceConfig</param-value>
    </init-param>
  </filter>

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

  <!--<welcome-file-list>-->
    <!--<welcome-file>index.html</welcome-file>-->
    <!--<welcome-file>index.htm</welcome-file>-->
    <!--<welcome-file>movies.jsp</welcome-file>-->
  <!--</welcome-file-list>-->

</web-app>

My JSP

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Sandbox application</title>
    <link rel="stylesheet" href="../../css/bootstrap.min.css">
    <!-- <script src="../js/bootstrap.min.js"></script> -->

    <!-- Latest compiled and minified CSS -->
    <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> -->

    <!-- Optional theme -->
    <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous"> -->

    <!-- Latest compiled and minified JavaScript -->
    <!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> -->
</head>
<body>
<h2>Listing Movies</h2>
    <table class="table table-striped">
        <tr>
            <th>Name</th>
            <th>Starring</th>
        </tr>
        <c:forEach var="item" items="${it.movies}">
            <tr>
                <td>${item.name}</td>
                <td>${item.starring}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

And finally, the project structure

Project structure

Any ideas on this would be greatly appreciated.
Thanks

jmm
  • 1,044
  • 2
  • 12
  • 38

2 Answers2

0

line 7 of your list.jsp should be

<link rel="stylesheet" href="../css/bootstrap.min.css">

instead of

<link rel="stylesheet" href="../../css/bootstrap.min.css">

I think all paths are relative to WEB-INF, because WEB-INF and the bootstrap resources are in the same parent folder you only need to go up one level before back down into /css or /js

0

<link rel="stylesheet" href="../css/bootstrap.min.css"> and

<link rel="stylesheet" href="../../css/bootstrap.min.css">

instead of

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">

and java file "property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "/(images|js|css)/.*");"

i am also faced same issue, now resolved. it should work

Praveen M
  • 1
  • 1