首页 > 解决方案 > 如何配置 Spring/Springboot 访问主页?

问题描述

我正在尝试使用 Springboot(JPA、H2)和 Thymeleaf 构建我的第一个 Web 应用程序。

entities/我的目标是通过控制器( )获取/修改/删除存储到数据库( )中的数据controllers/

但是,家乡路线不起作用。

你能告诉我并解释我做错了什么吗?我忘了配置什么?

感谢帮助!

错误

网址:localhost:8082/

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Mar 08 14:22:21 CET 2019
There was an unexpected error (type=Not Found, status=404).
No message available

索引控制器

@RestController
@RequestMapping("/")
public class IndexController {

    @GetMapping(value = "/")
    public ModelAndView getHome(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
}

索引.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
</head>
<body>
    <p>Test</p>
</body>
</html>

文件夹

projetname/
|__src/
   |__main/
      |__java/
          |__projectname/
          |    |__controllers/
          |    |  |__IndexController.java
          |    |__entities/
          |    |__repositories/
          |    |__App.java
          |__resources/
              |__static/
              |   |__css/
              |   |__js/    
              |__templates/
              |   |__index.html 
              |__data.sql
              |__application.properties 

标签: javaspringspring-bootspring-mvcthymeleaf

解决方案


确保 spring-boot-starter-thymeleaf 作为项目的依赖项包含在内。

本指南可能会有所帮助


推荐阅读