[英]how to get rid of org.thymeleaf.exceptions.TemplateInputException: while using thymeleaf expression to print data in form of bootstrap cards?
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()
我正在一个小型电子商务网站上工作,spring 引导用于后端目的,并尝试在包含 4 张卡片的行中打印产品名称和成本。为此,我使用
JpaRepository
的findAll()
方法获取所有数据并复制另一个列表中的前三个数据并使用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:当我运行程序时它工作正常,但是当我通过我已经映射的 URL 时,我得到了这个异常:
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/MyEcommerce.html]")
org.thymeleaf.exceptions.TemplateInputException:模板解析时出错(模板:“类路径资源[templates/MyEcommerce.html]”)
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "threeproducts.productName" (template: "MyEcommerce" - line 89, col 40)
引起:org.attoparser.ParseException:评估 SpringEL 表达式的异常:“threeproducts.productName”(模板:“MyEcommerce” - 第 89 行,第 40 列)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "threeproducts.productName" (template: "MyEcommerce" - line 89, col 40)
引起:org.thymeleaf.exceptions.TemplateProcessingException:异常评估 SpringEL 表达式:“threeproducts.productName”(模板:“MyEcommerce” - 第 89 行,第 40 列)
2020-08-10 20:57:52.433 ERROR 1152 --- [nio-8080-exec-4] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;
2020-08-10 20:57:52.433 ERROR 1152 --- [nio-8080-exec-4] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [带有路径 [] 的上下文中的 dispatcherServlet] 引发异常 [请求处理失败; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/MyEcommerce.html]")] with root cause
嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源 [templates/MyEcommerce.html]”)],根本原因
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?
org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“java.lang.String”类型的 object 上找不到属性或字段“productName” - 可能不是公共的或无效的?
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. 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:-我还在下面发布了必要的 class 和 thymeleaf 模板:-
Note : I am posting the part of thymeleaf template which contains code of grid and cards only.注意:我发布的 thymeleaf 模板部分仅包含网格和卡片的代码。
If it is required to post the full code, then definitely I will.如果需要发布完整的代码,那我肯定会的。 And also stackoverflow is giving me word restriction in post
而且stackoverflow还在帖子中给了我字数限制
Controller and service class: Controller 和服务 class:
Model 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 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:服务 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:在最后的 Thymeleaf 模板中:
<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..你的话对我来说很有价值..
In your controller try:在您的 controller 中尝试:
model.addAttribute("ListOfThreeProducts", products);
No quotes around products.产品周围没有报价。 Currently you are putting the string literal 'products' into the model instead of the collection.
目前,您将字符串文字“产品”放入 model 而不是集合中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.