Spring Boot 在 webapp 文件夹下找不到 index.html [英] Spring boot cannot find index.html under webapp folder

查看:37
本文介绍了Spring Boot 在 webapp 文件夹下找不到 index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从

MVC 配置

@Configuration公共类 MvcConfig {@豆角,扁豆InternalResourceViewResolver viewResolver(){InternalResourceViewResolver 解析器 = new InternalResourceViewResolver();resolver.setPrefix("/webapp/");resolver.setSuffix(".html");返回解析器;}}

Restful Service for index page

@RestController公共类索引控制器{@RequestMapping("/")公共字符串索引(){System.out.println("在索引控制器中查找.........");返回索引";}}

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

index.html

<!--[如果是 IE 7]><p class="browsehappy">您使用的是<strong>过时</strong>浏览器.请<a href="http://browsehappy.com/">升级您的浏览器</a>以改善您的体验.</p><![endif]--><div ng-view></div><div>Angular 种子应用程序:v<span app-version></span></div>

解决方案

Spring Boot 文档 还说:

<块引用>

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

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

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

您的应用也不需要视图解析器.您的应用程序只是单页角度应用程序使用的 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 string index is just rendered. Currently index.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 see 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 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 your IndexController would prevent static front-end files to be served from root URI. There is no need to create Controller 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.

这篇关于Spring Boot 在 webapp 文件夹下找不到 index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆