In my current spring-boot project I have a view mapped in my controller this way:
@RequestMapping(value = "cadastra")
@PreAuthorize("hasPermission(#user, 'cadastra_'+#this.this.name)")
@Menu(label = "cadastra")
public String cadastra(Model model) throws Exception {
model.addAttribute("command", serv.newObject());
return "private/cadastra";
}
the html page have a structure like that:
<form:form action="cadastra">
<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>
</form:form>
the code for the processor for the tag form:form
is that:
public class Form extends AbstractProcessor {
public static Element form = new Element("form");
private String action;
private String object;
@Override
public ProcessorResult doProcess(Arguments arguments,ProcessorMatchingContext context,Node node) {
Element parent = (Element) node;
setAction( parent.getAttributeValue("action") );
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);
}
List<Element> lista2 = form.getElementChildren();
for(Element child : lista2) {
child.setProcessable(true);
}
node.getParent().removeChild(node);
return ProcessorResult.OK;
}
@Override
public int getPrecedence() {
return 0;
}
@Override
public IProcessorMatcher<? extends Node> getMatcher() {
return new ElementNameProcessorMatcher("form");
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getObject() {
return object;
}
public void setObject(String object) {
this.object = object;
}
}
Right now, I am looking for a way to read in this class FormProcessor the variable command
from page context, to be able to store the value command.getClass().getSimpleName()
to the attribute object
.
Anyone can tell if this is possible and how to accomplish that?