尝试使用 Thymeleaf 加载 HTML 文件时出现 Spring Boot 404 [英] Spring Boot 404 when trying to load a HTML file using Thymeleaf

查看:85
本文介绍了尝试使用 Thymeleaf 加载 HTML 文件时出现 Spring Boot 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我在尝试访问 localhost:8080 时收到 Whitelabel 404 错误页面.

Like the title says, I'm getting the Whitelabel 404 error page when trying to access localhost:8080.

主类:

package com.michaelLong.studentaddressbook;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class StudentAddressBookApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {

        SpringApplication.run(StudentAddressBookApplication.class, args);
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.michaelLong</groupId>
    <artifactId>student-address-book</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>student-address-book</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制器:

package controller;

import model.Student;
import model.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import java.util.Map;

@Controller
public class StudentController {

    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/")
    public String showStudents(Model model){
        model.addAttribute("students", studentRepository.findAll());
        return "showStudents";
    }
}

application.properties:

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/StudentAddressBook
spring.datasource.username=root
spring.datasource.password=SQLpassword

showStudents.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Student List</title>
</head>
<body>
<h2>List of students</h2>

<table>
    <tr>
        <th>Id</th>
        <th>First name</th>
        <th>Last name</th>
    </tr>
    <tr th:each="student: ${students}">
        <td th:text="${student.id}">Id</td>
        <td th:text="${student.firstName}">First name</td>
        <td th:text="${student.lastName}">Last name</td>
    </tr>

</table>
</body>
</html>

项目结构:

src
|__main
   |__java
   |  |__com.example.studentaddressbook
   |  |  |__StudentAddressBookApplication
   |  |__controller
   |  |  |__StudentController
   |  |__model
   |     |__Student
   |     |__StudentRepository
   |__resources
      |__static
      |__templates
      |  |__showStudents.html
      |__application.properties

我试过查看很多不同的教程,以及一些像这样的帖子:Spring Boot 和 Thymeleaf:找不到 HTML 模板 &为什么 thymeleaf 中没有显示 html 页面?

I've tried looking at a lot of different tutorials, and a few SO posts like these: Spring Boot and Thymeleaf: Can't find HTML templates & Why the html page doesn't get showed in thymeleaf?

我最初尝试使用 JSP,但我也无法使用它们.在这一点上,我觉得我的头撞在墙上,我不知道还能做什么.这是我第一次尝试使用 Spring Boot 和 Thymeleaf,所以我很难弄清楚.

I was originally trying to use JSPs, but I couldn't get those to work either. I feel like I'm banging my head against the wall at this point and I'm not sure what else to do. This is my first time trying to use Spring Boot and Thymeleaf, so I'm having some difficulty figuring this out.

任何试图找出我无法访问 HTML 页面的原因的帮助将不胜感激.

Any help trying to figure out why I can't access the HTML page would be greatly appreciated.

推荐答案

你的 StudentAddressBookApplication 位于包 "com.michaelLong.studentaddressbook" => 它将只扫描来自这个父包的 bean.

You have your StudentAddressBookApplication located in package "com.michaelLong.studentaddressbook" => it will scan only the beans from this parent package.

StudentController 位于包controller"=> 应用程序根本不会扫描它.

StudentController is located in package "controller" => the application will not scan it at all.

非常简单的解决方案:将 StudentController 移动到包 com.michaelLong.studentaddressbook.此外,这同样适用于 StudentRepository.

Very simple solution : move StudentController to package com.michaelLong.studentaddressbook. Also, the same applies for StudentRepository.

Java 中的 P.S 包总是小写的.

P.S packages in java are always lower case.

这篇关于尝试使用 Thymeleaf 加载 HTML 文件时出现 Spring Boot 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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