Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何将特定对象传递给片段(thymelaf)_Spring_Spring Boot_Spring Mvc_Thymeleaf - Fatal编程技术网

Spring 如何将特定对象传递给片段(thymelaf)

Spring 如何将特定对象传递给片段(thymelaf),spring,spring-boot,spring-mvc,thymeleaf,Spring,Spring Boot,Spring Mvc,Thymeleaf,我有一个问题,我想得到帮助 我正在使用Intellij Idea,并将Spring MVC与Thymeleaf结合使用。我已经创建了一个片段,一切都很好,很漂亮,它运行良好,并被导入到我当前的html文件中 <div th:replace="fragments/ModalCart :: modal(${products.get(0)})"></div> 我的问题是,模态片段中我的“产品”是Object类型,而不是我希望的product类型(正因为如此

我有一个问题,我想得到帮助

我正在使用Intellij Idea,并将Spring MVC与Thymeleaf结合使用。我已经创建了一个片段,一切都很好,很漂亮,它运行良好,并被导入到我当前的html文件中

<div th:replace="fragments/ModalCart :: modal(${products.get(0)})"></div>
我的问题是,模态片段中我的“产品”是
Object
类型,而不是我希望的
product
类型(正因为如此,如果我在传入的产品上调用
imgUrl
getter方法,我会得到一条红色下划线,告诉我它无法解决,即使它可以工作,我的html文件中也有一个错误,这很烦人)。 如何确保传入此类的参数的类型为
Product
class,而不是
java.lang.Object
class

有没有办法把它扔回到
产品上
或者我被卡住了


感谢

Thymeleaf实际上并不关心传递给片段的变量的类型,因此无论是否有红色下划线,
${product.getImgUrl()}
(相当于
${product.imgUrl}
)都应该工作

至于解决这些问题……这是特定于IDE的。例如,在Intellij中,我认为类似的东西应该可以工作:

<div th:fragment="modal(product)">
  <!--/*@thymesVar id="product" type="your.package.Product"*/-->


用实际的包替换
您的.package.Product

变量提供给带有
org.springframework.ui.Model
的模板,该模板是
对象
存储的
字符串。我想一旦您将变量添加到模型中,它的类型就变得无关紧要了。
<div th:fragment="modal(product)">
  <!--/*@thymesVar id="product" type="your.package.Product"*/-->