简体   繁体   English

Spring Form绑定对象一对多

[英]Spring Form binding object one to many

I have a problem. 我有个问题。 I use Spring Mongo Data, Web, mvc ecc... I want to bind a complex object with his dependency, but my solution not work. 我使用Spring Mongo Data,Web,mvc ecc ...我想用他的依赖项绑定一个复杂的对象,但是我的解决方案不起作用。

This is the code: 这是代码:

MODELS 楷模

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

} }

CONTROLLER CONTROLLER

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

VIEW 视图

<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}" />

When i post the form, I get a correct value for nome and descrizione but dependency is always null... why? 当我发布表单时,我得到了nome和descrizione的正确值,但是依赖关系始终为空...为什么?

Example: 例:

 //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?
 }

Thanks in advance. 提前致谢。

I have found the solution! 我找到了解决方案! It's very simple! 非常简单!

We need a Converter Object because the binded form return Dependency ID and not the entire Object! 我们需要一个Converter Object,因为绑定的表单返回的是Dependency ID,而不是整个Object!

Well, this is the solution: 好吧,这是解决方案:

Converter 变流器


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

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

Now we register the converter in our application-context.xml 现在我们在application-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>

that's all... 就这样...

I hope this will be helpful in the future.... 我希望这对以后会有所帮助。

Bye Bye... 再见...

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

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