gpt4 book ai didi

java - Spring 不显示 home.html

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:59 24 4
gpt4 key购买 nike

我正在学习 Spring 框架。我在 resources/templates/home.html 中添加了 home.html。但是访问http://localhost:8080是看不到的.我有以下结构:

taco-cloud\src\main\java\tacos\TacoCloudApplication.java

package tacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TacoCloudApplication {

public static void main(String[] args) {
SpringApplication.run(TacoCloudApplication.class, args);
}

}

taco-cloud\src\main\java\tacos\HomeController.java

package tacos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController
{
@GetMapping("/")
public String home()
{
return "home.html";
}
}

taco-cloud\src\main\resources\static\home.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Taco Cloud</title>
</head>
<body>
<h1>Welcome to...</h1>
<img th:src="@{/images/TacoCloud.png}"/>
</body>
</html>

输出

可白化错误页面

本地主机:8080/home.html显示 home.html

最佳答案

您必须将主页的位置更改为在 static 文件夹中:

resources/static/home.html
^^^^^^

代替

resources/templates/home.html

并在你的 Controller 中指定扩展名:

return "home.html";
^^^^^

否则您必须创建 View 解析器以避免使用扩展并指定页面的其他位置,请查看 Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error

关于java - Spring 不显示 home.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56713050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com