Pedro Gonzalez
Pedro Gonzalez

Reputation: 180

Spring HashMap Form

I am creating an application with Spring Roo for generate documents. First of all the user create a document:

public class Document {

  @NotNull
  private String titleDocument;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "relatedDocumentToThisDateField")
  private Set<DateField> dateFields = new HashSet<DateField>();

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "relatedDocumentToThisRadioButton")
  private Set<RadioButtonField> radioButtonFields = new HashSet<RadioButtonField>();

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "relatedDocumentToThisStringField")
  private Set<StringField> stringFields = new HashSet<StringField>();
}

There are 3 types of fields, for simplify, this is StringField (the others are almost the same):

public class StringField extends Field {

  @NotNull
  private String valueString;

  @NotNull
  private Boolean isEditable;

  @ManyToOne
  private Document relatedDocumentToThisStringField;

  @NotNull
  private String nameStringField;
}

When the document with fields is created, another user has to fill it. Since I dont know how much fields the document will have I need to create a HashMap Form (I am following this tutorial)

My HashMap is:

public class DynamicForm {  

  private Map<String, String> dynamicMap=new HashMap<String, String>();

  public Map<String, String> getDynamicMap() {
    return dynamicMap;
  }

  public void setDynamicMap(Map<String, String> dynamicMap) {
    this.dynamicMap = dynamicMap;
  }

}

In my FillDocumentController I have:

@RequestMapping(value = "/{id}", params = "form", produces = "text/html")
public String updateForm(@PathVariable("id") Long id, Model uiModel) {

  ...

    // Create DynamicForm Map
    DynamicForm dynamicForm = new DynamicForm();
    for (Field field : allFields) {

        // Is StringField?
        if (field.getClass().equals(StringField.class) == true) {
        StringField  stringField = (StringField) field;

        if( stringField.getIsEditable() == true ) {
            dynamicForm.getDynamicMap().put(stringField.getNameStringField(), stringField.getValueString() );
            }
        }
    }

uiModel.addAttribute("dynamicForm", dynamicForm);

return "filldocuments/update";
}

This is the view:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<div
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields"
xmlns:form="http://www.springframework.org/tags/form"
xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">

<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />

<form:form action="/DocumentGenerator/filldocuments/add" modelAttribute="dynamicForm" method="post">

        <c:forEach items="${dynamicForm.dynamicMap}" var="element">

                <input name="element['${element.key}']" value="${element.value}"/>

        </c:forEach>

        <input type="submit" value="Save" />

        </form:form>

</div>

And this is the method that catch the modelAttribute:

@RequestMapping(value="/add", method = RequestMethod.POST, produces = "text/html")
public String update(@ModelAttribute(value="dynamicForm") DynamicForm dynamicForm, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {

    for( String key : dynamicForm.getDynamicMap().keySet() ) {
        System.out.println("key="+key);
    }


    return "redirect:/filldocuments";
}

My problem is that dynamicForm is empty. There is another DynamicForm inside of uiModel and is empty too (I was in debug mode). Where is the data that the user fill?? I dont know what is wrong!!

Upvotes: 2

Views: 3046

Answers (1)

Pedro Gonzalez
Pedro Gonzalez

Reputation: 180

My fault, the view has to be like that:

<c:forEach items="${dynamicForm.dynamicMap}" var="dynamicMap">

        <input name="dynamicMap['${dynamicMap.key}']" value="${dynamicMap.value}"/>

</c:forEach>

So the error was use the variable element

Upvotes: 3

Related Questions