Command Obj.

커맨드 오브젝트

컨트롤러에 들러붙은 책임들

HTTP 요청 파라미터

@RequestMapping(value = "/signup", method = RequestMethod.POST)
public @ResponseBody String upload(Model model, 
        @RequestParam(value = "email", required = true) String email,
        @RequestParam(value = "name", required = true) String name,
        @RequestParam(value = "password", required = true) String password,
        @RequestParam(value = "profile", required = false) String profile,
        @RequestParam(value = "birthday", required = false) String birthday,
        @RequestParam(value = "homepage", required = false) String homepage,
        @RequestParam(value = "profileImg", required = false) MultipartFile profileImg,
        @RequestParam(value = "protected", required = false, defaultValue = "false") boolean protect,
        HttpServletRequest request) throws BinaryException, NotEnoughParameterException {
        
    // ...이메일 포맷 확인...
    // ...패스워드 검증...
    // ...혹은 패스워드 해싱...
    // ...생일 -> Date...
    // ...프로필 이미지 사이즈 확인...
    // 등등등

    long userId = userService.signup(email, name, password, profile, birthday, homepage, profileImg, protect);
    // 뭐 이렇게 길어

    return result;
}

Command Object

@RequestMapping(value = "/signup", method = RequestMethod.POST)
public @ResponseBody String upload(Model model, 
    UserSignupForm form, // 이 친구 
    HttpServletRequest request) throws BinaryException, NotEnoughParameterException {

    long userId = userService.signup(form);

    return result;
}

선언

public class UserSignupForm {
    private String email;
    private String name;
    private String password;
    private String profile;
    private String birthday;
    private String homepage;
    private MultipartFile profileImg;
    private boolean protect;

    // getter & setter
}

in spring

스프링은 커맨드 오브젝트를 아래와 같은 순서로 생성한다.

  1. 인자 없는 생성자
  2. public void setter(param)

많은 데이터 타입을 지원한다

public setSomeInt(String intString) {
    try {
        this.someInt = Integer.parseInt(intString);
    } catch (NumberFormatException e) {
        this.someInt = 0;
    }
}

커맨드 오브젝트로 할 수 있는 것

default value

public class UserSignupForm {
    private String email;
    private String name;
    private String password;
    private String profile;
    private String birthday;
    private String homepage;
    private MultipartFile profileImg;
    private boolean protect;

    public UserSignupForm() {
        protect = false;
    }

    // getter & setter
}

로직이 함께 하는 getter, setter

public class UserSignupForm {
    private String email;
    private String name;
    private String password;
    private String profile;
    private String birthday;
    private String homepage;
    private MultipartFile profileImg;
    private boolean protect;

    private Date birthdayDate;

    public UserSignupForm() {
        protect = false;
    }

    public setProfile(String profile) {
        this.profile = SomeFilter.doFilter(profile);
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        birthdayDate = format.parse(birthday);
    }

    public Date getBirthdayDate() {
        return birthdayDate;
    }
}

@Valid

Spring Validator

Spring에서 제공하는 Spring Validator를 사용하여 커맨드 오브젝트를 검증할 수 있다.

옵션

public class UserSignupForm {
    @NotNull
    private String email;

    @NotNull
    private String name;

    @NotNull
    private String password;

    private String profile;
    private String birthday;
    private String homepage;
    private MultipartFile profileImg;
    private boolean protect;

    private Date birthdayDate;

    public UserSignupForm() {
        protect = false;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        birthdayDate = format.parse(birthday);
    }

    public Date getBirthdayDate() {
        return birthdayDate;
    }
}
public class UserSignupForm {
    @NotNull
    @Size(min = 5, max = 100)
    private String email;

    @NotNull
    @Max(50)
    private String name;

    @NotNull
    @Max(100)
    private String password;

    @Max(500)
    private String profile;

    private String birthday;

    @Max(50)
    private String homepage;

    private MultipartFile profileImg;

    private boolean protect;

    private Date birthdayDate;

    public UserSignupForm() {
        protect = false;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        birthdayDate = format.parse(birthday);
    }

    public Date getBirthdayDate() {
        return birthdayDate;
    }
}
public class UserSignupForm {
    @NotNull
    @Size(min = 5, max = 100)
    @Pattern(regexp = "^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]*\.[a-zA-Z]{2,3}$")
    private String email;

    @NotNull
    @Max(50)
    private String name;

    @NotNull
    @Max(100)
    private String password;

    @Max(500)
    private String profile;

    private String birthday;

    @Max(50)
    private String homepage;

    private MultipartFile profileImg;

    private boolean protect;

    private Date birthdayDate;

    public UserSignupForm() {
        protect = false;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        birthdayDate = format.parse(birthday);
    }

    public Date getBirthdayDate() {
        return birthdayDate;
    }
}