How to get dynamically generated input value in spring as a bean
In my project I am dynamically generating a textbox in a table as shown below
project Name Date1 Date2 Date3 Date4 Date5 Activity
java Development Addnew
C++ Development Addnew
i displays tables for one week. If the user clicks addnew, I generate a dynamic text box below the line where the user clicked the addnew button.
My problem is how to get all the textbox value in my spring controller as a bean class. Because I'm not sure if it is appropriate how many textboxes go into loading and submitting the page.
Any idea would be greatly appreciated !!!
source to share
There are not enough specific questions in your question to provide a specific answer. However, a general approach I would recommend.
- If you have a framework like Backbone or Angular, look into using its collection tools.
- Write JavaScript that builds a JSON array from all of your text fields
- Define a POJO in Java that reflects every entry in your array.
- Make sure you are using Jackson - this maps JSON objects to Java in front of you when your controller is called
- Define a method in your controller that accepts a POJO list for example. create (list values) with url as / times / {employeeId} using PUT
- To read from the database, add a method to the controller that returns a POJO list, for example. List of values ββget (long employeeId) with url as / times / {employeeId} using GET
- Alternatively, if you want a form that will "live", i.e. "Add new" calls a row in the database to instantly use the REST interface with create, update and DELETE using POST, PUT and DELETE respectively.
I assume you will need to update the list later, so I would recommend a structure with an ID that can be used for CREATE and UPDATE operations, not just a simple list of strings, this will also allow more fields later.
public void Foo {
private String project;
private String name;
private long id;
// getters + setters
}
JSON to create
[{"project":"java","name":"Development",id:0}, {"project":"C++","name":"Development",id:0}]
JSON for later update, that is, with identifiers with rounding
[{"project":"java","name":"Development",id:100}, {"project":"C++","name":"Development",id:101}]
source to share
Go to the traditional method getParameter()
. I am assuming your textbox will have unique names and generated with jquery
.
In the controller
List<String> requestParameterNames = Collections.list((Enumeration<String>) request.getParameterNames());
for (String parameterName : requestParameterNames) {
String attributeName = parameterName;
String attributeValue = request.getParameter(parameterName);
// will have the text box values
}
source to share