【发布时间】:2014-12-12 14:03:34
【问题描述】:
在我当前的 spring-boot 项目中,我有以下 thymeleaf 处理器:
public class Form extends AbstractProcessor {
public static Element form = new Element("form");
@Override
public ProcessorResult doProcess(Arguments arguments,ProcessorMatchingContext context,Node node) {
form.setProcessable(true);
form.setAttribute("role", "form");
form.setAttribute("class", "form");
form.setAttribute("action", "");
form.setAttribute("method", "post");
node.getParent().insertBefore(node, form);
List<Element> lista = node.getParent().getElementChildren();
for(Element child : lista) {
if(!child.getOriginalName().equals("form")) {
child.moveAllChildren(form);
}
}
node.getParent().removeChild(node);
return ProcessorResult.OK;
}
@Override
public int getPrecedence() {
return 0;
}
@Override
public IProcessorMatcher<? extends Node> getMatcher() {
return new ElementNameProcessorMatcher("form");
}
}
处理这个标签:
<form:form>
<field-box th:each="item : ${command.getClass().getDeclaredFields()}">
<div th:each="item2 : ${item.getDeclaredAnnotations()}">
<div th:switch="${item2.annotationType().getSimpleName()}">
<div th:case="'Checkbox'"><field:checkbox/></div>
<div th:case="'DataList'"><field:datalist/></div>
<div th:case="'Input'"><field:input/></div>
<div th:case="'Radiobutton'"><field:radio/></div>
<div th:case="'Select'"><field:select/></div>
<div th:case="'Textarea'"><field:textarea/></div>
</div>
</div>
</field-box>
<button type="submit" class="btn btn-default">Cadastrar</button>
<div id="yes" class="alert alert-success" role="alert" style="display: none;">
<button type="button" class="close" data-hide="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<span class="text">Configuração salva com sucesso</span>
</div>
<div id="not" class="alert alert-danger" role="alert" style="display: none;">
<button type="button" class="close" data-hide="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<span class="text"></span>
</div>
</form:form>
并生成此输出:
<form role="form" class="form" action="" method="post">
<field-box th:each="item : ${command.getClass().getDeclaredFields()}">
<div th:each="item2 : ${item.getDeclaredAnnotations()}">
<div th:switch="${item2.annotationType().getSimpleName()}">
<div th:case="'Checkbox'"><field:checkbox></field:checkbox></div>
<div th:case="'DataList'"><field:datalist></field:datalist></div>
<div th:case="'Input'"><field:input></field:input></div>
<div th:case="'Radiobutton'"><field:radio></field:radio></div>
<div th:case="'Select'"><field:select></field:select></div>
<div th:case="'Textarea'"><field:textarea></field:textarea></div>
</div>
</div>
</field-box>
<button type="submit" class="btn btn-default">Cadastrar</button>
<div id="yes" class="alert alert-success" role="alert" style="display: none;">
<button type="button" class="close" data-hide="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<span class="text">Configuração salva com sucesso</span>
</div>
<div id="not" class="alert alert-danger" role="alert" style="display: none;">
<button type="button" class="close" data-hide="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<span class="text"></span>
</div>
</form>
如上例所示,它没有处理标记<form:form> 内的元素的代码。有谁知道如何解决这个问题?
【问题讨论】:
标签: spring spring-boot thymeleaf