控制器类找不到HTML模板

时间:2019-02-10 18:33:38

标签: spring-boot spring-mvc thymeleaf html-templates

My directory

我正在尝试为学校创建Spring Boot应用程序,该应用程序使用控制器将书从数据库列出到html页面。

我个人认为问题是控制器由于某种原因找不到模板。因为当我通过chrome浏览至所需模板时,它只会在页面上显示“ booklist”,而没有其他显示。

我尝试创建一个全新的项目,并将代码从其他文件复制到新文件中,但是没有结果。

我的控制器类:

@Controller
@ResponseBody
public class BookController {
@Autowired
BookRepository bookRepository;

@RequestMapping(value = "/books", method = RequestMethod.GET)
public String getBooks(Model model) {
    List<Book> books =  (List<Book>) bookRepository.findAll();
    model.addAttribute("books", books);
    return "booklist";
}

我的html模板:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Book List</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Books</h1>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Year</th>
        <th>Isbn</th>
        <th>Price</th>
    </tr>

    <tr th:each="book : ${books}">
        <td th:text="${book.id}">id</td>
        <td th:text="${book.title}">title</td>
        <td th:text="${book.year}">year</td>
        <td th:text="${book.isbn}">isbn</td>
        <td th:text="${book.price}">price</td>

    </tr>
</table>
</body>
</html>

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>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>hh.swd20</groupId>
<artifactId>Bookstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Bookstore</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</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>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

application.properties文件:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true

2 个答案:

答案 0 :(得分:1)

从您的控制器类中删除注释@ResponseBody,因为:

  

@ResponseBody注释告诉控制器返回的对象会自动序列化为JSON并传递回HttpResponse对象。

然后,Spring-MVC将使用返回的字符串booklist来解析命名的HTML模板文件。

默认情况下,将在默认模板目录booklist.html中查找模板文件(例如src/main/resources/templates)。

否则,请确保拥有configured the ViewResolver properly

对于胸腺,您必须向Maven POM添加依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

另请参阅this Spring-Boot & Thymeleaf tutorial

答案 1 :(得分:0)

您可以尝试一下。

命中:localhost:// yourportnumber / api / books

//@Controller
  @RestController
//@ResponseBody
  @RequestMapping("/api")
  public class BookController {
  @Autowired
  BookRepository bookRepository;

  @GetMapping(value = "/books", method = RequestMethod.GET)
  public ModelAndView getBooks(Model model) {
  List<Book> books =  (List<Book>) bookRepository.findAll();
  model.addAttribute("books", books);
  ModelAndView mav = new ModelAndView("booklist");
  return mav;

}