Search code examples
spring-bootspring-mvcintellij-ideathymeleaf

Why i am unable to launch Thymeleaf Template with springboot application?


I am using IntelliJ IDEA community Edition 2020.1.2 x64 and written a program using Spring DataJPA and Thymeleaf,there is no problem with my code and i am not getting error but when i am passing URL which i mapped in controller class i am unable to get desired result i.e the thymeleaf template which i have created is not displayed on browser.

I am posting the controller class,thymeleaf template and output which i am getting on browser to make you clear:-

controller class:

@RestController
@RequestMapping("/ecommerce")
public class EmployeeController {

    @Autowired
    private ProductService productService;

    @GetMapping("/available")
    public String ShowAllProducts(Model model)
    {
        model.addAttribute("listProducts",productService.getAllProducts());
        return "availableProducts";
    }
}

Thymeleaf template:-

<!DOCTYPE html>
<html xmlns:th="html://thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>AvailableProducts</title>
</head>
<body>
<div align="center">
    <h1>Product List</h1>
    <table>
        <thead>
        <tr>
            <th>p_id</th>
            <th>ProductName</th>
            <th>Productcost</th>
            <th>QuantityAvailable</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="product : ${listProducts}">
            <td th:text="${product.productId}"></td>
            <td th:text="${product.productName}"></td>
            <td th:text="${product.productCost}"></td>
            <td th:text="${product.quanityAvailable}"></td>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>

Output on browser:-

Dependency added:-

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

application.properties:-

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ecommerce
spring.datasource.username=root
spring.datasource.password=deep

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

spring.thymeleaf.prefix=classpath*:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5

At last when i am passing url http://hocalhost:8080/ecommerce/available what i am seeing in console is,hibernate is accessing the database every time.

Thanks in advance.. Please help i am stuck and don't know what i am doing wrong and not able to figure out on my own..


Solution

  • In your case seems like controller is just returning string "availableProducts" and its not resolving as thymleaf template. you can just return some dummy name("availableProductsTest") and you will see same(availableProductsTest) on the web page.

    Did you referred to sample https://spring.io/guides/gs/serving-web-content/

    Sample code is available here .. git clone https://github.com/spring-guides/gs-serving-web-content.git

    if you just add your product class and modify the Greetings contoller like below then you can see its working!

    @GetMapping("/greeting")
        public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
            model.addAttribute("name", name);
            List<Products> productList= new ArrayList<>();
            Products product = new Products("productId","ProductName", "ProductCost", "ProductQtyAvlb");
            productList.add(product);
             model.addAttribute("listProducts",productList);
            return "greeting";
        }
    

    Also modify greetings template as below

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head> 
        <title>Getting Started: Serving Web Content</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p th:text="'Hello, ' + ${name} + '!'" />
        <div align="center">
        <h1>Product List</h1>
        <table>
            <thead>
            <tr>
                <th>p_id</th>
                <th>ProductName</th>
                <th>Productcost</th>
                <th>QuantityAvailable</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="product : ${listProducts}">
                <td th:text="${product.productId}"></td>
                <td th:text="${product.productName}"></td>
                <td th:text="${product.productCost}"></td>
                <td th:text="${product.quanityAvailable}"></td>
            </tr>
            </tbody>
        </table>
    </div>
    </body>
    </html>
    

    enter image description here

    Hope this Helps! Happy Coding

    Web reference: https://spring.io/guides/gs/serving-web-content/

    Whenever you get Whitelabel Error Page you will have details about error listed on same page also same details can be seen in server logs or on console of the application.

    For example if i use wrong template name in controller then i see below:

    enter image description here 2020-07-28 22:35:51.031 ERROR 19572 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "greeting1": Error resolving template [greeting1], template might not exist or might not be accessible by any of the configured Template Resolvers

    org.thymeleaf.exceptions.TemplateInputException: Error resolving template [greeting1], template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]