Spring 表单绑定对象一对多

Spring Form binding object one to many

我有问题。 我用Spring Mongo Data, Web, mvc ecc... 我想用他的依赖绑定一个复杂的对象,但我的解决方案不起作用。

这是代码:

型号

public class Foo {

   @Id
   private String id;

   private String nome;

   private String descrizione;

   private Date dataCreazione;

   private Date dataUltimaModifica;

   @DBRef
   private Dependency dependency;   //getters and setters
}


public class Dependency {
   @Id
   private String id;

   @Indexed(unique = true)
   private String nome;

   private String descrizione;

   private Date dataCreazione;

   private Date dataUltimaModifica;

  //GETTERS AND SETTERS

}

控制器

@RequestMapping(value = Constants.ADD, method = RequestMethod.GET)
public String add(Model model) {

    Foo foo = new Foo();
    model.addAttribute("fooForm", foo);
    model.addAttribute("depList",depService.getList());

    return "foo/add";
}

@RequestMapping(value = Constants.ADD, method = RequestMethod.POST)
public String addPost(@ModelAttribute("fooForm") Foo foo, BindingResult result, Model model) {
   //Check if foo is not null
}

查看

<form:form class="form-horizontal" method="post" modelAttribute="fooForm" action="${addFoo}">
    <spring:bind path="dependency">
       <div class="form-group">
         <label for="dependency" class="col-sm-2 control-label">Dependency</label>
         <div class="col-sm-10">
         <form:errors path="dependency" class="control-label" />
           <form:select path="dependency" title="Dependency" >
             <form:option value="">&nbsp;</form:option>
               <form:options items="${depList}" />
           </form:select>
           <p class="help-block mb-0">Select a dependency</p>
         </div>
      </div>
   </spring:bind>
   <spring:bind path="nome">
     <div class="form-group">
       <label for="nome" class="col-sm-2 control-label">Nome</label>
       <div class="col-sm-10">
       <form:errors path="nome" class="control-label" />
          <form:input path="nome" type="text" class="form-control" id="nome" placeholder="Nome Foo" />
       <p class="help-block mb-0">Specificare il nome of Foo</p>
       </div>
    </div>
 </spring:bind>
 <spring:bind path="descrizione">
    <div class="form-group">
       <label for="descrizione" class="col-sm-2 control-label">Descrizione</label>
        <div class="col-sm-10">
           <form:errors path="descrizione" class="control-label" />
              <form:input path="descrizione" type="text" class="form-control" id="descrizione" placeholder="Descrizione" />
      <p class="help-block mb-0">Inserire una breve descrizione per Foo</p>
        </div>
     </div>
  </spring:bind>
  <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-rounded btn-primary btn-sm">Salva</button>
      </div>
  </div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

当我 post 表单时,我得到了 nome 和 descrizione 的正确值,但依赖项始终为空...为什么?

示例:

 //METHOD POST
 public String addPost(@ModelAttribute("fooForm") Foo foo, 
        BindingResult result, Model model) {

      foo.getNome is not null
      foo.getDescrizione is not null
      foo.getDependency is always null ... why?
 }

提前致谢。

我找到解决办法了!很简单!

我们需要一个转换器对象,因为绑定形式 return 依赖 ID 而不是整个对象!

嗯,这是解决方案:

转换器


    
    public class DependencyConverter implements Converter {
        @Autowired
        private CategoriaRepository repository;

        @Override
        public Categoria convert(String id) {
            return repository.findById(id);
        }
    }
    

现在我们在我们的应用程序中注册转换器-context.xml

<mvc:annotation-driven conversion-service="conversionService"/>

<bean id="conversionService"
    class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="myPackage.DependencyConverter" />
        </set>
    </property>
</bean>

就这些了...

希望这对以后有所帮助....

再见再见...