我试图建立一个简单的Spring Web应用程序。该应用程序工作正常,但我无法连接到Web服务器时,使用Thymeleaf模板。
控制器类如下所示:
@Controller() public class BookController {
private final BookRepository bookRepository;
public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@GetMapping("/books")
public String getBooks(Model model){
model.addAttribute("bookList", bookRepository.findAll());
return "books/list";
}
}
HTML模板:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring Web App</title>
</head>
<body>
<h1>Book List</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
</tr>
<tr th:each="book : ${bookList}">
<td th:text="${book.id}">123</td>
<td th:text="${book.title}"> Spring in Action</td>
<td th:text="${book.isbn}">Wrox</td>
</tr>
</table>
</body>
</html>
我的目录树如下:
我没有得到任何错误或异常,我只得到下面的空白页:
我已经尝试了"@GetMapping"和"@RequestMapping",并遵循了一些教程,但我不知道是什么问题。如果有人能帮助我解决这个问题,我将非常感谢。
我设法Map了错误,但只得到了状态代码:https://i.stack.imgur.com/CGmSI.png
我还尝试使用'server.error.include-message = always'属性,但只收到以下消息:无可用消息
2条答案
按热度按时间ncecgwcz1#
在屏幕中尝试这样的文件夹结构
daolsyd02#
问题是包控制器凌驾于其他控制器之上,而IDE解释起来就像它处于同一级别。
我只需要把包移到同一个级别,它就起作用了。