当前位置: 首页 > 知识库问答 >
问题:

如何摆脱org.thymeleaf.exceptions.TemplateInputExc0019:同时使用thymeleaf表达式打印数据形式的自举卡?

扈高逸
2023-03-14

我在一个小型电子商务网站上工作,后台使用spring boot,并试图打印包含4张卡片的产品名称和成本。为此,我使用JpaRepository的findAll()方法获取所有数据,并将前三个数据复制到另一个列表中,并使用模型传递到网页。addAttribute()

当我运行的程序,它的工作原理很好,但当我传递的URL,我已经映射我得到这个异常:

org.thymeleaf.exceptions.TemplateInputExc0019:模板解析过程中发生错误(模板:"类路径资源[模板/MyEcommerce.html]")

引起:org.attoparser.ParseExc0019:异常评估SpringEL表达式:threeproducts.product名称(模板:MyECommerce-第89行,第40行)

原因:org。百里香。例外情况。TemplateProcessingException:异常评估SpringEL表达式:“threeproducts.productName”(模板:“MyEcommerce”-第89行,第40列)

2020-08-10 20:57:52.433错误1152---[nio-8080-exec-4]o.a.c.c.c.c。[.[.[/].[dispatcherServlet]:Servlet。路径为[]的上下文中servlet[dispatcherServlet]的服务()引发异常[请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源[templates/MyEcommerce.html]”),其根本原因是

org.springframework.expression.spel.SpelEvalue ation异常: EL1008E:在类型为java.lang.String的对象上找不到属性或字段productName-可能不是公共的或无效的?

我所知道的是,如果模型类不包含我们试图在thymeleaf表达式中访问的属性,但在我的模型类属性中,我们会得到这个异常。

我还在下面发布了必要的课程和课程模板:-

注意:我张贴的thymeleaf模板的部分只包含网格和卡片的代码。

如果需要发布完整的代码,那么我肯定会。而且stackoverflow在帖子中给了我字数限制

控制器和服务级别:

模型类:

@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
@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();
       /* products.stream().forEach((productTemp) -> listofthreeproducts.add(productTemp));*/
        listofthreeproducts=products.subList(0,4);
        return listofthreeproducts;
    }
}

在最后的胸腺叶模板中:

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

提前感谢...

你的话对我很有价值。。


共有1个答案

马臻
2023-03-14

在您的控制器中尝试:

model.addAttribute("ListOfThreeProducts", products);

产品周围没有报价。目前,您正在将字符串文字“产品”放入模型中,而不是集合中。

 类似资料:
  • 我试图在iReport中实现一个简单的表达式,但无论我尝试什么,似乎都给了我一个错误。这似乎是一件很简单的事情,但每当我尝试用Java做任何事情时,我都需要大约20次尝试才能把它做好。 我所要做的就是在整数字段的值不为零时显示子报告。我正在对子报告控件使用打印时表达式。 以下是我尝试过的许多事情中的一些: 显然我的Java很烂。任何帮助都将不胜感激。谢谢

  • 问题内容: 我有一个对象列表,我想在一个漂亮的表中打印每个对象的参数。 我的代码在这里: 我想要得到的输出是 问题答案: for attr in (‘thing’, ‘owner’, ‘color’): for item in bin: print ‘%-10s’%getattr(item, attr), print 使用列表理解更紧凑

  • 下面的代码运行良好。它根据LinkedIn上的列表收集信息。 (提供帐户信息并免费使用,因为它是测试帐户) 但是,输出连接数据,而不是每个字段都有自己的字段。

  • 你已经看过一些MATLAB表达式的例子,下面再举一些例子和运算结果。 rho = (1+sqrt(5))/2 rho = 1.6180 a = abs(3+4i) a = 5 z = sqrt(besselk(4/3,rho-i)) z = 0.3730+ 0.3214i huge = exp(log(realmax)) huge = 1.7977e+308 t

  • 问题内容: 到目前为止,给我带来了很多麻烦,所以我想摆脱它。尽管spring框架文档清楚地说明了应该做的事情,但实际上 并没有摘要列表。 所以我一直坚持删除并得到错误 -在名称为的中找不到带有请求的映射 对于所有应该由控制器类解决的Url(在这种情况下:)。有什么建议可以让我了解更多信息吗?我非常想知道到底由代表什么标签。 问题答案: 你可以用来自定义定义的每个bean 。现在,javadocs详

  • 问题内容: 到现在为止,给我造成了很多麻烦,所以我想摆脱它。尽管spring框架文档清楚地说明了应该做的事情,但实际上 并没有摘要列表。 所以我坚持删除并现在得到错误 WARN osweb.servlet.PageNotFound-在DispatcherServlet中,名称为’workoutsensor’的URI [/ webapp / trainees]的HTTP请求未找到映射 对于所有应该由