【问题标题】:Dynamically adding Spring MVC JSP form:input using JS giving binding errors动态添加 Spring MVC JSP 表单:使用 JS 的输入给出绑定错误
【发布时间】:2016-03-16 06:51:00
【问题描述】:

我有一个动态添加的代码块

下面是我正在使用的代码-

<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>

下面是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--;
    }
  });
});

现在尝试加载页面时 -

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)

我的直觉是,这是因为 form:input 不是 form:form 标签本身的一部分。但由于我在表单标签中添加,不应该是这种情况。

请提出建议。

AJ

【问题讨论】:

  • 即使没有点击加号按钮“addRowCabin”,即使在页面加载时也会出现此问题。
  • 你的 'someModel' 对象是否有一个名为 test 的字段,带有一个 getter 和一个 setter?
  • 是的,它确实有带 getter/setter 的字段。只有 JS 部分给出了这个问题,而不是原始表单本身。

标签: javascript jquery spring forms jsp


【解决方案1】:

&lt;form:input&gt; 是在服务器上处理请求期间评估的 jsp 标记。您不能使用 javascript 添加它。如果您的 javascript 是 JSP 的一部分,那么这就是它在加载时失败的原因。

而是查看生成的 HTML,现有输入 ID 和名称的外观,并附加原始 HTML 输入标记。

或者更好的使用 c:forEach 围绕 JSP 中的字段集合,并通过 AJAX 请求添加新的动态字段。

关于绑定集合的好文章在这里:http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2014-06-21
    • 2020-10-31
    • 2016-07-07
    • 2011-01-24
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多