无法从Spring Boot控制器渲染thymeleaf页面。

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

Unable to render thymleaf page from Spring-Boot controller

问题

问题: 我开发了一个Spring Boot的REST API,我可以从POSTMAN调用它。现在我想从一个Thymeleaf页面中请求相同的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";
    }
}

这是我的Thymeleaf页面:

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

Thymeleaf的Maven依赖:

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

项目结构图像:

无法从Spring Boot控制器渲染thymeleaf页面。

输出:

调用URL: http://localhost:1000/shopping/productList
它在页面上只返回字符串 productList

请告诉我我做错了什么。我可以渲染一个index.html页面,但不能渲染这个页面。

更新: 我提供了我正在使用的Maven依赖以及我Rest Controller类上使用的注解。

英文:

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 :

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

UPDATE : I have provided the Maven dependency I am using and the Annotations used over my Rest Controller class.

答案1

得分: 1

  • 我相信您要么

  • 缺少spring-boot-starter-thymeleaf依赖项。此依赖项包含必要的依赖项和Thymeleaf模板解析器的自动配置(将返回类型字符串转换为HTML模板名称)以及实际渲染。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  • 或者在您的控制器类中使用 @Controller 而不是 @RestController。@RestController 默认返回 JSON 响应。要进行模板渲染,您应该使用 @Controller。
英文:

I believe you are either

  • missing spring-boot-starter-thymeleaf dependency. This dependency contains necessary dependency and auto configuration for thymeleaf template resolver(return type string to html template name) and actual rendering.
    &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;
  • OR using @Controller instead of @RestController in your controller class. @RestController returns json response by default. For template rendering you should use @Controller.

答案2

得分: 0

使用@Controller代替@RestController解决了这个问题。现在可以成功渲染HTML页面。

英文:

Answer :

Using @Controller instead of @RestController resolved the issue. Able to render the html page now.

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

发表评论

匿名网友

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

确定