简体   繁体   中英

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. 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 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

<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.

That's because you are using @RestController annotation on top of your controller, which is nothing but @Controller+@ResponseBody.

Try to use only @Controller and then based on your method use @ResponseBody exclusively.

For example . If you want to return json response body then use @ResponseBody on method.

If you want to render thymeleaf page then don't use @ResponseBody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM