简体   繁体   English

Spring 中的嵌套 Thymeleaf 模板

[英]Nested Thymeleaf Templates in Spring

Short version精简版

How is one supposed to make nested templates in Thymeleaf when using Spring?使用 Spring 时,如何在 Thymeleaf 中制作嵌套模板? It appears asterisk notation is not supported ( "*{mailingAddress}" ) inside th:object attributes in Spring.在 Spring 中的th:object属性中似乎不支持星号表示法"*{mailingAddress}" )。 Is there a work-around / different tag to use?是否有解决方法/不同的标签可供使用?

Long version长版

For example, let's say I have these classes:例如,假设我有这些类:

class Address { String street; }
class Person { Address mailingAddress; Address shippingAddress; }
class Order { int orderNo; Person customer; } 

So I make an address.html Thymeleaf template:所以我做了一个address.html Thymeleaf 模板:

<span th:text="*{street}"></span>

We test it with a sample Address .我们使用示例Address对其进行测试。 Looks good.看起来挺好的。

and I make a person.html Thymeleaf template that references the address like so:我制作了一个person.html Thymeleaf 模板,引用地址如下:

<span th:text="*{firstName}"></span>
<span th:object="${person.shippingAddress}">
    <span th:include="fragments/address :: address"></span>
</span>

And we test it with an example person.我们用一个例子来测试它。 I could even reference the same template and set the context to be the ${person.mailingAddress} .我什至可以引用相同的模板并将上下文设置为${person.mailingAddress} So far so good.到现在为止还挺好。

Now let's make our Order template.现在让我们制作Order模板。 Only, hey, wait.只是,嘿,等等。 Earlier, in our person.html file we said ${person.shippingAddress} but now we need it to say ${order.customer.shippingAddress} .早些时候,在我们的person.html文件中,我们说${person.shippingAddress}现在我们需要说${order.customer.shippingAddress} If I were not using Spring I'd put the following into person.html : 如果我不使用 Spring,我会将以下内容放入person.html

<span th:text="*{firstName}"></span>
<span th:object="*{shippingAddress}">
    <span th:include="fragments/address :: address"></span>
</span>

That way, no matter what my path to getting here all I have to care about is that my current context has a shippingAddress .这样,无论我到达这里的路径如何,我都需要关心的是我当前的上下文有一个shippingAddress I could then use person.html directly as well as within my order.html template.然后我可以直接使用person.html以及在我的order.html模板中。

Unfortunately I am in Spring, so I get the following exception:不幸的是在春天,所以我得到以下异常:

org.thymeleaf.exceptions.TemplateProcessingException: 
    The expression used for object selection is *{shippingAddress},
    which is not valid: only variable expressions (${...}) are
    allowed in 'th:object' attributes in Spring-enabled environments. 
    (include:510)
at org.thymeleaf.spring4.processor.attr.SpringObjectAttrProcessor.validateSelectionValue(SpringObjectAttrProcessor.java:73)
at org.thymeleaf.standard.processor.attr.AbstractStandardSelectionAttrProcessor.getNewSelectionTarget(AbstractStandardSelectionAttrProcessor.java:69)
at org.thymeleaf.processor.attr.AbstractSelectionTargetAttrProcessor.processAttribute(AbstractSelectionTargetAttrProcessor.java:61)

To move forward I must duplicate all my nested templates.为了继续前进,我必须复制所有嵌套的模板。 In this example, I would have one person.html with th:object="${person.mailingAddress}" calling to address.html , and a duplicate of person.html called orderCustomer.html where we change the line to th:object="${order.customer.mailingAddress}" , but is otherwise identical.在这个例子中,我将有一个person.htmlth:object="${person.mailingAddress}"调用address.html ,以及一个名为orderCustomer.htmlperson.html orderCustomer.html ,我们将行更改为th:object="${order.customer.mailingAddress}" ,但其他方面相同。

Is there a work-around out there that would let me re-use templates?是否有解决方法可以让我重用模板?

You can report a bug to the thymeleaf developers in github, or fork the project to add this functionality and convince the Daniel Fernández to accept it.您可以在 github 中向 thymeleaf 开发人员报告错误,或者 fork 项目以添加此功能并说服 Daniel Fernández 接受它。

https://github.com/thymeleaf/thymeleaf/issues https://github.com/thymeleaf/thymeleaf/issues

Or else, he is available in StackOverflow.或者,他可以在 StackOverflow 中找到。 You can simply send him a message about the posibility of integrating this functionality您可以简单地向他发送有关集成此功能的可能性的消息

https://stackoverflow.com/users/550664/daniel-fern%C3%A1ndezhttps://stackoverflow.com/users/550664/daniel-fern%C3%A1ndez

apart from that there is nothing much we can do rather to stick with the approach of putting th:object="${person.mailingAddress}" and th:object="${order.customer.mailingAddress}" outside each import.除此之外,我们无能为力,而是坚持将th:object="${person.mailingAddress}"th:object="${order.customer.mailingAddress}"放在每个导入之外的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM