I have a problem that i would like to get help with!
I am using Intellij Idea and are using Spring MVC with Thymeleaf. I have created a fragment and all is fine and dandy, it works good and gets imported in my current html file.
<div th:replace="fragments/ModalCart :: modal(${products.get(0)})"></div>
This is my import statment and as you can see i am passing a single object of type Product
.
This is my fragment html file
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
</head>
<body>
<div th:fragment="modal(product)">
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<section class="card">
<div class="columns">
<div class="column">
<p th:text="${product.getImgUrl()}"></p>
</div>
</div>
</section>
</div>
</div>
</div>
</body>
</html>
My problem is that my 'product' in the modal fragment is of type Object
not type Product
as i would like to to be (because of that if i call the imgUrl
getter method on the passed in product i get a red underline that tells me it can not be resolved, even doe it works i have a error in my html file which is annoying).
How would i make sure that the parameter that gets pass into this class is of type Product
class and not of java.lang.Object
class?
Is there any way to cast it back to a Product
or am I stuck?
Thanks
Thymeleaf doesn't actually care about the type of the variable passed to the fragment, so ${product.getImgUrl()}
(equivalent to ${product.imgUrl}
) should work whether or not it has red underlines.
As for fixing those problems... this is IDE specific. For example, in Intellij I think something like this should work:
<div th:fragment="modal(product)">
<!--/*@thymesVar id="product" type="your.package.Product"*/-->
Where you replace your.package.Product
with your actual package.