Web App不使用Spring Boot和Tomcat提供静态资源

时间:2014-07-28 17:06:16

标签: java spring tomcat tomcat7 spring-boot

您好我的网络服务器“服务”资源有问题,而我的其他一些资源都在“服务”。 (graphs.js正在加载)

在我的浏览器控制台中:

GET http://localhost:8080/js/app.js [HTTP/1.1 400 Bad Request 2ms]
GET http://localhost:8080/includes/header.html [HTTP/1.1 400 Bad Request 4ms]
GET http://localhost:8080/cs/bootstrap.min.css [HTTP/1.1 400 Bad Request 3ms]

但是这些文件位于我的目录中:

css/
    bootstrap.min.css
includes/
    header.html
js/
    app.js
    controllers/
        graphs.js

当我的服务器启动时:

2014-07-28 08:25:06.748  INFO 5260 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-07-28 08:25:06.939  INFO 5260 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2014-07-28 08:25:06.940  INFO 5260 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-07-28 08:25:07.017  INFO 5260 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-07-28 08:25:07.017  INFO 5260 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1146 ms
2014-07-28 08:25:07.346  INFO 5260 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
**mapping**
2014-07-28 08:25:07.903  INFO 5260 --- [           main] o.s.w.s.c.a.WebMvcConfigurerAdapter      : Adding welcome page: jndi:/localhost/index.html
2014-07-28 08:25:07.905  INFO 5260 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2014-07-28 08:25:07.912  INFO 5260 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-07-28 08:25:07.912  INFO 5260 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-07-28 08:25:08.023  INFO 5260 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-07-28 08:25:08.147  INFO 5260 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-07-28 08:25:08.149  INFO 5260 --- [           main] c.c.i.qualifier.datacentral.Application  : Started Application in 2.556 seconds (JVM running for 2.783)

我的HTML就像:

<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers/graphs.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

1 个答案:

答案 0 :(得分:2)

你还没有说过你是否已经按照包装的说明而不是JAR并将Tomcat捆绑在一起。这确实改变了一些事情。无论哪种方式,请查看此处的文档:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-static-content

基本上,开箱即用,您可以将静态内容放在其中一个位置:/ static,/ public,/ resources或/ META-INF / resorces。至于这些文件夹的位置,要么位于servlet上下文的根目录,要么位于类路径上。

你可以在这里找到一个非常基本的Tomcat应用程序:https://github.com/mmeany/spring-boot-web-mvc来完成这项工作。