简体   繁体   English

无法使用 Spring Boot 提供静态 index.html

[英]Can't serve static index.html with Spring Boot

I'm new to Spring and I find it is really hard to get into it.我是 Spring 的新手,我发现很难进入它。 I want to serve a static index.html but it doesn't work.我想提供静态 index.html 但它不起作用。 在此处输入图片说明

I used this tutorial but can't access index.html with: http://localhost:8080/ or http://localhost:8080/src/main/public/index.html我使用了教程,但无法访问 index.html: http://localhost:8080/http://localhost:8080/src/main/public/index.html

IndexHtmlController:索引HTML控制器:

package de.phip1611.springboot_test_1;

import org.springframework.stereotype.Controller;

@Controller
public class IndexHtmlController {}
// due to https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
// this should be enough..

在此处输入图片说明

Can you try the below: 您可以尝试以下方法:

  1. Move index.html to src/main/resources/static folder index.html移动到src/main/resources/static文件夹
  2. In the application.properties file of spring boot, set server.contextPath to / . 在Spring Boot的application.properties文件中,将server.contextPath设置为/ (ie server.contextPath=/ ) (即server.contextPath=/

I have a html file in src/main/resources/static folder and added below in application.yml but it didn't work for me either. 我在src / main / resources / static文件夹中有一个html文件,并在application.yml下面添加了它,但它对我也不起作用。 On tomcat start ,localhost:8080 shows a 404 and webpage shows whitelabel error. 在tomcat start上,localhost:8080显示404,网页显示whitelabel错误。 But for localhost:8080/hello.html I see a 200 response code when inspect but no contents of html on webpage . 但是对于localhost:8080 / hello.html,我在检查时看到200响应代码,但网页上没有html内容。

server:
  servlet:
    context-path: /
  port: 8080

I encounter similar issue. 我遇到类似的问题。 I have spring boot 2.1.3, and only has spring-boot-starter-web as dependency. 我有spring boot 2.1.3,并且只有spring-boot-starter-web作为依赖项。

By placing index.html under src/main/public folder, and creating a controller that map "/" and simply returns "index.html", i observe same error as the thread starter. 通过将index.html放在src / main / public文件夹下,并创建一个映射“ /”并仅返回“ index.html”的控制器,我观察到与线程启动器相同的错误。

But when I place index.html under src/main/resources/public. 但是当我将index.html放在src / main / resources / public下时。 It works. 有用。

But when I place index.html under src/main/resources. 但是当我将index.html放在src / main / resources下时。 It not works. 它不起作用。

I am confused. 我很困惑。 I thought by default, Spring boot will also look for static content under src/main/public and src/main/resources. 我认为默认情况下,Spring Boot还会在src / main / public和src / main / resources下查找静态内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM