Application Web ne servant pas de ressources statiques à l'aide de chaussures de ressort et de tomcat
-
20-12-2019 - |
Question
Bonjour, j'ai des problèmes de "servir" des ressources avec mon serveur Web, tandis que certaines de mes autres ressources sont "servant".(Graphiques.js est chargé)
dans ma console de navigateur:
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]
Mais ces fichiers sont situés dans mon répertoire en tant que tel:
css/
bootstrap.min.css
includes/
header.html
js/
app.js
controllers/
graphs.js
Lorsque mon serveur démarre:
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)
Et mon HTML est comme:
<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">
La solution
Vous n'avez pas indiqué si vous avez suivi des instructions pour l'emballage en tant que guerre au lieu d'un bocal et de grouper Tomcat. Cela change les choses un peu.De toute façon, jetez un coup d'œil à la documentation ici: http://docs.spring.io/spring-boot/docs/current-snapshot/reference/htmlsingle/#boot-features-spring-mvc-static-content
Bascally, hors de la boîte, vous pouvez placer du contenu statique dans l'un des emplacements mentionnés: / statique, / public, / ressources ou / méta-inf / résorces.En ce qui concerne l'emplacement de ces dossiers, bien soit à la racine du contexte de servlet, soit sur la classe de classe.
Vous pouvez trouver une application Tomcat très basique ici ici: https://github.com/MmeANY / Spring-Boot-Web-MVC qui fait le travail.