java - Nested Thymeleaf Templates in Spring -
short version
how 1 supposed make nested templates in thymeleaf when using spring? appears asterisk notation not supported ("*{mailingaddress}"
) inside th:object
attributes in spring. there work-around / different tag use?
long version
for example, let's have these classes:
class address { string street; } class person { address mailingaddress; address shippingaddress; } class order { int orderno; person customer; }
so make address.html
thymeleaf template:
<span th:text="*{street}"></span>
we test sample address
. looks good.
and make person.html
thymeleaf template references address so:
<span th:text="*{firstname}"></span> <span th:object="${person.shippingaddress}"> <span th:include="fragments/address :: address"></span> </span>
and test example person. reference same template , set context ${person.mailingaddress}
. far good.
now let's make our order
template. only, hey, wait. earlier, in our person.html
file said ${person.shippingaddress}
now need ${order.customer.shippingaddress}
. if not using spring i'd put following person.html
:
<span th:text="*{firstname}"></span> <span th:object="*{shippingaddress}"> <span th:include="fragments/address :: address"></span> </span>
that way, no matter path getting here have care current context has shippingaddress
. use person.html
directly within order.html
template.
unfortunately am in spring, following exception:
org.thymeleaf.exceptions.templateprocessingexception: expression used object selection *{shippingaddress}, not valid: variable expressions (${...}) allowed in 'th:object' attributes in spring-enabled environments. (include:510) @ org.thymeleaf.spring4.processor.attr.springobjectattrprocessor.validateselectionvalue(springobjectattrprocessor.java:73) @ org.thymeleaf.standard.processor.attr.abstractstandardselectionattrprocessor.getnewselectiontarget(abstractstandardselectionattrprocessor.java:69) @ org.thymeleaf.processor.attr.abstractselectiontargetattrprocessor.processattribute(abstractselectiontargetattrprocessor.java:61)
to move forward must duplicate nested templates. in example, have 1 person.html
th:object="${person.mailingaddress}"
calling address.html
, , duplicate of person.html
called ordercustomer.html
change line th:object="${order.customer.mailingaddress}"
, otherwise identical.
is there work-around out there let me re-use templates?
you can report bug thymeleaf developers in github, or fork project add functionality , convince daniel fernández accept it.
https://github.com/thymeleaf/thymeleaf/issues
or else, available in stackoverflow. can send him message posibility of integrating functionality
https://stackoverflow.com/users/550664/daniel-fern%c3%a1ndez
apart there nothing can rather stick approach of putting th:object="${person.mailingaddress}"
, th:object="${order.customer.mailingaddress}"
outside each import.
Comments
Post a Comment