无法从 Spring-启动控制器呈现 thymeleaf 页面
Unable to render thymleaf page from Spring-Boot controller
问题:我开发了一个 Spring-Boot rest api,我可以从 POSTMAN 调用它。
现在我从 Thymleaf 页面请求相同的 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";
}
}
这是我的 Thymleaf 页面:
<!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>
Thymleaf 的 Maven 依赖性 :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
项目结构图:
输出:
Calling URL : http://localhost:1000/shopping/productList
It returns only the string `productList` in the page.
请告诉我哪里做错了。我能够呈现 index.html 页面。但不是这个页面。
UPDATE :我提供了我正在使用的 Maven 依赖项以及在我的 Rest Controller class.
上使用的注释
我相信你是
- 缺少
spring-boot-starter-thymeleaf
依赖项。此依赖项包含 thymeleaf 模板解析器(return 类型字符串到 html 模板名称)和实际渲染的必要依赖项和自动配置。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 或者在您的控制器中使用@Controller 而不是@RestController class。 @RestController returns json 默认响应。对于模板渲染,你应该使用@Controller。
答案:
使用 @Controller
而不是 @RestController
解决了这个问题。现在可以呈现 html 页面。
问题:我开发了一个 Spring-Boot rest api,我可以从 POSTMAN 调用它。 现在我从 Thymleaf 页面请求相同的 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";
}
}
这是我的 Thymleaf 页面:
<!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>
Thymleaf 的 Maven 依赖性 :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
项目结构图:
输出:
Calling URL : http://localhost:1000/shopping/productList
It returns only the string `productList` in the page.
请告诉我哪里做错了。我能够呈现 index.html 页面。但不是这个页面。
UPDATE :我提供了我正在使用的 Maven 依赖项以及在我的 Rest Controller class.
上使用的注释我相信你是
- 缺少
spring-boot-starter-thymeleaf
依赖项。此依赖项包含 thymeleaf 模板解析器(return 类型字符串到 html 模板名称)和实际渲染的必要依赖项和自动配置。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 或者在您的控制器中使用@Controller 而不是@RestController class。 @RestController returns json 默认响应。对于模板渲染,你应该使用@Controller。
答案:
使用 @Controller
而不是 @RestController
解决了这个问题。现在可以呈现 html 页面。