0

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?

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188
  • Are you trying to generate the form dynamically based on the command object? I just created similar question [here](http://stackoverflow.com/q/30093414/2944265), but I'd like more, if the form:form tag will render itself completely. Is it possible to generate the HTML inside the processor code? – Zveratko May 07 '15 at 08:00

1 Answers1

1

I manage to solve this with this code:

Map<String, Object> map = arguments.getContext().getVariables();
setObject( map.get("command") );

where object is a attribute from the processor class.

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188