英文:
Can't access index file in spring boot project
问题
以下是您提供的代码的中文翻译:
package com.school.project;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/")
public String AllUsers(Model model) {
model.addAttribute("listUsers", userService.getAllUsers());
return "users";
}
}
以及位于我的模板文件夹中的index.html
文件:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>管理网站</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" th:href="@{webjars/bootstrap/5.2.3/css/bootstrap.min.css}">
</head>
<body>
<div class="container-fluid text-center">
<div>
<table>
<thead>
<th>ID</th>
<th>电子邮件</th>
<th>名称</th>
<th>用户名</th>
<th>密码</th>
<th></th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>
但是,每当我启动我的应用程序时,我都会收到以下错误:
2023年5月20日18:57:06 IDT
发生了意外错误(类型=内部服务器错误,状态=500)。
错误解析模板[users],模板可能不存在,或者可能无法由任何配置的模板解析器之一访问
org.thymeleaf.exceptions.TemplateInputException: 错误解析模板[users],模板可能不存在,或者可能无法由任何配置的模板解析器之一访问
如果我更改我的索引映射的路由,HTML将无问题加载,但如果我为它们提供相同的路由,我会收到该错误。
这与我尝试访问没有匹配模板文件的URL时收到的错误相同。
我知道这个错误已经发布过,但是其中没有任何解决方案适用于我。
我的服务类如下:
package com.school.project;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() {
List<User> users = userRepository.findAll();
return users;
}
}
以及我的模型类:
package com.school.project;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@Table(name = "user")
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "isAdmin")
private boolean isAdmin;
@Column(name = "email")
private String email;
@Column(name = "username")
private String userName;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
}
希望这些翻译能帮助您更好地理解您的代码和问题。如果您有其他问题或需要进一步的帮助,请随时提出。
英文:
I have the following controller.
package com.school.project;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/")
public String AllUsers(Model model) {
model.addAttribute("listUsers", userService.getAllUsers());
return "users";
}
}
and an index.html
file placed in my templates folder.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Manager Site</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" th:href="@{webjars/bootstrap/5.2.3/css/bootstrap.min.css}">
</head>
<body>
<div class="container-fluid text-center">
<div>
<table>
<thead>
<th>ID</th>
<th>E-mail</th>
<th>name</th>
<th>username</th>
<th>password</th>
<th></th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>
But whenever I launch my application, I get the following error.
Sat May 20 18:57:06 IDT 2023
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [users], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [users], template might not exist or might not be accessible by any of the configured Template Resolvers
If I change the routing of my index mapping, the html will load with no problem, but if I give them the same routing, I get that error.
It is the same error. I get if I try to go to a URL with no matching template file.
I know this error has already been posted, but none of the solutions worked for me.
My service class is the following.
package com.school.project;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() {
List<User> users = userRepository.findAll();
return users;
}
}
and my model class.
package com.school.project;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@Table(name = "user")
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "isAdmin")
private boolean isAdmin;
@Column(name = "email")
private String email;
@Column(name = "username")
private String userName;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
}
答案1
得分: 1
更新文件内容如下:html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Manager Site</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/5.2.3/css/bootstrap.min.css}">
</head>
<body>
<div class="container-fluid text-center">
<div>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>Name</th>
<th>Username</th>
<th>Password</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${listUsers}">
<td th:text="${user.id}"></td>
<td th:text="${user.email}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td><!-- additional actions if needed --></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
英文:
Update the file with the following content: html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Manager Site</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/5.2.3/css/bootstrap.min.css}">
</head>
<body>
<div class="container-fluid text-center">
<div>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>Name</th>
<th>Username</th>
<th>Password</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${listUsers}">
<td th:text="${user.id}"></td>
<td th:text="${user.email}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td><!-- additional actions if needed --></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
答案2
得分: 0
我认为我找到了问题,你的文件html的名称应该与你的方法AllUsers返回的名称相同,所以只需将index.html替换为users.html。
英文:
I think i found the problem, The name of your file html should be the same name returned in your method AllUsers, so just replace index.html by users.html.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论