Java Thymeleaf、片段和默认参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22093149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thymeleaf, fragments and default parameters
提问by user3364391
I have created fragments.html file. It contains the following fragment:
我已经创建了 fragment.html 文件。它包含以下片段:
<div th:fragment="my_fragment(content)">
<p th:text="${content}"></p>
</div>
I put the above fragment into my view file:
我把上面的片段放到我的视图文件中:
<div th:replace="fragments :: my_fragment('test')"></div>
Now, I want to pass two parameters to my_fragment, but I must ensure backward compatibility.
现在,我想将两个参数传递给 my_fragment,但我必须确保向后兼容。
I tried to solve the problem as follows:
我试图解决这个问题如下:
<div th:fragment="my_fragment(content, defaultParameter='default value')">
<p th:text="${content}"></p>
</div>
Unfortunelly, the above solution generated error:
不幸的是,上述解决方案产生了错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Cannot resolve fragment. Signature "my_fragment (content,defaultParameter='default value')" declares 2 parameters, but fragment selection specifies 1 parameters. Fragment selection does not correctly match.
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:无法解析片段。签名“my_fragment (content,defaultParameter='default value')”声明了2个参数,但是片段选择指定了1个参数。片段选择不正确匹配。
Any idea?
任何的想法?
回答by Rafal Borowiec
Thymeleaf allows a signature of a fragment without explicit parameters like this:
Thymeleaf 允许在没有显式参数的情况下对片段进行签名,如下所示:
<div th:fragment="my_fragment">
<p th:text="${content}"></p>
<p th:text="${defaultParameter}"></p>
</div>
To call this fragment and pass content
and defaultParameter
you may call the fragment as follows:
要调用此片段并传递content
,defaultParameter
您可以按如下方式调用该片段:
<!-- both parameters not specified -->
<div th:replace="fragments :: my_fragment"></div>
<!-- only content parameter specified -->
<div th:replace="fragments :: my_fragment(content='a')"></div>
<!-- both parameters specified -->
<div th:replace="fragments :: my_fragment(content='a', defaultParameter='b')"></div>
But the below will not work:
但以下将不起作用:
<div th:replace="fragments :: my_fragment('a', 'b')"></div>
And the message is self-explonatory:
该消息是不言自明的:
Signature "my_fragment" declares no parameters, but fragment selection did specify parameters in a synthetic manner (without names), which is not correct due to the fact parameters cannot be assigned names unless signature specifies these names.
So in case you want to maintain backward compatibility, you should use named parameters while calling a fragment and do not specify parameters in a signature of a fragment.
因此,如果您想保持向后兼容性,您应该在调用片段时使用命名参数,并且不要在片段签名中指定参数。
回答by geoff.weatherall
If you want to provide a default value (as in your case) you can use the elvis operator to specify a default value, for example:
如果您想提供默认值(如您的情况),您可以使用 elvis 运算符来指定默认值,例如:
<div th:fragment="my_fragment">
<p th:text="${content}"></p>
<p th:text="${defaultParameter ?: 'the backwards compat value'}"></p>
</div>
see: elvis-operator
回答by Grigory Kislin
At last I've solved task like this:
最后我解决了这样的任务:
<div th:fragment="my_fragment2(content, param2)">
<th:block th:replace="my_fragment(${content})"/>
</div>
<div th:fragment="my_fragment(content)" th:with="param2=${param2} ?: 'default value'">
<p th:text="${content}"></p>
<p th:text="${param2}"></p>
</div>
<div th:replace="fragments :: my_fragment2('test', 'my_value')"></div>
But it is overhead:(
但这是开销:(
Also ask a question to issue Allow fragment parameters to have default values
回答by snap
The best way to allow optional parameter for a fragment is to declare them with "th:with" and describe them with meaningful default values.
允许片段的可选参数的最佳方法是用“th:with”声明它们并用有意义的默认值描述它们。
So, you define explicit your mandatory and your optional values in the declaration tag of your fragment.
因此,您可以在片段的声明标签中明确定义强制性和可选值。
Here is simple example, with 1 mandatory and 2 optional parameters:
这是一个简单的例子,有 1 个强制参数和 2 个可选参数:
<div th:fragment="printGreetings (name)" th:with="title=${title} ?: 'Mr.', greeting=${greeting} ?: 'Hello'">
<span th:text="${greeting}">Hello</span>
<span th:text="${title}">Mr.</span>
<span th:text="${name}">John Doe</span>
</div>
You can then call it like the following:
然后你可以像下面这样调用它:
<div th:replace="fragments :: printGreetings (name='daniel')">
Hello Mr. Daniel
</div>
<div th:replace="fragments :: printGreetings (name='Lea', title='Ms.')>
Hello Ms. Lea
</div>
<div th:replace="fragments :: printGreetings (name='Lea', title='Ms.', greeting='Hi')>
Hi Ms. Lea
</div>
(Please note that all values inside the tags are replaced by the dynamic ones. It's just for better reading.)
(请注意,标签内的所有值都被动态值替换。只是为了更好地阅读。)