Miten
Miten

Reputation: 358

static file not loading in spring boot for web-secure-jdbc

I am using spring boot web secure jdbc sample spring-boot-sample-web-secure-jdbc and want to modify it to load static html files (no template like thymeleaf or jsp needed). I have tried putting the html files under src/main/resources but they are not loading.

I noticed that static file example in samples spring-boot-sample-web-static inherits SpringBootServletInitializer for main class while web secure jdbc use WebMvcConfigurerAdapter. I suppose there might be some more configuration needed to make it work them. Below is my dir tree:

src/main/java/hello/MyController.java
src/main/resources/backupshell.html
src/main/resources/myscripts.js

After moving resources to src/main/resources/resources direct url load works fine but via request url mapping as below is not loading:

 @RequestMapping("/admin")
    //@ResponseBody
    String admin() {
        //return "Hello World admin!";
        return "backupshell";
    }

and logs snippet is as below:

2016-04-18 16:16:51.716 DEBUG 8896 --- [nio-8081-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/backupshell] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@7a9c84a5]]] and 1 interceptor
2016-04-18 16:16:51.716 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/backupshell] is: -1
2016-04-18 16:16:51.717 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2016-04-18 16:16:51.717 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2016-04-18 16:16:51.718 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2016-04-18 16:16:51.718 DEBUG 8896 --- [nio-8081-exec-9] o.s.b.c.web.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@24880cb8
2016-04-18 16:16:51.718 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2016-04-18 16:16:51.720 DEBUG 8896 --- [nio-8081-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error

Regards,

Miten.

Upvotes: 0

Views: 671

Answers (1)

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

The root resource directory src/main/resources/ isn't exposed publicly. As described in this post there are 4 directories which are automatically loaded by Spring Boot to serve static content:

  1. /public/
  2. /static/
  3. /resources/
  4. /META-INF/resources/

In your case you can put the HTML and the JS file inside ,e.g., the /public directory:

src/main/resources/public/backupshell.html
src/main/resources/public/myscripts.js

Then you can access the files with the address of your application, with default configuration it would be:

localhost:8080/backupshell.html

Note that 3rd option /resource/ doesn't mean that src/main/resources/ is exposed. It's the src/main/resources/resources/ directory, which can feel quite cumbersome.

Upvotes: 1

Related Questions