Sri
Sri

Reputation: 1505

how to get dynamic values from posting a form in the Spring controller

I am creating a form dynamically, and I need to post it and get the values in SpringController

    for(var i=0;i<datArray.length;i++){
        element[i] = document.createElement("input");
        element[i].setAttribute("type", "text");
        element[i].setAttribute("name", "text");
        element[i].setAttribute("placeholder",datArray[i]);
        element[i].setAttribute("id", datArray[i]+"id");
        var foo = document.getElementById("fooBar");        
        //Append the element in page (in span).
        foo.appendChild(element[i]);
}   

this is my dynamic form drawn in Select onChange.

About Pic - this is my dynamic form drawn in Select onChange

On Every dropdown Changed I am generating different text boxes dynamically. I need to post the dynamic text boxes and get the values in the Controller Spring in JAVA. How to get dynamically posted Values in controller?

Any Idea?

Upvotes: 0

Views: 3093

Answers (1)

storm_buster
storm_buster

Reputation: 7568

Are you always sending the same model Object? I mean, does your form element always has the same name attribute?

if yes, you can just create a pojo class with attributes maching your names and use a RequestAttribute annotation.

If no, there is no way, you will just have to use the old requestparameter.

UPDATE If you dont know what parameters are submitted, loop into all parameters :

    List<String> requestParameterNames = Collections.list((Enumeration<String>) request.getParameterNames());

for (String parameterName : requestParameterNames) {
    String attributeName = parameterName;
    String attributeValue = request.getParameter(parameterName);

    //DO YOUR STUFF
}
//DO YOUR STUFF

Upvotes: 3

Related Questions