0

I am designing an application with Spring MVC3. I have to create a JSP page which has a data table and user and can add rows in the JSP data table. I was considering the following two options:

  1. To use JavaScript function to add a row on an "Insert Row" button
  2. To submit the form on "Insert Row" button, go to Controller and return a model attribute with an extra row.

In JavaScript approach, I am able to add rows but these rows are not binding at server side. How can I achieve this using Controller approach?

Please note that I cannot use jQuery or any other JavaScript library.

1 Answer 1

2

I have achieved this using the LazyList. You need to have a lazy list object in your model which will hold the list of objects of the another model class which will represent data in each row. Following is the syntax to declare LazyList in your model class.

private List<OperationParameters> operationParameterses = LazyList
            .decorate(new ArrayList<OperationParameters>(),
                    FactoryUtils.instantiateFactory(OperationParameters.class));

And the OperationParameters is the simple pojo class whose one object will represent your 1 row.

Once you have this lazy list in your modelAttribute class then on the jsp you need to create control for each property in your OperationParameters pojo with help of simple html input tag. You can not use spring form taglib to bind those controls to your pojo. You need to give the name of each input tag as per following.

<input type='text' id='operationParameterses0.inputOutputParamName' name='operationParameterses[0].inputOutputParamName'/>

where in the name and id attribute the '0' represents the index of the list. Now when you submit the form you can get those inserted rows binded to the pojo in the LazyList object.

Hope this helps you.

Cheers.

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

13 Comments

but how i will bind those rows added by java script to this lazy list??
It will be managed by Spring automatically. You just need to specify the ID and Name in the format I have mentioned. Create the input tag with this ID and name and when you submit the form you will get every row in your list.
You need to maintain the counter of the number of rows added and on each new row added you need to increase the counter and put that counter in place of '0' in the id and name attribute in the tag.
may you suggest me ,how can i increse this counter by 1 while clicking on 'add Row' button???
You need to have a global variable in your javascript that will have the counter value. When user clicks on the add row button you need to add 1 in the counter and create the new row having the controls with the id and name as I suggested with this incremented counter value.
|

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.