在spring-boot-jdbc

时间:2016-04-11 10:41:39

标签: spring-boot

我正在使用spring boot web安全jdbc示例 spring-boot-sample-web-secure-jdbc 并希望修改它以加载静态html文件(不需要像thymeleaf或jsp那样的模板)。我已经尝试将html文件放在src / main / resources下,但它们没有加载。

我注意到示例 spring-boot-sample-web-static 中的静态文件示例为主类继承SpringBootServletInitializer,而web安全jdbc使用WebMvcConfigurerAdapter。我想可能需要更多配置才能使它工作。下面是我的主干树:

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

将资源移动到src / main / resources / resources之后,直接url load工作正常但是通过请求url映射如下所示:

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

和日志片段如下:

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

此致

米滕。

1 个答案:

答案 0 :(得分:1)

根资源目录src/main/resources/未公开显示。如this post中所述,有4个目录由Spring Boot自动加载以提供静态内容:

  1. /公共/
  2. /静态/
  3. /资源/
  4. / META-INF /资源/
  5. 在您的情况下,您可以将HTML和JS文件放在里面,例如/public目录:

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

    然后,您可以使用应用程序的地址访问文件,默认配置为:

    localhost:8080/backupshell.html
    

    请注意,第3个选项/resource/并不意味着公开src/main/resources/。这是src/main/resources/resources/目录,感觉非常麻烦。