Results 1 to 4 of 4

Thread: Binding a dynamic hashmap to form

  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Default 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. #2
    Join Date
    Jun 2012
    Posts
    3

    Default

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

  3. #3
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    285

    Default

    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" />
    Amila Domingo

  4. #4
    Join Date
    Jun 2012
    Posts
    3

    Default

    Thank you, Thank you.. It worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •