如何在html脚本中调用thymeleaf自定义方言

问题描述 投票:0回答:1

所以基本上我尝试使用 thymeleaf 使用 ld+json 构建动态模式。为了检索相应页面的 url,我们设置了一个自定义处理器:

public class QuackUrlAttributeTagProcessor extends AbstractAttributeTagProcessor {
    private static final String ATTR_NAME = "quackToHref";
    private static final int PRECEDENCE = 10000;

    @Autowired
    private ApplicationPropertyAccess applicationProperties;

    /**
     * Constructs a new processor.
     *
     * @param dialectPrefix the prefix to use if not the standard defined by the dialect
     */
    public QuackUrlAttributeTagProcessor(final String dialectPrefix) {
        super(
            TemplateMode.HTML, // This processor will apply only to HTML mode
            dialectPrefix,     // Prefix to be applied to name for matching
            null,              // No tag name: match any tag name
            false,             // No prefix to be applied to tag name
            ATTR_NAME,         // Name of the attribute that will be matched
            true,              // Apply dialect prefix to attribute name
            PRECEDENCE,        // Precedence (inside dialect's precedence)
            true);             // Remove the matched attribute afterwards
    }

    /**
     * Performs the actual task of the processor. This will use quack data to construct an Url path.
     */
    @Override
    protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag,
            final AttributeName attributeName, final String attributeValue,
            final IElementTagStructureHandler structureHandler) {
        IEngineConfiguration configuration = context.getConfiguration();
        IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
        IStandardExpression expression = parser.parseExpression(context, attributeValue);

        Quack quack = (Quack)expression.execute(context);
        String url = QuackManager.buildUrlPath(quack);

        structureHandler.setAttribute("href", applicationProperties.getFrontendBaseUrl() + url);
    }
}

这样我就可以在 html 模板中添加属性,如下所示:

<html xmlns:th="http://www.thymeleaf.org" xmlns:xc="http://www.example.de">

 <link rel="canonical" xc:quackToHref="${quack}" />

正确设置 href 属性。现在 ld+json 脚本如下所示:

<script type="application/ld+json">

    /*<![CDATA[*/

    {
      "@context": "https://schema.org",
      "@graph": [
        {
          "@type": "Article",
          "url": "[[xc:quackToHref="${quack}"]]",
          "isPartOf": { "@id": "test" },
          "author": {
            "name": "[[${article.nickname}]]",
            "@id": "https://example.de/profile/[[${article.authorId}]]"
          },          
        },
       
        ]
      }

    /*]]>*/

  </script>

什么引发了错误:

Caused by: org.attoparser.ParseException: Could not parse as expression: "xc:quackToHref="${quack}" (template: "articlequack" - line 38, col 22)

显然,无法在脚本内调用自定义处理器。有谁知道如何获取CDATA中的url?

java html json thymeleaf schema.org
1个回答
0
投票

如果您想在文本模板模式中使用属性,语法应如下所示:

[# xc:quackToHref="${quack}" /]
© www.soinside.com 2019 - 2024. All rights reserved.