【问题标题】:How to pass multiple parameters from thymeleaf html to thymeleaf dialect processor如何将多个参数从 thymeleaf html 传递到 thymeleaf 方言处理器
【发布时间】:2023-03-15 00:48:01
【问题描述】:

我正在创建一个自定义的百里香方言。我需要知道如何将参数从 thymleaf html 片段传递到 dielect 处理器。我能够实现一个参数,但我需要知道我们将如何实现多个参数。 下面是我的百里香片段。

<th:block dialectPrefix:customDialect="${parameter}"> </th:block>

下面是我的处理器逻辑

protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag,
final AttributeName attributeName, final String attributeValue,
final IElementTagStructureHandler structureHandler) {
  final IEngineConfiguration configuration = context.getConfiguration();
  final IStandardExpression categoryExpression = parser.parseExpression(context, someString);
  final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
  String fetchValue=categoryExpression.execute(context).toString()
   //I am able to get the value of parameter
}

如果我想从 thymeleaf html 传递多个参数,如下所示

<th:block dialectPrefix:customDialect="${parameter1}" "${parameter2} etc">  </th:block>

我可以通过',' 将它分开,就像 html 中的"${parameter1,parameter2}" 一样,它工作正常,但我需要在我的处理器[.java] 级别进行拆分。如果有任何其他方式我们可以在 html 级别实现这对我有帮助。

任何遇到过这种情况的人都可以有所启发。

【问题讨论】:

  • 本机不支持此功能。您可以在code for th:with(它支持多个赋值)中看到它只是在处理表达式并拆分输入本身。
  • 嗨,Metroids 感谢您的输入。您能否将 thymleaf html 代码粘贴给我,以便在 doProcess 方法中解析 A​​ssignationUtils 序列?

标签: java html jsp thymeleaf


【解决方案1】:

可以这样做:

呼叫:

<th:block th:include="mailFragments::test('f1', 'f2', 'f3')"/>

片段:

 <th:block th:fragment="test(p1,p2,p3)">
    <element dialectPrefix:customDialect="|${p1},${p2},${p3}|">тест</element>
 </th:block>

还有:

 new AbstractAttributeTagProcessor(TemplateMode.HTML, PREFIX, null, false, NAME, true, 1000, true) {
    @Override
    protected void doProcess(final ITemplateContext context, final ProcessableElementTag tag, final AttributeName name, String href, final IElementTagStructureHandler structureHandler) {
            final IEngineConfiguration configuration = context.getConfiguration();
            final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
            final IStandardExpression categoryExpression = parser.parseExpression(context, value);
            String delimitedResult = categoryExpression.execute(context).toString();
            String[] params = delimitedResult.split(",");
            .....
    }

PS:自定义百里香标签见Say Hello! Extending Thymeleaf in 5 minutes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2016-03-26
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    相关资源
    最近更新 更多