无法从Spring Boot控制器渲染Thymeleaf页面。输出中只打印返回字符串。

huangapple go评论82阅读模式
英文:

Unable to render thymleaf page from Spring-Boot controller. Prints only the return string in output

问题

问题: 我已经开发了一个可以从POSTMAN调用的Spring-Boot REST API。现在我想从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**页面`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>

**Thymleaf的Maven依赖**

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

**项目结构图像**

[![enter image description here][1]][1]

  [1]: https://i.stack.imgur.com/5bkK7.png

**输出**:

    调用URL: http://localhost:1000/shopping/productList
    页面仅返回字符串 `productList`。

请告诉我我在哪里出错了。我能够呈现一个index.html页面,但是不能呈现这个页面。
英文:

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(&quot;/shopping&quot;)
public class ShoppingController {

@RequestMapping(value=&quot;/productList&quot;)
public String listAllProducts(ModelMap model){
    
    logger.info(&quot;ShoppingMS : listAllProducts()&quot;);
    
    ResponseEntity&lt;List&lt;Product&gt;&gt; responseProductList = prodServ.listAllProducts();
    List&lt;Product&gt; products = responseProductList.getBody();
    
    if(products.isEmpty()) {
        logger.info(&quot;No Products Found&quot;);
    }
     
    logger.info(&quot;No of products fetched : &quot; +products.size());
    
    model.addAttribute(&quot;products&quot;, products);
    return &quot;productList&quot;;
}
}

This is my Thymleaf page productsList.html :

&lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;
    xmlns:sec=&quot;http://www.thymeleaf.org/thymeleaf-extras-springsecurity4&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Product List&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{/css/styles.css}&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;th:block th:include=&quot;/_header&quot;&gt;&lt;/th:block&gt;
    &lt;th:block th:include=&quot;/menu&quot;&gt;&lt;/th:block&gt;

    &lt;div class=&quot;page-title&quot;&gt;Product List&lt;/div&gt;

    &lt;div class=&quot;product-preview-container&quot;
        th:each=&quot;prodInfo : ${products.list}&quot;&gt;
        &lt;ul&gt;
            &lt;li&gt;Product Code:  &lt;span th:utext=&quot;${prodInfo.productCode}&quot;&gt;&lt;/span&gt;&lt;/li&gt;
            &lt;li&gt;Product Name:  &lt;span th:utext=&quot;${prodInfo.productName}&quot;&gt;&lt;/span&gt;&lt;/li&gt;
            &lt;li&gt;Product Price: &lt;span th:utext=&quot;${#numbers.formatDecimal(prodInfo.productPrice,3,2,&#39;COMMA&#39;)}&quot;&gt;&lt;/span&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a th:href=&quot;@{|/buyProduct?code=${prodInfo.code}|}&quot;&gt;Buy Now&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;

    &lt;br /&gt;
    &lt;div class=&quot;page-navigator&quot;&gt;
        &lt;th:block th&gt;
            &lt;a th:href=&quot;@{|/productList|}&quot;  class=&quot;nav-item&quot;&gt;&lt;/a&gt;
            &lt;span class=&quot;nav-item&quot; &gt; ... &lt;/span&gt;
        &lt;/th:block&gt;
    &lt;/div&gt;
    &lt;th:block th:include=&quot;/_footer&quot;&gt;&lt;/th:block&gt;

&lt;/body&gt;
&lt;/html&gt;

Maven Dependency for Thymleaf

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
&lt;/dependency&gt;

Project Structure Image

无法从Spring Boot控制器渲染Thymeleaf页面。输出中只打印返回字符串。

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.

答案1

得分: 2

这是因为您在控制器的顶部使用了 @RestController 注解,它等同于 @Controller+@ResponseBody。

尝试仅使用 @Controller,然后根据您的方法使用 @ResponseBody。

例如,如果您想返回 JSON 响应主体,则在方法上使用 @ResponseBody。

如果您想渲染 Thymeleaf 页面,则不要使用 @ResponseBody。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月19日 13:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63965406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定