Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot static content with context path

I'm having problems with static content in Spring Boot when using a context path. i.e.: I wish to deploy my app to localhost:8080/{appname}/

When I run the app without context path everything works fine and Spring Boot finds and runs my .html files (from resources/templates/, I'm using Thymeleaf) and JS files (from resources/static/js/) but when I add the context path with either:

server.context-path=/{appname}

OR

server.servlet-path=/{appname}

then the .html pages are still displayed by the JS files generate 404 errors.

I have tried to change the spring.resources.static-locations in the application.properties and overriding addResourceHandlers() method in my MvcConfig.class but neither seem to work

I use a MvcConfig class because I need to define a CookieLocaleResolver and a MessageSource but that is all I have in the MvcConfig. I do not use @EnableWebMvc and just have the @SpringBootApplication annotation with a @ComponentScan.

Any help would be appreciated :)

like image 404
svg Avatar asked Aug 05 '16 11:08

svg


People also ask

What is the use of context path in spring boot?

Simply put, the context path is a name with which a web application is accessed. It is the root of the application. By default, Spring Boot serves the content on the root context path (“/”).

Where do static files go in spring boot?

Using Spring Boot Spring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.


2 Answers

As per your comment:

the HTML is referencing the JS without the app context

the problem is not in Spring serving the js, its that the page is not creating the URL to the resources correctly.

Thymeleaf provides a mechanism to support this automatically by just marking the src attribute with the th prefix.

See section 2 "Context-relative URLs": www.thymeleaf.org/doc/articles/standardurlsyntax.html

like image 139
rhinds Avatar answered Sep 29 '22 02:09

rhinds


Below is an example shows how to configure static resources in spring boot.

enter image description here

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**/*.js").addResourceLocations("/ui/static/");
        registry.addResourceHandler("/**/*.css").addResourceLocations("/ui/static/");
    }
}

Path patterns

Add a resource handler for serving static resources based on the specified URL path patterns. The handler will be invoked for every incoming request that matches to one of the specified path patterns.

Patterns like "/static/" or "/css/{filename:\w+\.css}"} are allowed. See **org.springframework.util.AntPathMatcher for more details on the syntax.

your jsp/html looks refer static content as in below

<link href="/webAppContext/cssa/bootstrap.min.css" rel="stylesheet"/>
<script src="/webAppContext/jquery-2.2.1.min.js"></script>
<script src="/webAppContext/bootstrap.min.js"></script>

urls using which browser tries to get static content

http://localhost:8080/webAppContext/jquery-2.2.1.min.js
http://localhost:8080/webAppContext/bootstrap.min.js
http://localhost:8080/webAppContext/cssa/bootstrap.min.css


server.servlet.context-path=/webAppContext is in your application.properties
like image 41
Bhukailas Avatar answered Sep 29 '22 01:09

Bhukailas