所以基本上我尝试使用ld+json通过thymeleaf构建一个动态模式。为了检索相应页面的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);
}
}
字符串
习惯方言:
public class XContDialect extends AbstractProcessorDialect implements ApplicationContextAware {
private ApplicationContext applicationContext;
/**
* Constructor that will define the name of the Dialect.
*/
public XConDialect() {
super("XConsult Dialect", "xc", 1000);
}
/**
* Initialise the dialect's processors.
*
* Note the dialect prefix is passed here because, although "xc" is set to be the dialect's prefix in the
* constructor, that only works as a default, and at engine configuration time the user might have chosen
* a different prefix to be used.
*/
public Set<IProcessor> getProcessors(final String dialectPrefix) {
final Set<IProcessor> processors = new HashSet<IProcessor>();
QuackUrlAttributeTagProcessor processor = new QuackUrlAttributeTagProcessor(dialectPrefix);
AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
factory.autowireBean(processor);
processors.add(processor);
return processors;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
型
这样我就可以在html模板中添加属性,如下所示:
<html xmlns:th="http://www.thymeleaf.org" xmlns:xc="http://www.example.de">
<link rel="canonical" xc:quackToHref="${quack}" />
型
这会正确设置href-attribute。现在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吗?
3条答案
按热度按时间fumotvh31#
如果要在文本模板模式中使用属性,语法应该如下所示:
字符串
wf82jlnq2#
试试这个
字符串
[[...]]
或${...}.
试试这个,让我知道如果没有解决的评论在这里..
mefy6pfw3#
要在HTML模板中使用自定义Thymeleaf方言,您需要在项目中包含方言,配置它,然后在HTML文件中使用它。以下是在HTML脚本中调用自定义Thymeleaf方言的步骤:
org.thymeleaf.dialect.IDialect
接口来创建一个自定义的Thymeleaf方言。这涉及到定义你自己的处理器、属性或方言将处理的标签。application.properties
或application.yml
文件中使用自定义方言配置Thymeleaf的示例:字符串
在本例中,通过指定自定义方言的包和类名来配置Thymeleaf方言。
型
将
http://www.example.com/thymeleaf-custom-dialect
替换为自定义方言的命名空间URI。确保自定义方言在应用程序中正确实现并注册,以使其按预期工作。此外,确保在HTML模板中使用正确的命名空间和语法来调用自定义方言的功能。