+-
java – Spring引导在webapp文件夹下找不到index.html
我从 spring.io读到了以下文档,它说默认情况下,Spring Boot将在类路径中提供来自名为/ static(或/ public或/ resources或/ META-INF / resources)的目录中的静态内容,但是当我放入index.html时/ resources下的文件只是呈现字符串索引.目前index.html在webapp下,我正在使用AngularJS.

directory

MvcConfiguration

@Configuration
public class MvcConfig {

    @Bean
    InternalResourceViewResolver viewResolver(){

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

        return resolver;
    }

}

索引页面的Restful Service

@RestController
public class IndexController {

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

}

在我的IDE控制台上,我确实看到在索引控制器中……从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>
最佳答案
Spring Boot docs还说:

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非常自以为是,当你不试图抵制默认值时效果最佳.我没有看到任何理由将您的文件放在/ src / main / webapp中.只需使用/ src / main / resources / static作为前端资产.那是最常见的地方.

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

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

点击查看更多相关文章

转载注明原文:java – Spring引导在webapp文件夹下找不到index.html - 乐贴网