英文:
Unable to render thymleaf page from Spring-Boot controller
问题
问题: 我开发了一个Spring Boot的REST API,我可以从POSTMAN调用它。现在我想从一个Thymeleaf页面中请求相同的REST API方法。但它只呈现一个字符串,实际页面没有加载。
这是我的控制器:
@RestController
@RefreshScope
@RequestMapping("/shopping")
public class ShoppingController {
@RequestMapping(value = "/productList")
public String listAllProducts(ModelMap model) {
logger.info("ShoppingMS : listAllProducts()");
ResponseEntity<List<Product>> responseProductList = prodServ.listAllProducts();
List<Product> products = responseProductList.getBody();
if(products.isEmpty()) {
logger.info("No Products Found");
}
logger.info("No of products fetched : " + products.size());
model.addAttribute("products", products);
return "productList";
}
}
这是我的Thymeleaf页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8">
<title>Product List</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/styles.css}">
</head>
<body>
<th:block th:include="/_header"></th:block>
<th:block th:include="/menu"></th:block>
<div class="page-title">Product List</div>
<div class="product-preview-container" th:each="prodInfo : ${products.list}">
<ul>
<li>Product Code: <span th:utext="${prodInfo.productCode}"></span></li>
<li>Product Name: <span th:utext="${prodInfo.productName}"></span></li>
<li>Product Price: <span th:utext="${#numbers.formatDecimal(prodInfo.productPrice,3,2,'COMMA')}"></span></li>
<li><a th:href="@{|/buyProduct?code=${prodInfo.code}|}">Buy Now</a></li>
</ul>
</div>
<br />
<div class="page-navigator">
<th:block th>
<a th:href="@{|/productList|}" class="nav-item"></a>
<span class="nav-item"> ... </span>
</th:block>
</div>
<th:block th:include="/_footer"></th:block>
</body>
</html>
Thymeleaf的Maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
项目结构图像:
输出:
调用URL: http://localhost:1000/shopping/productList
它在页面上只返回字符串 productList
。
请告诉我我做错了什么。我可以渲染一个index.html页面,但不能渲染这个页面。
更新: 我提供了我正在使用的Maven依赖以及我Rest Controller类上使用的注解。
英文:
Issue : I have developed a Spring-Boot rest api which I can call from POSTMAN.
Now I am requesting the same REST API method from a Thymleaf page. But it renders only a string. The actual page doesn't gets loaded..
This is my controller :
@RestController
@RefreshScope
@RequestMapping("/shopping")
public class ShoppingController {
@RequestMapping(value="/productList")
public String listAllProducts(ModelMap model){
logger.info("ShoppingMS : listAllProducts()");
ResponseEntity<List<Product>> responseProductList = prodServ.listAllProducts();
List<Product> products = responseProductList.getBody();
if(products.isEmpty()) {
logger.info("No Products Found");
}
logger.info("No of products fetched : " +products.size());
model.addAttribute("products", products);
return "productList";
}
}
This is my Thymleaf page :
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8">
<title>Product List</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/styles.css}">
</head>
<body>
<th:block th:include="/_header"></th:block>
<th:block th:include="/menu"></th:block>
<div class="page-title">Product List</div>
<div class="product-preview-container"
th:each="prodInfo : ${products.list}">
<ul>
<li>Product Code: <span th:utext="${prodInfo.productCode}"></span></li>
<li>Product Name: <span th:utext="${prodInfo.productName}"></span></li>
<li>Product Price: <span th:utext="${#numbers.formatDecimal(prodInfo.productPrice,3,2,'COMMA')}"></span></li>
<li><a th:href="@{|/buyProduct?code=${prodInfo.code}|}">Buy Now</a></li>
</ul>
</div>
<br />
<div class="page-navigator">
<th:block th>
<a th:href="@{|/productList|}" class="nav-item"></a>
<span class="nav-item" > ... </span>
</th:block>
</div>
<th:block th:include="/_footer"></th:block>
</body>
</html>
Maven Dependency for Thymleaf :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Project Structure Image :
Output :
Calling URL : http://localhost:1000/shopping/productList
It returns only the string `productList` in the page.
Please tell me where I am doing wrong. I am able to render a index.html page. But not this page.
UPDATE : I have provided the Maven dependency I am using and the Annotations used over my Rest Controller class.
答案1
得分: 1
-
我相信您要么
-
缺少
spring-boot-starter-thymeleaf
依赖项。此依赖项包含必要的依赖项和Thymeleaf模板解析器的自动配置(将返回类型字符串转换为HTML模板名称)以及实际渲染。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 或者在您的控制器类中使用 @Controller 而不是 @RestController。@RestController 默认返回 JSON 响应。要进行模板渲染,您应该使用 @Controller。
英文:
I believe you are either
- missing
spring-boot-starter-thymeleaf
dependency. This dependency contains necessary dependency and auto configuration for thymeleaf template resolver(return type string to html template name) and actual rendering.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- OR using @Controller instead of @RestController in your controller class. @RestController returns json response by default. For template rendering you should use @Controller.
答案2
得分: 0
使用@Controller
代替@RestController
解决了这个问题。现在可以成功渲染HTML页面。
英文:
Answer :
Using @Controller
instead of @RestController
resolved the issue. Able to render the html page now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论