Trying to create and save list of objects in spring mvc

12,242

Solution 1

Your inputs need to look like this:

<form:form action="save" method="POST" modelAttribute="DynamicRowForm">
  ...
  <td><form:input path="dynamicRow[0].id"  type="text" /></td>
  <td><form:input path="dynamicRow[0].name"  type="text" /></td>
  <td><form:input path="dynamicRow[0].email"  type="text" /></td>
  ...
</form:form>

Which essentially states: on form submission bind the id field to the DynamicRow at index 0 in the form backing Object - DynamicRowForm.

If you do this, then the following should print the values input:

  @RequestMapping(value="/save")
    public String showList(@ModelAttribute DynamicRowForm dynamicRowForm) {
        System.out.println(dynamicRowForm.getDynamicRow().get(0).getId());
        System.out.println(dynamicRowForm.getDynamicRow().get(0).getName());
        System.out.println(dynamicRowForm.getDynamicRow().get(0).getEmail());
        return "success";
    }

And one more thing I would like to clarify, in my first controller am creating a dynamicRowForm object and adding into model so that my jsp page can access it...and in my second controller am receiving DynamicRowForm object using @ModelAttribute with same name..but here am getting different object.why?

Because it is stateless. If you want to work with the same instance you will need to store it in the session between requests. See the following useful discussion.

http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/

Solution 2

You are missing indexing here as you are using collection. Try...

<form:input path="${DynamicRowForm.dynamicRow[0].id}"  type=""   size="17%"/>

and also in your addRow(), you have to use the dynamic index with row count. If it is not working with form:input, try simple html input type.

Share:
12,242
Sharique
Author by

Sharique

Updated on June 13, 2022

Comments

  • Sharique
    Sharique almost 2 years

    This is my model class

    public class DynamicRow {       
            private String id;
            private String name;
            private String email;
    
            public DynamicRow(String id, String name, String email) {
                this.id = id;
                this.name = name;
                this.email = email;
            }
            public DynamicRow() {
                // TODO Auto-generated constructor stub
            }
            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            @Override
            public String toString() {
                return "DynamicRow [id=" + id + ", name=" + name + ", email=" + email
                        + "]";
            }
            public String getEmail() {
                return email;
            }
            public void setEmail(String email) {
                this.email = email;
            }
        }
    

    This is the form class

    public class DynamicRowForm {
    
        private List<DynamicRow> dynamicRow = LazyList.decorate(new ArrayList<DynamicRow>(), FactoryUtils.instantiateFactory(DynamicRow.class));
        public DynamicRowForm() {
    
        }
        public List<DynamicRow> getDynamicRow() {
            return dynamicRow;
        }
    
        public void setDynamicRow(List<DynamicRow> dynamicRow) {
            this.dynamicRow = dynamicRow;
        }
    }
    

    Below is my controllers

    @RequestMapping(value="/createDetails")
        public String list(Model model){
            DynamicRowForm dynamicRowForm = new DynamicRowForm();
            model.addAttribute("DynamicRowForm",dynamicRowForm);
    
            return "test2";
        }
    
        @RequestMapping(value="/save")
        public String showList(Model model,@ModelAttribute("DynamicRowForm") DynamicRowForm dynamicRowForm) {
            System.out.println(dynamicRowForm.getDynamicRow());
            return "success";
        }
    

    And this is my jsp page named as test2

    <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
    var rowCount = 1;
    function addMoreRows(form) {
    rowCount ++;
    var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="" type="text" size="17%"  maxlength="120" /></td><td><input name="" type="text"  maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> <a href="javascript:void(0);" onclick="removeRow('+rowCount+');">Delete</a></p>';
    $('#addedRows').append(recRow);
    }
    
    function removeRow(removeNum) {
    $('#rowCount'+removeNum).remove();
    }
    </script>
    </head>
    <body>
    <form:form action="save" method="GET" modelAttribute="DynamicRowForm">
    <input type="button" onclick="addMoreRows(this.form);" value="AddRow">
    <table rules="all" style="background:#fff;">
    <tr>
    <td style="font-size:14px;" >Name</td>
    <td style="font-size:14px;">Email</td>
    <td style="font-size:14px;">Mobile</td>
    <!-- <td><span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">
    Add More
    </span>
    </td> -->
    </tr>
     &nbsp;
    <tr id="rowId">
    
    <td><form:input path="${dynamicRow.id}"  type=""   size="17%"/></td>
    <td><form:input path="${dynamicRow.name}"  type="text"   /></td>
    <td><form:input path="${dynamicRow.email}"  type="text"   /></td>
    </table>
    <div id="addedRows"></div>
    <input type="submit" value="Save">
    </form:form>
    </body>
    

    I know these questions have been asked before, but as am very new to spring mvc and learning things so am trying to get some comfort in this technology..

    Anyway, what here I am trying to do is to save multiple row/list of object... but its not getting saved in the list... Please help me out on what is the mistake am doing, and correct me.

    Edit And one more thing I would like to clarify, in my first controller am creating a dynamicRowForm object and adding into model so that my jsp page can access it...and in my second controller am receiving DynamicRowForm object using @ModelAttribute with same name..but here am getting different object.why?