但是我想使用标准 Thymeleaf 表达-6ren">
gpt4 book ai didi

spring - Thymeleaf 自定义方言与标准表达

转载 作者:行者123 更新时间:2023-12-02 20:34:53 25 4
gpt4 key购买 nike

我创建了一个自定义 Thymeleaf 3 方言,如果我仅将它与如下所示的静态属性值一起使用,它就可以正常工作:

<formBuilder:inputRow title="Email:" name="email" value="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86f2e3f5f2c6f2e3f5f2a8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>" />

但是我想使用标准 Thymeleaf 表达式来填充 value 属性,但如果我这样写:

<formBuilder:inputRow title="Email:" name="email" value="${user.email}" />

输出将是文本${user.email},而不是真实的电子邮件地址。

如果我像这样使用 th:value:

<formBuilder:inputRow title="Email:" name="email" th:value="${user.email}" />

那么根本不会生成任何值字段。

方言类如下。它只注册一个 elementTag 处理器:

public class FormBuilderDialect extends AbstractProcessorDialect {

private static final String DIALECT_NAME = "Form Builder Dialect";

public FormBuilderDialect() {
super(DIALECT_NAME, "formBuilder", StandardDialect.PROCESSOR_PRECEDENCE);
}

@Override
public Set<IProcessor> getProcessors(String dialectPrefix) {
final Set<IProcessor> processors = new HashSet<IProcessor>();
processors.add(new InputElementTagProcessor(dialectPrefix));
return processors;
}
}

元素处理器是:

public class InputElementTagProcessor extends AbstractElementTagProcessor {

private static final String TAG_NAME = "inputRow";
private static final int PRECEDENCE = 1000;

public InputElementTagProcessor(final String dialectPrefix) {
super(
TemplateMode.HTML, // This processor will apply only to HTML mode
dialectPrefix, // Prefix to be applied to name for matching
TAG_NAME, // Tag name: match specifically this tag
true, // Apply dialect prefix to tag name
null, // No attribute name: will match by tag name
false, // No prefix to be applied to attribute name
PRECEDENCE); // Precedence (inside dialect's own precedence)
}

@Override
protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {

final String name = tag.getAttributeValue("name");
final String value = tag.getAttributeValue("value");

final IModelFactory modelFactory = context.getModelFactory();

final IModel model = modelFactory.createModel();

// Add input
Map<String, String> inputAttributes = new HashMap<>();
inputAttributes.put("name", name);
inputAttributes.put("value", value);
model.add(modelFactory.createStandaloneElementTag("input", inputAttributes, AttributeValueQuotes.DOUBLE, false, false));

structureHandler.replaceWith(model, false);
}
}

知道如何解决这个问题吗?

最佳答案

为了解决这个问题,我必须创建一个属性标签处理器,如下所示:

public class FormBuilderAttributeTagProcessor extends AbstractAttributeTagProcessor {

private static final String ATTR_NAME = "value";
private static final int PRECEDENCE = 100;

public FormBuilderAttributeTagProcessor(final String dialectPrefix) {
super(
TemplateMode.HTML,
dialectPrefix,
"inputRow",
true,
ATTR_NAME,
false,
PRECEDENCE,
false);
}

@Override
protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) {
final IEngineConfiguration configuration = context.getConfiguration();

final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);

final IStandardExpression expression = parser.parseExpression(context, attributeValue);

final String result = (String) expression.execute(context);

structureHandler.setAttribute("value", result);
}
}

并将其注册为方言类中的处理器:

public class FormBuilderDialect extends AbstractProcessorDialect {

private static final String DIALECT_NAME = "Form Builder Dialect";

public FormBuilderDialect() {
super(DIALECT_NAME, "formBuilder", StandardDialect.PROCESSOR_PRECEDENCE);
}

@Override
public Set<IProcessor> getProcessors(String dialectPrefix) {
final Set<IProcessor> processors = new HashSet<IProcessor>();
processors.add(new InputElementTagProcessor(dialectPrefix));
processors.add(new FormBuilderAttributeTagProcessor(dialectPrefix));
return processors;
}
}

关于spring - Thymeleaf 自定义方言与标准表达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47477277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com