【发布时间】:2020-11-16 04:58:28
【问题描述】:
我正在使用 IntelliJ IDEA 社区版 2020.1.2 x64 并使用 Spring DataJPA 和 Thymeleaf 编写程序,我的代码没有问题,我没有收到错误,但是当我传递我在控制器类中映射的 URL 时我无法得到想要的结果,即我创建的百里香模板没有显示在浏览器上。
我在浏览器上发布控制器类、thymeleaf 模板和输出以使您清楚:-
控制器类:
@RestController
@RequestMapping("/ecommerce")
public class EmployeeController {
@Autowired
private ProductService productService;
@GetMapping("/available")
public String ShowAllProducts(Model model)
{
model.addAttribute("listProducts",productService.getAllProducts());
return "availableProducts";
}
}
Thymeleaf 模板:-
<!DOCTYPE html>
<html xmlns:th="html://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>AvailableProducts</title>
</head>
<body>
<div align="center">
<h1>Product List</h1>
<table>
<thead>
<tr>
<th>p_id</th>
<th>ProductName</th>
<th>Productcost</th>
<th>QuantityAvailable</th>
</tr>
</thead>
<tbody>
<tr th:each="product : ${listProducts}">
<td th:text="${product.productId}"></td>
<td th:text="${product.productName}"></td>
<td th:text="${product.productCost}"></td>
<td th:text="${product.quanityAvailable}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
在浏览器上的输出:-
添加依赖:-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.properties:-
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ecommerce
spring.datasource.username=root
spring.datasource.password=deep
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.thymeleaf.prefix=classpath*:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
最后,当我传递 url http://hocalhost:8080/ecommerce/available 时,我在控制台中看到的是,hibernate 每次都在访问数据库。
提前谢谢.. 请帮助我被卡住了,不知道我做错了什么并且无法自己弄清楚..
【问题讨论】:
标签: spring-boot spring-mvc intellij-idea thymeleaf