Spring引导在webapp文件夹下找不到index.html

Dre*_*208 11 java spring spring-mvc angularjs spring-boot

我从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.

目录

MvcConfiguration

@Configuration
public class MvcConfig {

    @Bean
    InternalResourceViewResolver viewResolver(){

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

        return resolver;
    }

}
Run Code Online (Sandbox Code Playgroud)

索引页面的Restful Service

@RestController
public class IndexController {

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

}
Run Code Online (Sandbox Code Playgroud)

在我的IDE控制台上,我确实看到Looking in the index controller......从IndexController和网络中打印出来的Chrome开发工具我只看到了localhost 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>
Run Code Online (Sandbox Code Playgroud)

lub*_*nac 31

Spring Boot文档还说:

如果您的应用程序将打包为jar,请不要使用src/main/webapp目录.虽然这个目录是一个通用标准,但它只适用于war包装,如果你生成一个jar,它将被大多数构建工具默默忽略.

Spring Boot非常自以为是,当你不试图抵制默认值时效果最佳.我没有看到任何理由放入您的文件/src/main/webapp.只需/src/main/resources/static用于您的前端资产.那是最常见的地方.

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

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