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;
}
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.