Java Spring Boot 在 webapp 文件夹下找不到 index.html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35968479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:18:11  来源:igfitidea点击:

Spring boot cannot find index.html under webapp folder

javaangularjsspringspring-mvcspring-boot

提问by Mike3355

I read the following documentation from spring.ioand 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 classpathhowever when I put my index.html file under /resourcesthe string indexis just rendered. Currently index.htmlis under webapp and I am using AngularJS.

我从spring.io阅读了以下文档,但它说By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath当我将 index.html 文件放在/resources字符串下时,index它只是呈现出来。目前index.html在 webapp 下,我正在使用 AngularJS。

directory

目录

MvcConfiguration

MVC配置

@Configuration
public class MvcConfig {

    @Bean
    InternalResourceViewResolver viewResolver(){

        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/webapp/");
        resolver.setSuffix(".html");

        return resolver;
    }

}

Restful Service for index page

索引页的 Restful 服务

@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 see localhost 200.

在我的 IDE 控制台上,我确实看到Looking in the index controller......从 IndexController 打印出来,在 Chrome 开发工具的网络下我只看到localhost 200.

index.html

索引.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>

采纳答案by luboskrnac

Spring Boot docsalso says:

Spring Boot 文档还说:

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.

如果您的应用程序将被打包为 jar,请不要使用 src/main/webapp 目录。虽然这个目录是一个通用标准,但它只适用于 war 打包,如果你生成一个 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/staticfor your front-end assets. That is most common place.

Spring Boot 非常自以为是,当您不尝试抵制默认值时效果最佳。我看不出有任何理由将您的文件放在/src/main/webapp. 仅/src/main/resources/static用于您的前端资产。那是最常见的地方。

It will serve these static files from root URI automatically, without need to create any root level Controller. In fact your IndexControllerwould prevent static front-end files to be served from root URI. There is no need to create Controllerfor static files at all.

它将自动从根 URI 提供这些静态文件,而无需创建任何根级别Controller. 事实上,您IndexController会阻止从根 URI 提供静态前端文件。根本不需要Controller为静态文件创建。

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.

您的应用程序也不需要视图解析器。您的应用程序只是单页角度应用程序使用的 REST API。所以你的 HTML 模板在客户端上。如果您正在做服务器端 HTML 模板(例如使用 Thymeleaf 或 JSP),则需要查看解析器。所以也删除那块。

回答by Volodymyr Grynda

@RestController
public class IndexController {

    @RequestMapping("/")
    public String index(){
        System.out.println("Looking in the index controller.........");
        return "index";
    }

}

Problem is here, you are using @RestController, so in this case, if you write "return 'index';" spring boot covers it as just string answer. You need use @Controller annotation instead.

问题就在这里,你使用的是@RestController,所以在这种情况下,如果你写“return 'index';” spring boot 将其覆盖为字符串答案。您需要改用 @Controller 注释。