gpt4 book ai didi

尝试使用 Thymeleaf 加载 HTML 文件时出现 Spring Boot 404

转载 作者:行者123 更新时间:2023-12-04 16:44:08 25 4
gpt4 key购买 nike

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

主类 :

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>

Controller :
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 and Thymeleaf: Can't find HTML templates & Why the html page doesn't get showed in thymeleaf?

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

任何试图找出为什么我无法访问 HTML 页面的帮助将不胜感激。

最佳答案

您的 StudentAddressBookApplication 位于包“com.michaelLong.studentaddressbook”=> 它将仅扫描来自该父包的 bean。

StudentController 位于包“controller”=> 应用程序根本不会扫描它。

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

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

关于尝试使用 Thymeleaf 加载 HTML 文件时出现 Spring Boot 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52505514/

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