Announcement Announcement Module
Collapse
No announcement yet.
Binding a dynamic hashmap to form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding a dynamic hashmap to form

    I am trying to bind a dynamic hashmap to a form. It will put question name in the form and i want to get answer that user inputs. I can see my fields in form but don't see any question name and its value in hashmap in command object in my controller onSubmit method. Appreciate any help. My code is as follows.

    <code>
    Question.java
    ----------------
    private String name;
    private String label;
    getter and setter methods


    Command class:
    ------------------
    private Map<String, Object> values= new HashMap<String, Object>();

    public Map<String, Object> getValues() {
    return values;
    }

    public void setValues(final Map<String, Object> map) {
    this.values=map;
    }


    Controller class:
    -------------------
    protected Map<String, Object> referenceData(httpServletRequest request,
    Object command, Errors errors)
    throws Exception {

    {
    //here all question objects are added
    Map<String, Object> data = super.referenceData(request, command, errors);
    List<Question> questions = getAllQuestions();
    if (questions != null && !questions.isEmpty()) {
    data.put("myQuestions", questions);
    }
    return data;

    }



    jsp page:
    ----------

    <c:forEach var="myQuestion" items="${myQuestions}">
    <div id="customFieldSection" class="inputSection">
    <div class="fieldName">
    <label>
    <c:set var="myQuestionLabel"><c:out value="${myQuestion.label}"/></c:set>
    </label>
    </div>
    <div class="fieldValue">
    <input id='${myQuestion.name}' path='values[${myQuestion.name}]'/>
    </div>
    </div>
    </c:forEach>

  • #2
    After more investigation i found request parameter names doesn't have neither myQuestion.name nor values .Any idea please.

    Comment


    • #3
      Hi,

      Your following line use simple html input control?

      Code:
      <input id='${myQuestion.name}' path='values[${myQuestion.name}]'/>
      I think it should be either,

      Code:
      <form:input path = "values[${myQuestion.name}]" />
      Or

      Code:
      <input name = "values[${myQuestion.name}]" type = "text" />

      Comment


      • #4
        Thank you, Thank you.. It worked.

        Comment

        Working...
        X