spring mvcのフォームタイプ変換(custom property editor)

6920 ワード

spring mvcのフォームタイプの変換が強すぎて、今は二つの簡単なものを使っています.
一つはフォームの中のfileを自動的にbyte[]にマッピングすることで、ファイルをアップロードする(blobを使用すれば)コードを書く必要がなくなります.
もう一つはフォームのyyyy-M-ddフォーマットをjava.util.Dateにマッピングし、
User.javaには以下の2つの特殊な属性があると仮定する.
public class User implements Serializable{
    private Date birth;
    private byte[] icon;
}
```             Controller         initBinder  :





<div class="se-preview-section-delimiter">div>
ここにコードシートを書きます.
@Controller("userController")
@RequestMapping(value = "/user")
public class UserController {
    @RequestMapping(value = "create", method = RequestMethod.POST)

    public String create(@ModelAttribute("user") User user,

            RedirectAttributes redirectAttributes) {

        userService.createUser(user);

        redirectAttributes.addFlashAttribute("message", "create success!");

        return SUCCESS;

    }    

    @InitBinder
    protected void initBinder(
            WebDataBinder binder) throws ServletException {
        binder.registerCustomEditor(byte[].class,
                new ByteArrayMultipartFileEditor());         

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                dateFormat.setLenient(false);
                binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
}
ByteArayMultiphleEditorとCustom DateEditorはすべてspringで直接提供されます.カスタム:
  public class User implements Serializable{
    public Set roles = new HashSet();
    }
public class Role implements Serializable {
    private Long id;
    private String name;
UserControllerは以下の通りである.
@RequestMapping(value = "create", method = RequestMethod.GET)
public String createForm(ModelMap model) {
    model.addAttribute("roleList", roleService.findAllRoles());
    User user = new User();
    model.addAttribute(user);
    return "user/user_new";
}
public class RoleEditor extends PropertyEditorSupport {2
    private RoleService roleService;
    public RoleEditor(RoleService roleService) {
        this.roleService = roleService;
    }
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (text != null) {
            Role role = roleService.findRoleById(Long.valueOf(text));
            setValue(role);
        } else {
            setValue(null);
        }
    }
}
そしてUserControllerのinitBinder方法にこのエディタを登録します.
@InitBinder
protected void initBinder(
        WebDataBinder binder) throws ServletException {
    //@see http://forum.springsource.org/showthread.php?59612-Service-injection-amp-PropertyEditor
    binder.registerCustomEditor(Role.class, new RoleEditor(roleService));
}
この時UserControllerのcreate方法で取得したUserオブジェクトはすでにrolesに紐付けされています.
@RequestMapping(value = "create", method = RequestMethod.POST)
public String create(@ModelAttribute("user") User user,
        RedirectAttributes redirectAttributes) {
    userService.createUser(user);
    redirectAttributes.addFlashAttribute("message", "create success!");
    return SUCCESS;
}
注意すべきなのは、RoleのequalsとhashCodeの方法を上書きしなければなりません.そうでないと、修正ページに入ると、userのrole属性は自動的にcheckになりません.