Spring mvc 创造我自己的方言
我将按照Thymeleaf文档创建一个处理器和一个方言,以便在组件未填充时更改其css。若并没有填充,则AddCSS类有错误,但不起作用 我用的是弹簧靴 查找类标记ErrorAttributeProcessorSpring mvc 创造我自己的方言,spring-mvc,spring-boot,thymeleaf,Spring Mvc,Spring Boot,Thymeleaf,我将按照Thymeleaf文档创建一个处理器和一个方言,以便在组件未填充时更改其css。若并没有填充,则AddCSS类有错误,但不起作用 我用的是弹簧靴 查找类标记ErrorAttributeProcessor public class TagErrorAttributeProcessor extends AbstractTextChildModifierAttrProcessor { public TagErrorAttributeProcessor() { supe
public class TagErrorAttributeProcessor extends AbstractTextChildModifierAttrProcessor {
public TagErrorAttributeProcessor() {
super("fielderror");
}
@Override
public int getPrecedence() {
return 12000;
}
@Override
protected String getText(final Arguments arguments, final Element element, final String attributeName) {
final Configuration configuration = arguments.getConfiguration();
// Obtém o analisador de expressões padrão do Thymeleaf
final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
// Analisa o valor do atributo com uma expressão padrão do Thymeleaf
final IStandardExpression expression = parser.parseExpression(configuration, arguments, element.getAttributeValue(attributeName));
if (FieldUtils.hasErrors(arguments, element.getAttributeValue(attributeName))) {
element.setAttribute("class", element.getAttributeValue("class") + " has-error");
}
// Executar expressão apenas para analisar
final String execute = (String) expression.execute(configuration, arguments);
return execute;
}
}
类标记ErrorAttributeDialect
public class TagErrorAttributeDialect extends AbstractDialect {
@Override
public String getPrefix() {
return "rwfield";
}
@Override
public Set<IProcessor> getProcessors() {
final Set<IProcessor> processors = new HashSet<IProcessor>();
processors.add(new TagErrorAttributeProcessor());
return processors;
}
}
public类TagErrorAttributeDialect扩展了抽象方言{
@凌驾
公共字符串getPrefix(){
返回“rwfield”;
}
@凌驾
公共集getProcessors(){
最终设置处理器=新HashSet();
添加(新标记ErrorAttributeProcessor());
返回处理器;
}
}
html
<!DOCTYPE html>
<html lang="pt" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:rwfield="http://filedsErros"
layout:decorator="'model/Layout/default'">
<div class="form-group" rwfield:fielderror="nome">
<label for="LBUsuario">Nome:</label>
<input id="nomUsr" type="text" th:field="*{nome}" class="form-control"/>
</div>
</html>
诺姆: