春天开机找不到WebApp文件夹下的index.html [英] Spring boot cannot find index.html under webapp folder
问题描述
我读<一个下列文件href=\"http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content\"相对=nofollow> spring.io 和它说默认情况下春季启动将提供静态内容从一个名为/静态的(或/公共或/资源或/ META-INF目录/资源)在classpath
但是当我把我的index.html文件在 /资源
字符串首页
只是渲染。目前, index.html的
是在web应用程序,我用AngularJS。
MvcConfiguration
@Configuration
公共类MvcConfig { @豆
InternalResourceViewResolver这个视图解析器(){ 使用InternalResourceViewResolver解析器=新InternalResourceViewResolver这个();
resolver.set preFIX(/ Web应用程序/);
resolver.setSuffix(HTML); 返回解析;
}}
RESTful服务索引页
@RestController
公共类的IndexController { @RequestMapping(/)
公共字符串指数(){
的System.out.println(在索引控制器看.........);
返回指数;
}}
在我的IDE控制台,我看到展望指数控制器......
在Chrome的开发工具从索引控制器,然后在网络打印我只看到本地主机200
。
index.html的
&LT;身体GT; &LT;! - [如果LT IE 7&GT;
&LT; p =类browsehappy&gt;您使用的是&LT;强&GT;&过时LT; / STRONG&GT;浏览器。请&lt; A HREF =http://browsehappy.com/&GT;升级浏览器&LT; / A&GT;改善您的体验与LT; / P&GT;
百分比抑制率ENDIF] - GT!; &LT; DIV NG-视图&gt;&LT; / DIV&GT; &LT; DIV&GT;角种子应用:V&LT;跨度的应用程序版本&GT;&LT; / SPAN&GT;&LT; / DIV&GT;
<一个href=\"http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content\"相对=nofollow>春季启动文档还说:
不要使用src /主/ webapps目录下,如果你的应用将是
打包成一个罐子。虽然这个目录是一个共同的标准,它
将只与战争的包装工作,它将会被忽略
大多数构建工具,如果你生成一个罐子。
块引用>春季引导是非常刚愎自用,效果最好的时候,你不要试图反抗的默认值。我看不出有任何理由已经摆在你的文件
/ src目录/主/ web应用
。只需使用/ src目录/主/资源/静态
为您的前端资产。这是最常见的地方。这将有助于从根本URI自动将这些静态文件,而不需要创建任何根级
控制器
。其实你的的IndexController
将prevent必须从根URI担任静态前端的文件。有没有必要建立控制器
静态文件的。还可以查看是不是需要为您的应用程序解析。您的应用程序是单页角应用程序使用只是REST API。所以,你的HTML模板是客户端。如果你是做服务器端的HTML模板(例如,与Thymeleaf或JSP)需要查看解析器。所以删除那块也。
I read the following documentation from spring.io and it said
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath
however when I put my index.html file under/resources
the stringindex
is just rendered. Currentlyindex.html
is under webapp and I am using AngularJS.MvcConfiguration
@Configuration public class MvcConfig { @Bean InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/webapp/"); resolver.setSuffix(".html"); return resolver; } }
Restful Service for index page
@RestController public class IndexController { @RequestMapping("/") public String index(){ System.out.println("Looking in the index controller........."); return "index"; } }
ON my IDE console I do see
Looking in the index controller......
printed from IndexController and under network in the Chrome development tools I only seelocalhost 200
.index.html
<body> <!--[if lt IE 7]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <div ng-view></div> <div>Angular seed app: v<span app-version></span></div>
解决方案Spring Boot docs also says:
Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.
Spring Boot is very opinionated and works best when you do not try to resist defaults. I don't see any reason having your files placed in
/src/main/webapp
. Just use/src/main/resources/static
for your front-end assets. That is most common place.It will serve these static files from root URI automatically, without need to create any root level
Controller
. In fact yourIndexController
would prevent static front-end files to be served from root URI. There is no need to createController
for static files at all.Also view resolver is not needed for your app. Your app is just REST API consumed by single page angular application. So your HTML templating is on client. View resolvers are needed if you are doing server side HTML templating (e.g. with Thymeleaf or JSP). So remove that piece also.
这篇关于春天开机找不到WebApp文件夹下的index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!