当前位置: 首页 > 知识库问答 >
问题:

Spring不显示home.html

拓拔奇
2023-03-14

我正在学习Spring框架。我添加了主页。html资源/模板/主页中。html。但当我去看的时候,它是看不见的http://localhost:8080.我的结构如下:

taco cloud\src\main\java\tacos\tacoCloud应用程序。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\资源\静态\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>

输出

白色错误页面

localhost:8080/home.html显示home.html

共有3个答案

施慈
2023-03-14

下面是几种在项目中放置html文件的不同方法。(注意它的优先级从高到低)src/main/resources/resources/home。html src/main/resources/static/home。html src/main/resources/public/home。html

通过本文了解spring mvc项目结构

傅博容
2023-03-14

你可以试试这个。

@GetMapping("/")
    public String home()
    {
        return "templates/home.html";
    }

查看更多详细信息

刘运浩
2023-03-14

您必须将主页的位置更改为静态文件夹:

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

而不是

resources/templates/home.html

并在控制器中指定扩展:

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

否则,您必须创建一个视图解析器,以避免使用扩展,并指定页面的其他位置,请参阅使用Spring Boot配置ViewResolver,注释给出了没有找到带有URI错误的HTTP请求的映射

 类似资料:
  • 我无法在jsp Dispalyed中获得添加到模型中的值。我已经尝试了所有的方法,并检查了stackoverflow上的所有答案,但没有任何帮助。 为了节省时间,我粘贴了一部分代码: 我的: 我写错误的地方?

  • 我将greeting.html文件放在src/main/webapp/web-inf/templates和src/main/resources/templates中,我试图在application.properties中指定 我尝试了这些StackOverflow中提出的解决方案:“Spring Boot不提供静态内容”和“Spring Boot不启动静态web内容”,但没有任何改变... 最后是

  • 我需要使用Spring创建一个SOAP web服务。我一直在关注Spring官方网站上的教程:https://Spring.io/guides/gs/production-web-service/ 我已经成功地在本地计算机上部署了该教程中的SOAP Web服务,并且可以从浏览器成功地访问WSDL定义。之后,我尝试使用现有的XSD模式,但当我尝试访问WSDL定义时,它就不起作用了,控制台也没有显示任

  • 我使用的是SpringBoot2.1。3,我想添加一个错误页面作为回退。我试图添加一个错误。模板文件夹下的html,但它不起作用。 还试图添加错误控制器,并为每个错误类型分类一个页面,但它也不起作用。 我还需要做其他配置吗? 提前谢谢。

  • 单击时,我的应用可在相机和图库之间进行选择,然后该图片将显示在 ImageView 中。我最初尝试显示完整图像,然后尝试使用位图方式,但没有任何效果。我只是得到一个空白的图像视图。请给我一些关于我做错了什么的指导,并在必要时要求澄清: 相机/画廊照片代码:

  • Android Studio 3.4 styles.xml 在清单中: 在activity: 这里是布局: 但工具栏未显示: