Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 국제화
- oracle
- 안드로이드
- 오버로딩
- Graphic
- struts2
- AWT
- 기본
- 이클립스
- Java
- layout
- HTML
- Menu
- 어노테이션
- OGNL
- 전화걸기
- Spring
- 에러페이지
- Android
- Eclips
- JavaScript
- 메서드
- 배열
- mybatis
- 메소드
- 클래스
- 생성자
- paint
- 예외처리
- JSP
Archives
- Today
- Total
note
Spring 유효성 체크 본문
dispatcher-servlet.xml
message.validation
madvirus.spring.chap06.validator/MemberInfoValidator.java
package madvirus.spring.chap06.validator;
import madvirus.spring.chap06.model.Address;
import madvirus.spring.chap06.model.MemberInfo;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
public class MemberInfoValidator implements Validator {
@Override
public boolean supports(Class clazz) {
return MemberInfo.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
MemberInfo memberInfo = (MemberInfo) target;
if(memberInfo.getId() == null || memberInfo.getId().trim().isEmpty()){
errors.rejectValue("id","required");
}
if(memberInfo.getName() == null || memberInfo.getName().trim().isEmpty()){
errors.rejectValue("name","required");
}
Address address =memberInfo.getAddress();
if(address == null){
errors.rejectValue("address","required");
}
if(address != null){
errors.pushNestedPath("address");
try{
if(address.getZipcode() == null || address.getZipcode().trim().isEmpty()){
errors.rejectValue("zipcode","required");
}
if(address.getAddress1() == null || address.getAddress1().trim().isEmpty()){
errors.rejectValue("address1","required");
}
}finally{
errors.popNestedPath();
}
}
}
}
madvirus.spring.chap06.model/Address.java
package madvirus.spring.chap06.model;
public class Address {
private String zipcode;
private String address1;
private String address2;
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
}
madvirus.spring.chap06.model/MemberInfo.java
package madvirus.spring.chap06.model;
public class MemberInfo {
private String id;
private String name;
private Address address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
message/validation.properties
required=필수항목입니다.
invalidIdOrPassword.loginCommand=아이디({0})와 암호가 일치하지 않습니다.
required.loginCommand.userId=사용자 아이디는 필수 항목입니다.
required.password=암호는 필수 항목입니다.
typeMisamatch.from=시작일 값 형식은 yyyy-MM-dd 여야 합니다
typeMisamatch.to=종료일 값 형식은 yyyy-MM-dd 여야 합니다.
madvirus.spring.chap06.controller/CreateAccountController.java
package madvirus.spring.chap06.controller;
import javax.servlet.http.HttpServletRequest;
import madvirus.spring.chap06.model.Address;
import madvirus.spring.chap06.model.MemberInfo;
import madvirus.spring.chap06.validator.MemberInfoValidator;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/account/create.do")
public class CreateAccountController {
@ModelAttribute("command")
public MemberInfo formBacking(HttpServletRequest request) {
if (request.getMethod().equalsIgnoreCase("GET")) {
MemberInfo mi = new MemberInfo();
Address address = new Address();
address.setZipcode(AutoDetectZipcode(request.getRemoteAddr()));
mi.setAddress(address);
return mi;
} else {
return new MemberInfo();
}
}
private String AutoDetectZipcode(String remoteAddr) {
return "000000";
}
@RequestMapping(method = RequestMethod.GET)
public String form() {
return "account/creationForm";
}
@RequestMapping(method = RequestMethod.POST)
public String submit(@ModelAttribute("command") MemberInfo memberInfo,
BindingResult result) {
new MemberInfoValidator().validate(memberInfo, result);
if (result.hasErrors()) {
return "account/creationForm";
}
return "account/created";
}
}
CreateAccountController
equalsIgnoreCase
대소문자 구별없이 조건 체크함 (영문만 가능함)
view/account/created.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
계정 생성
계정 생성됨
- 아이디: ${command.id}
- 이름: ${command.name}
- 우편번호: ${command.address.zipcode}
- 주소: ${command.address.address1} ${command.address.address2}
view/account/creationForm.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
계정 생성
Spring에서 제공하는 커스텀 태그 사용
<spring:hasBindErrors name="command" />
<form:errors path="command" />에러 처리를 할 수 있는 기본적인 셋팅
'JSP > Spring' 카테고리의 다른 글
@PathVariable 을 이용한 주소 확장기능 (0) | 2012.03.06 |
---|---|
Spring 요청 URI 매칭 (0) | 2012.03.05 |
뷰에 모델 데이터 전달하기 (0) | 2012.03.05 |
Spring 컨트롤러 메서드의 리턴 타입 (0) | 2012.03.05 |
@RequestHeader 어노테이션을 이용한 헤더 매핑 (0) | 2012.03.05 |