简体   繁体   English

创建不映射到实体的表单

[英]Creating a form that doesn't map to an entity

Using spring MVC, how would I create a form that doesn't map to a entity (ie it has properties from multiple entities). 使用spring MVC,我将如何创建不映射到实体的表单(即,它具有来自多个实体的属性)。

I want to also validate and use their 'results' object that has the errors collection etc. 我还想验证并使用具有错误收集等功能的“结果”对象。

Any online examples of this? 有网上的例子吗? I need to see it to understand it (newbie) 我需要看它才能理解它(新手)

You'd simply create a new class containing all the properties you need for the form and use this as model attribute for your form. 您只需创建一个新类,其中包含表单所需的所有属性,然后将其用作表单的模型属性即可。 On the receiving call you can then use this type, too. 然后,在接听电话时,您也可以使用此类型。 Spring will automatically bind properties for you. Spring将自动为您绑定属性。 You should also consider using JSR-303 validation annotations. 您还应该考虑使用JSR-303验证批注。

The general approach is to load all the necessary entities to create the form backing object from on the GET request. 通用方法是加载所有必需的实体,以从GET请求中创建表单支持对象。 Then you put that form backing object in the model. 然后,您将该表单支持对象放入模型中。 On the POST/PUT request you have to reconstruct the actual entities touched. 在POST / PUT请求上,您必须重建所接触的实际实体。 Typically you load them again then and apply the new submitted partial data to them. 通常,您然后再次加载它们,并将新提交的部分数据应用于它们。

In general it might be a good idea to construct a dedicated component to handle that assembling behaviour for you to not pollute the controller with that code. 通常,最好构造一个专用组件来处理这种组装行为,以使您不会用该代码污染控制器。

/**
 * Prepares the form by setting up a custom form backing object.
 */
@RequestMapping(value = "account/{id}", method = GET)
public String processGet(@PathVariable("id") Long id, Model model) {

  Account account = accountService.getAccount(id);
  return prepareForm(dtoAssembler.createFormFor(account), model);
}


/**
 * Process form submission. Uses JSR-303 validation and explicit error 
 * handling.
 */  
@RequestMapping(value = "account/{id}", method = PUT)
public String processGet(@ModelAttribute("accountForm") @Valid AccountForm form, Errors errors, Model model) {

  if (errors.hasErrors()) {
    return prepareForm(form, model);
  }

  Account account = accountService.getAccount(form.getId());
  accountService.save(dtoAssembler.applyData(account, form));

  return "redirect:/accounts";
}


/**
 * Populates the given {@code Model} with the given {@code AccountForm}
 * and returns the view to show the form.
 */     
private String prepareForm(AccountForm form, Model model) {
  model.addAttribute("accountForm", form);
  return "account";
}

I just wrote it this way here to emphasize what's going on. 我只是在这里这样写,以强调正在发生的事情。 In a real world scenario I'd probably let the DtoAssembler do all the work with the service (so I'd inject the service into the assembler). 在现实世界中,我可能会让DtoAssembler完成该服务的所有工作(因此,我会将服务注入到汇编器中)。

To ease transferring data from the DTO into the domain object using Dozer, BeanUtils or something similar is probably reasonable. 为了使用Dozer,BeanUtils或类似工具轻松地将数据从DTO传输到域对象,可能是合理的。

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

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