Java Thymeleaf、片段和默认参数

Java Thymeleaf、片段和默认参数,java,spring,spring-mvc,thymeleaf,Java,Spring,Spring Mvc,Thymeleaf,我已经创建了fragments.html文件。它包含以下片段: 我将上述片段放入我的视图文件中: 现在,我想将两个参数传递给我的_片段,但我必须确保向后兼容性 我试着解决这个问题如下: <!-- both parameters not specified --> <div th:replace="fragments :: my_fragment"></div> <!-- only content parameter specified --&g

我已经创建了fragments.html文件。它包含以下片段:


我将上述片段放入我的视图文件中:


现在,我想将两个参数传递给我的_片段,但我必须确保向后兼容性

我试着解决这个问题如下:

<!-- 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>

不幸的是,上述解决方案产生了错误:

org.springframework.web.util.NestedServletException:请求 处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:无法解析 碎片。签名“my_片段(内容,defaultParameter='default value')” 声明2个参数,但片段选择指定1个参数。 片段选择不正确匹配


有什么想法吗?

Thymeleaf允许在没有如下明确参数的情况下对片段进行签名:

<div th:fragment="my_fragment">
    <p th:text="${content}"></p>
    <p th:text="${defaultParameter}"></p>
</div>
<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>
但以下方法行不通:

<div th:replace="fragments :: my_fragment('a', 'b')"></div>

因此,如果您希望保持向后兼容性,则在调用片段时应使用命名参数,而不要在片段的签名中指定参数。

如果您希望提供默认值(如您的情况),则可以使用elvis运算符指定默认值,例如:



请看:

我终于解决了这样的任务:

<div th:fragment="my_fragment">
    <p th:text="${content}"></p>
    <p th:text="${defaultParameter}"></p>
</div>
<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>

但这是间接的:(


还可以问一个问题来发布

允许片段的可选参数的最佳方法是使用“th:with”声明它们,并使用有意义的默认值描述它们

因此,在片段的声明标记中定义显式的强制值和可选值

下面是一个简单的示例,其中包含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>

你好
先生
无名氏
然后,您可以按如下方式调用它:

<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>

你好,丹尼尔先生
你好,李女士
(请注意,标记中的所有值都被动态值替换。这只是为了更好地阅读。)

对我来说最好的方法是:

片段:

<div th:fragment="my_fragment(content)">
    <p th:text="${content}"></p>
</div>

如果模型中给出了内容:


是否可以为

所以如果没有输入参数,它不会变成空的?@perak是的,它是通过在片段声明标记中使用th:with来实现的。我的答案中提供了一个示例。在传递变量而不是文本时,必须记住的是将这些变量包装在
${}
例如,
是否有任何语法可用于消息资源?我尝试了以下几种样式,但都失败了。