英文:
how to get rid of org.thymeleaf.exceptions.TemplateInputException: while using thymeleaf expression to print data in form of bootstrap cards?
问题
我正在开发一个小型电子商务网站,使用Spring Boot作为后端,并尝试在包含4个卡片的行中打印产品名称和成本。为此,我使用了JpaRepository
的findAll()
方法获取了所有数据,并将前三个数据复制到另一个列表中,然后使用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 = "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 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 class:
@Service
public class CustomerServices {
@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..
答案1
得分: 0
在你的控制器中尝试:
model.addAttribute("ListOfThreeProducts", products);
在products周围不要加引号。当前你正在将字符串字面量 'products' 放入模型中,而不是集合。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论