简体   繁体   English

springMVC:视图不会向控制器提交值

[英]springMVC: view won't submit values to controller

I've got the following setup: 我有以下设置:

public class MyController extends BaseFormController {
  public MyController() {
    setCommandClass(MyObject.class);
    setCommandName("object");
    setSessionForm("true);
  }

  @Override
  protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
        throws Exception {
    MyObject o = (MyObject) command;
    ...
  }

MyObject has a member of type MyOtherObject which, in turn, has a member Set<String> foo : MyObject具有类型MyOtherObject的成员,而后者又具有成员Set<String> foo

public class MyObject {
  ...
  MyOtherObject otherObject;
  ...
}

public class MyOtherObject {
  ...
  Set<String> foo = new HashSet<String>();
  ...
}

and all corresponding setters and getters are implemented. 并实现所有相应的setter和getter。 Then I've got a view, written in jsp and: 然后,我得到了一个用jsp编写的视图,并且:

<form:form method="POST" action="" commandName="object" ...
...
    <form:select path="otherObject.foo" multiple="multiple" size="4">
      <c:forEach var="entry" items="${listOfItems}">
        <option value="${entry.size}" <c:if test="${!entry.enabled}">disabled</c:if>>${entry.name}</option>
      </c:forEach>
    </form:select>
 ...
 </form:form>

My goal is to map the size s of some entries in the list to the set foo . 我的目标是将列表中某些条目的size s映射到set foo

All other entries in the form are set to the values the user types in, so the overall syntax should be right. 表单中的所有其他条目均设置为用户键入的值,因此总体语法应正确。 However when it comes to this multiple select form, which should be mapped to object.otherObject.foo, the controller acts as if no values whatsoever were ever provided in the view, ie even if I were to select multiple values from the box before hitting submit, the command object in the Controller still gets an empty otherObject.foo member. 但是,当涉及到应该映射到object.otherObject.foo的多选表单时,控制器的作用就像视图中没有提供任何值一样,即,即使我在点击之前从框中选择了多个值提交后,Controller中的command对象仍将获得一个空的otherObject.foo成员。

Any ideas what may be causing this? 任何想法可能是什么原因造成的?

PS I also tried adding a Set<String> foo directly in MyObject and using it in the path="" of the view. PS我还尝试了直接在MyObject中添加Set<String> foo ,并在视图的path=""中使用它。 It still didn't work out. 它仍然没有解决。

Did you override formBackingObject method? 您是否覆盖了formBackingObject方法? if yes, did you assign MyOtherObject to MyObject? 如果是,是否将MyOtherObject分配给MyObject?

After a lot of poking around I found the solution - foo had to be declared as String[] foo and from there on spring took care of all the rest, just as expected. 经过一番摸索后,我找到了解决方案-必须将foo声明为String[] foo然后从春天开始,其余所有工作都按预期进行。 Beats me why spring would use "old-fashioned" arrays in this day and age but it apparently does. 让我感到困惑的是,为什么弹簧在当今时代会使用“老式”阵列,但显然如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM