控制器 class 找不到 html 模板
Controller class cannot find html template
My directory
我正在尝试为学校创建 spring 引导应用程序,该应用程序使用控制器将数据库中的书籍列出到 html 页面。
就我个人而言,我认为问题在于控制器出于某种原因找不到模板。因为当我通过 chrome 导航到想要的模板时,它只在页面上显示 "booklist",没有其他内容。
我已经尝试创建全新的项目并将代码从我的其他文件复制到新文件,但没有结果。
我的控制器class:
@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
从您的控制器 class 中删除注释 @ResponseBody,因为:
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.
然后 Spring-MVC 将使用返回的字符串 booklist
来解析命名的 HTML 模板文件。
默认情况下,将在 默认模板目录 中查找模板文件(例如 booklist.html
)src/main/resources/templates
。
否则确保 .
对于 Thymeleaf 你必须添加依赖到你的 Maven POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
你可以试试这个。
命中: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;
}
My directory
我正在尝试为学校创建 spring 引导应用程序,该应用程序使用控制器将数据库中的书籍列出到 html 页面。
就我个人而言,我认为问题在于控制器出于某种原因找不到模板。因为当我通过 chrome 导航到想要的模板时,它只在页面上显示 "booklist",没有其他内容。
我已经尝试创建全新的项目并将代码从我的其他文件复制到新文件,但没有结果。
我的控制器class:
@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
从您的控制器 class 中删除注释 @ResponseBody,因为:
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.
然后 Spring-MVC 将使用返回的字符串 booklist
来解析命名的 HTML 模板文件。
默认情况下,将在 默认模板目录 中查找模板文件(例如 booklist.html
)src/main/resources/templates
。
否则确保
对于 Thymeleaf 你必须添加依赖到你的 Maven POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
你可以试试这个。
命中: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;
}