英文:
html page can't read from model spring boot thymeleaf
问题
I have the following controller
package com.website.school;
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.PostMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public String index(Model model) {
model.addAttribute("listUsers", userService.getAllUsers());
return "users";
}
}
and a users.html file
<!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">
<h1>ljkñafsñjafnjdfbhjñsdbfjñsdfvhjsdf</h1>
<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></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
But whenever I run my application and navigate to /users I get the following error
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/users.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/users.html]")
If I comment out the tbody the page loads as expected
I also know that I have a connection with my database because I made a test class that is a RestController and it returns the json I expected
My service class
package com.website.school;
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() {
return userRepository.findAll();
}
}
My Model class
package com.website.school;
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;
}
My repository
package com.website.school;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
英文:
I have the following controller
package com.website.school;
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.PostMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public String index(Model model) {
model.addAttribute("listUsers", userService.getAllUsers());
return "users";
}
}
and a users.html file
<!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">
<h1>ljkñafsñjafnjdfbhjñsdbfjñsdfvhjsdf</h1>
<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></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
But whenever I run my application and navigate to /users I get the following error
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/users.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/users.html]")
If I comment out the tbody the page loads as expected
I also know that I have a connection with my database because I made a test class that is a RestController and it returns the json I expected
My service class
package com.website.school;
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() {
return userRepository.findAll();
}
}
My Model class
package com.website.school;
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;
}
My repository
package com.website.school;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
答案1
得分: 1
错误拼写的实体字段 userName
。
应该是:
<td th:text="${user.userName}"></td>
英文:
Wrong spelling of of the entity field userName
.
<td th:text="${user.username}"></td>
Should be:
<td th:text="${user.userName}"></td>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论