Spring MVC dynamically adding form elements I made a simple form with an option of adding one text field dynamically on mouse click. And I will be grateful for suggestions on how I may improve the code.

@Controller
public class MyController {

public ArrayList&lt;NewField&gt; fieldArrayList;
@RequestMapping(value="/create", method= RequestMethod.GET)
public String myForm(Model model, @ModelAttribute NewField newField) {

    fieldArrayList = new ArrayList&lt;&gt;();
    fieldArrayList.add(newField);
    model.addAttribute("dataForm", new DataForm());

    return "create";
}

@RequestMapping(value="/create", method=RequestMethod.POST)
public String formSubmit(Model model, HttpServletRequest hsr, @ModelAttribute DataForm dataForm) {

    if (hsr.getParameterValues("lastField") != null) {


        String[] lastF = null;
        lastF = hsr.getParameterValues("lastField");
        List&lt;String&gt; d = Arrays.asList(lastF);

        int count = d.size() + 1;
        for (int i = 2; i &lt; count+1; i++) {
            NewField newField = new NewField();
            String s1 = hsr.getParameter("p_new_" + String.valueOf(i));
            String s2 = hsr.getParameter("l_" + String.valueOf(i));
            newField.setStr1(s2);
            newField.setStr2(s1);
            fieldArrayList.add(newField);
        }
    }
    model.addAttribute("dataForm", dataForm);
    model.addAttribute("fieldArrayList", fieldArrayList);

    return "view";
}




&lt;!DOCTYPE HTML&gt;
&lt;html xmlns:th="http://www.thymeleaf.org"&gt;
&lt;head&gt;
    &lt;title&gt;Getting Started: Handing Form Submission&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
    &lt;script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Form&lt;/h1&gt;
&lt;form action="#" th:action="@{/create}" th:object="${dataForm}" method="post"&gt;
    &lt;p&gt;Id: &lt;input type="text" th:field="*{id}" /&gt;&lt;/p&gt;
    &lt;p&gt;Name: &lt;input type="text" th:field="*{content}" /&gt;&lt;/p&gt;
    &lt;script type="text/javascript"&gt;
        $(function() {
            var addDiv = $('#addinput');
            var i = $('#addinput p').size() + 1;
            $('#addNew').live('click', function() {
                $('&lt;p&gt;&lt;label&gt; ' + document.getElementById('p_new').value + '&lt;/label&gt;&lt;input type="text" name="p_new_' + i
                        +'"  /&gt;&lt;input type="hidden" value="' + document.getElementById('p_new').value
                        + '" name="l_' + i
                        + '" /&gt;&lt;input type="hidden" value="' + i + '" name="lastField" /&gt; &lt;/p&gt;').appendTo(addDiv);
                //alert(i);
                i++;
                return false;
            });
            $('#remNew').live('click', function() {
                if( i &gt; 2 ) {
                    $(this).parents('p').remove();
                    i--;
                }
                return false;
            });
        });
    &lt;/script&gt;
    &lt;div id="addinput"&gt;
        &lt;p&gt;
        &lt;/p&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;input type="submit" value="Submit" /&gt; &lt;/p&gt;
&lt;/form&gt;
&lt;input type="text" id="p_new" size="20" name="p_new" value="" placeholder="Input Value" /&gt;
&lt;input type="button" id="addNew" value="Add New field"/&gt;

&lt;/body&gt;
&lt;/html&gt;
Use interface List instead of concrete class for the fieldArrayList
field.
Make the fieldArrayList field private.
@RequestMapping(value="/create", method= RequestMethod.GET). Requests using GET should only retrieve data, not create.
Extract the calls of hsr.getParameterValues and hsr.getParameter methods in separate methods.
Don't use meaningless variable names such as d, s1, s2.
Format code with your IDE. For example, in IDEA you can use the Ctrl+Alt+L hotkey.
Extract attribute and parameter names in constants.
@RequestMapping(value="/create", method=RequestMethod.POST) can be replaced with @PostMapping("/create").