简体   繁体   English

无法从 Spring-Boot 控制器呈现 thymleaf 页面。 只打印输出中的返回字符串

[英]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.

相关问题 无法从 Spring-Boot 控制器呈现 thymleaf 页面 - Unable to render thymleaf page from Spring-Boot controller 从spring-boot rest控制器返回JSON对象 - Return JSON object from a spring-boot rest Controller Spring Boot休息控制器。 返回对象,包括遗产属性以及异常处理 - Spring boot rest controller. Return object including heritage properties plus exception handling 鉴于调试器命中了我在控制器中设置的断点,因此Spring Boot返回404。 我该如何解决? - Spring boot return 404 given that debugger hits the breakpoint I set in the controller. How can I fix it? 有没有办法从 Spring Boot 中的 Restful Controller 返回 HTML 页面? - Is there a way to return HTML page from Restful Controller in Spring Boot? 如何在 Spring 引导中从 RESTful controller 返回 HTML 页面? - How to return an HTML page from a RESTful controller in Spring Boot? 无法使用 Spring 引导呈现 JSP 页面 - Unable to render the JSP page using Spring Boot Spring-Boot Controller无法识别来自Ajax的数据 - Spring-Boot Controller does not recognize data from ajax 无法读取 React 从 Spring-Boot 发送的 header 值 - Unable to read header value sent by React from Spring-Boot 无法从字符串值Spring-Boot Restful项目反序列化 - Cannot deserialize from String value Spring-Boot Restful project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM