2

I have a code block where i am dynamically adding

Below is the code that i am using -

<form:form role="form" method="post" id="addForm" action="/data/SomeAction" modelAttribute="someModel">
    <div id="rowCabin1">
        <div id="rowCabinData1">
            <div class="row" id="cabinRow1">
                <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                    <div class="form-group">
                    <form:input path="test" placeholder="Name" class="form-control" style="width:100%" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</form:form>

Below is the javascript.

$().ready(function() {
  var i=2;
  $("#addRowCabin").click(function(){
    $('#rowCabin1').append('<div id="rowCabinData'+i+'"><div class="row" id="cabinRow1"><div class="col-xs-12 col-sm-12 col-md-6 col-lg-6"><div class="form-group"><form:input path="test" placeholder="Name" class="form-control" style="width:100%" /></div></div></div></div>');
    i++; 
  });
  $("#delRowCabin").click(function(){
    if(i>2){
      $("#rowCabinData"+(i-1)).remove();
      i--;
    }
  });
});

Now when trying to load the page -

ERROR: org.springframework.web.servlet.tags.form.InputTag - Neither BindingResult nor plain target object for bean name 'test' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'test' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)

My gut feel was that it is because of the form:input not being part of the form:form tag itself. but since i am adding in the form tag, that should not be the case.

Please suggest.

AJ

3
  • This issue is coming even without clicking the plus button "addRowCabin", i.e. even at the page load time. Commented Dec 10, 2015 at 15:30
  • Does your 'someModel' object have a field named test with a getter and a setter? Commented Dec 10, 2015 at 15:34
  • Yes, it does have the field with getter/setter. Only the JS part is giving this issue and not the original form itself. Commented Dec 10, 2015 at 15:56

1 Answer 1

2

<form:input> is jsp tag which is evaluated during request processing on server. You cannot add it with javascript. If your javascript was part of the JSP then this is why it failed on load-time.

Instead look at generated HTML how existing input id and name look like and append raw HTML input tag.

Or better use c:forEach around field collection in JSP and add new dynamic field by AJAX request.

Nice article about binding a collection is here: http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/

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

Comments

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.