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

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

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

问题

我正在开发一个小型电子商务网站,使用Spring Boot作为后端,并尝试在包含4个卡片的行中打印产品名称和成本。为此,我使用了JpaRepositoryfindAll()方法获取了所有数据,并将前三个数据复制到另一个列表中,然后使用model.addAttribute()将其传递到网页上。

当我运行程序时,一切都正常,但当我传递我映射的URL时,我收到了这个异常:

org.thymeleaf.exceptions.TemplateInputException: 模板解析期间发生错误(模板:"class path resource [templates/MyEcommerce.html]")
    
Caused by: org.attoparser.ParseException: 在评估SpringEL表达式时发生异常:"threeproducts.productName"(模板:"MyEcommerce" - 第89行,第40列)
    
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: 在评估SpringEL表达式时发生异常:"threeproducts.productName"(模板:"MyEcommerce" - 第89行,第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: 模板解析期间发生错误(模板:"class path resource [templates/MyEcommerce.html]")] with root cause
    
org.springframework.expression.spel.SpelEvaluationException: EL1008E: 属性或字段'productName'无法在类型为'java.lang.String'的对象上找到 - 可能不是公共的或无效的?

根据我了解的情况,如果模型类不包含我们在Thymeleaf表达式中尝试访问的属性,则会出现这个异常,但在我的模型类中确实存在该属性。

我也会在下面贴出必要的类和Thymeleaf模板:(注意:我只贴出包含网格和卡片代码的部分。如果需要贴出完整代码,我会提供。)

控制器和服务类:

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

    @Autowired
    private CustomerServices 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
public class CustomerServices {

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

    public List<Products> ReturnThreeProducts()
    {
        List<Products> products = customerRepository.findAll();
        listofthreeproducts = products.subList(0, 4);
        return listofthreeproducts;
    }
}

最后是Thymeleaf模板的部分:

<body>
<!-- 导航栏和jumbotron的代码在这里,下面是卡片的代码,忽略图片 -->
<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>
</div>
</body>

提前感谢您的帮助。您的建议对我非常重要。

英文:

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 this exception:

> 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 below:-

Note: 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 = &quot;products&quot;)
public class Products {

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

Controller class:

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

    @Autowired
    private CustomerServices customerServices;

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

Service class:

@Service
public class CustomerServices {

    @Autowired
    private CustomerRepository customerRepository;
    public List&lt;Products&gt; listofthreeproducts;

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

And in Last Thymeleaf template:

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

Thanks in advance...

Your words are value for me..

答案1

得分: 0

在你的控制器中尝试:

model.addAttribute("ListOfThreeProducts", products);

在products周围不要加引号。当前你正在将字符串字面量 'products' 放入模型中,而不是集合。
英文:

In your controller try:

model.addAttribute(&quot;ListOfThreeProducts&quot;, products);

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

huangapple
  • 本文由 发表于 2020年8月10日 23:33:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63343304.html
匿名

发表评论

匿名网友

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

确定