Spring Boot static content with context path

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


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