spring mvc 2(김영한) - 메세지 소스, 검증, 로그인

  • 메세지 소스
    • 변수 사용 가능, 점(.)으로 이름 나눌 수 있음, default message 제공 가능

 

  • 검증
    • bindingResult -> addError() rejectValue() FieldError()
    • 파라미터 값도 재전달 가능
    • bindingResult에서도 message source 사용 가능
    • spring.messages.basename=messages,errors -> errors.properties에서 사용
    • 단계적 메세지 처리 가능; 자세한 값, 기본값 순으로 메세지 반환함
    • code.objectName.fieldName 식으로 값 설정하면됨(typeMismatch.user.age)
    • implements Validator, @Override supports(Class<?> clazz), @Override validate(Object target, Error errors)

 

  • 검증2 - Bean Validation
    • @NotNull @Email @Range(min, max) @Max(int) @Pattern(regexp="")
    • 상황에 따라 제한조건이 다름 - null 또는 not null
    • 이 경우, @NotNull(groups={SaveCheck.class, UpdateCheck.class}) 설정 후 @Validated(SaveCheck.class)로 사용하거나 dto 분리해서 사용하면 됨

 

  • 로그인 처리
    • Cookie or @CookieValue()
    • 초기화 시 null값 넣어주고, setMaxAge(0)
    • (public static final == interface 일반 변수)
    • HttpSession or @SessionAttribute

 

  • 필터
    • 과 관련된 공통관심사는 AOP보다 서블릿 필터 또는 인터셉터로 해결하기
    • Http 요청 -> WAS -> 필터 -> 서블릿 -> 컨트롤러
    • Filter 사용하고 chain.doFilter()해줘야 그 다음 단계로 넘어감
    • FilterRegistrationBean<Filter> setFilter() addUrlPatterns()
    • 필터 조건에 맞지 않는 경우 거를 때는 sendRedirect로.

 

  • 인터셉터
    • 서블릿 필터는 서블릿이 제공, 스프링 인터셉터는 스프링이 제공
    • Http 요청 -> WAS -> 필터 -> 서블릿 -> 스프링 인터셉터 -> 컨트롤러
    • HandlerInterceptor, preHandle(), postHandle(), afterCompletion()
    • ModelAndView 사용 가능
    • webConfig 빈등록
    • addInterceptors(InterceptorRegistry registry)
    • registry.addInterceptor(new CustomInterceptor())
    • addPathPatterns("/**"), excludePathPatterns("/css/**", )

 

 

댓글을 작성해보세요.