创造我自己的方言百里香

时间:2016-09-02 00:07:36

标签: spring-mvc spring-boot thymeleaf

我正在按照Thymeleaf文档创建一个处理器和一个方言来改变组件未填充时的css。如果未填充,则添加css类有错误,但无法正常工作。

我正在使用Spring Boot

查看类TagErrorAttributeProcessor

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;
    }
}

类TagErrorAttributeDialect

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;
    }
}

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>

0 个答案:

没有答案