[英]Unable to render thymleaf page from Spring-Boot controller. Prints only the return string in output
Issue : I have developed a Spring-Boot rest api which I can call from POSTMAN.问题:我开发了一个 Spring-Boot rest api,我可以从 POSTMAN 调用它。 Now I am requesting the same REST API method from a Thymleaf page.
现在我从 Thymleaf 页面请求相同的 REST API 方法。 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 productsList.html
:这是我的Thymleaf页面
productsList.html
:
<!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 Thymleaf 的 Maven 依赖
<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.
我能够呈现一个 index.html 页面。 But not this page.
但不是这个页面。
That's because you are using @RestController annotation on top of your controller, which is nothing but @Controller+@ResponseBody.那是因为您在控制器之上使用了@RestController 注释,它只不过是@Controller+@ResponseBody。
Try to use only @Controller and then based on your method use @ResponseBody exclusively.尝试仅使用 @Controller,然后根据您的方法专门使用 @ResponseBody。
For example .例如 。 If you want to return json response body then use @ResponseBody on method.
如果要返回 json 响应正文,请在方法上使用 @ResponseBody。
If you want to render thymeleaf page then don't use @ResponseBody.如果要呈现 thymeleaf 页面,请不要使用@ResponseBody。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.