2

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?

4
  • Is the default row you have in your page working? Commented Mar 9, 2015 at 17:07
  • you mean to say, the default row value is getting saved or not!right? Commented Mar 9, 2015 at 17:10
  • Yes, in controller are you getting 1 entry in the list with the default row, without any added rows? Commented Mar 9, 2015 at 17:11
  • No,its not getting saved...by default one row is coming and when am adding data to save, it does hit my controller but list is empty. Not even the value of default row is getting saved Commented Mar 9, 2015 at 17:11

3 Answers 3

3

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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.

8 Comments

I added that indexing part ${dynamicRow[0].id} like this, so atleast default row should get saved...but then also it didn't work
Can you see the view source to see how your text box is rendered? Please provide a comment.
This is what am getting in view source <td><input type="text" value="" size="17%"/></td> <td><input type="text" value=""/></td> <td><input type="text" value=""/></td>
That is strange! Can you please don't use type="" and try again? It should convert your path to name and id.
Yes!! That's what I also read, it converts path into id and name. I removed type attribute but still am getting same thing source <td><input type="text" value=""/></td> <td><input type="text" value=""/></td> <td><input type="text" value=""/></td>
|
0
 var count=0;
 function addMoreRows(form) {
    rowCount ++;
    count =++count;
    var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input path="users['+count+'].id" id="users'+count+'.id" name="users['+count+'].id" type="" size="17%"  maxlength="120" /></td><td><input path="users['+count+'].name" id="users'+count+'.name" name="users['+count+'].name" type="text"  maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input path="users['+count+'].email" id="users'+count+'.email" name="users['+count+'].email" 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);
 }

1 Comment

jquery on how to generate the index dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.