how to get rid of org.thymeleaf.exceptions.TemplateInputException: while using thymeleaf expression to print data in form of bootstrap cards?

Atharva :

I am working on a small E-commerce website,with spring boot for back-end purpose and trying to print product name and cost in row containing 4 cards.For this I am fetching all data using findAll() methods of JpaRepository and copied the first three data in another list and passed to webpage using model.addAttribute()

When i am running the program it works fine but when i am passing the URL which i have mapped I am getting Exceptions bellow:-

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/MyEcommerce.html]")

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "threeproducts.productName" (template: "MyEcommerce" - line 89, col 40)

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "threeproducts.productName" (template: "MyEcommerce" - line 89, col 40)

2020-08-10 20:57:52.433 ERROR 1152 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/MyEcommerce.html]")] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'productName' cannot be found on object of type 'java.lang.String' - maybe not public or not valid?

What i know is we get this exception if model class not contain the property which we are trying to access in thymeleaf expression but in my model class property is there.

I am also posting necessary class and thymeleaf template bellow:-

Note:- Below i am posting the part of thymeleaf template which contains code of grid and cards only.
If it is required to post the full code,then definitely i will. And also stackoverflow is giving me word restriction in post

Controller and service class:-

Model Class:-

@Entity
@Table(name = "products")
public class Products {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int productId;
    private String productName;
    private String productCost;
    private String quantityAvailable;
}

Controller class:-

@Controller
@RequestMapping("/www.shopping.com")           /*Just For Fun*/
public class CustomerController {

    @Autowired
    private CustommerServices customerServices;

    /*controller to show index page of website to customers*/
    @GetMapping("/IndexPage")
    public String ViewIndexPage(Model model)
    {
        List<Products> products= customerServices.ReturnThreeProducts();
        model.addAttribute("ListOfThreeProducts","products");
        return "MyEcommerce";
    }
}

Service Class:-

@Service
public class CustommerServices {

    @Autowired
    private CustomerRepository customerRepository;
    public List<Products> listofthreeproducts;

    public List<Products> ReturnThreeProducts()
    {
        List<Products> products=customerRepository.findAll();
       /* products.stream().forEach((productTemp) -> listofthreeproducts.add(productTemp));*/
        listofthreeproducts=products.subList(0,4);
        return listofthreeproducts;
    }
}

And in Last Thymeleaf Template:-

<body>
<--Code for navbar and jambotron upto here and below is of cards ignore 
images-->
<div class="container" th:each="threeproducts : ${ListOfThreeProducts}">
<div class="row">
    <div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap" height="155">
            <div class="card-body">
                <h6 class="card-title" th:text = ${threeproducts.productName}></h6>
                <h6 class="card-text" th:text: $threeproducts.productCost></h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>
    <!--<div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
                <h6 class="card-title">Card title</h6>
                <h6 class="card-text">Price</h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>
    <div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
                <h6 class="card-title">Card title</h6>
                <h6 class="card-text">Price</h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>
    <div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
                <h6 class="card-title">Card title</h6>
                <h6 class="card-text">Price</h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>-->
</div>
</div>
</body>

Thanks in advance... Your words are value for me..

Lee Greiner :

In your controller try:

model.addAttribute("ListOfThreeProducts", products);

No quotes around products. Currently you are putting the string literal 'products' into the model instead of the collection.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Thymeleaf : org.thymeleaf.exceptions.TemplateInputException

Thymeleaf: org.thymeleaf.exceptions.TemplateInputException

Thyemleaf nested iteration triggers org.thymeleaf.exceptions.TemplateInputException

Getting org.thymeleaf.exceptions.TemplateInputException: Error resolving template

issue with springboot project : org.thymeleaf.exceptions.TemplateInputException

My spring-boot app gives the following error "org.thymeleaf.exceptions.TemplateInputException:"

org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="login", line 36 - column 3

org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: "${content}": template or fragment could not be resolved (template: "base"

Thymeleaf Could not bind form errors using expression "*"

how to initialize bootstrap data table on thymeleaf fragment

How to post thymeleaf form data to Controller

thymeleaf not allowing using `&&` in form

Spring Boot Testing Thymeleaf org.thymeleaf.exceptions.TemplateProcessingException

org.thymeleaf.exceptions.TemplateProcessingException: Concatenation href

Error while rendering HTML template using Thymeleaf 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "ServiceAmc.id" (results:30)

How to get data from html with thymeleaf?

How can I populate value at form inside modal using Bootstrap and thymeleaf?

Thymeleaf + Spring: get rid of the default element id

How to get HttpServletRequest Attribute in Thymeleaf using jQuery?

How to grab textarea data using thymeleaf

How to print Array size in thymeleaf?

Using thymeleaf value in bootstrap alert

Using thymeleaf to post form data to a Controller that uses @ModelAttribute (complex objects)

How to print mensagens in the source code of the page using thymeleaf?

What does this expression mean in thymeleaf form validation?

How to get a Token in SpringBoot form external Site: Thymeleaf Error

How to get tinymce interface to appear in thymeleaf form textarea?

How to get rid of urls while using TfidfVectorizer