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
- 국제화
- 오버로딩
- Spring
- paint
- HTML
- 전화걸기
- JavaScript
- struts2
- Menu
- Java
- mybatis
- JSP
- 메서드
- oracle
- AWT
- 이클립스
- Graphic
- 안드로이드
- Android
- 클래스
- 예외처리
- 기본
- 메소드
- 어노테이션
- OGNL
- 배열
- layout
- Eclips
- 생성자
- 에러페이지
Archives
- Today
- Total
note
Spring 커스텀 태그 / 로그인 유효성 체크 본문
파일 위치
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>defaultHtmlEscape</param-name> <param-value>false</param-value> </context-param> <servlet> <servlet-name>dispatcherInternal</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherInternal</servlet-name> <url-pattern>/jsp/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>dispatcherNonHtml</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcherNonHtml</servlet-name> <url-pattern>/download/*</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
dispatcherInternal-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 커스텀 태그 이용 --> <bean class="madvirus.spring.chap07.controller.LoginController"> <property name="authenticator"> <bean class="madvirus.spring.chap07.controller.MockAuthenticator"/> </property> </bean> <!-- View 글로벌 설정 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/viewjsp/" /> <property name="suffix" value=".jsp"/> </bean> <!-- 리소스 번들 지정 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>messages.validation</value> <value>messages.label</value> </list> </property> </bean> </beans>
AuthenticationException.java
package madvirus.spring.chap07.controller; public class AuthenticationException extends RuntimeException { private static final long serialVersionUID = 1L; public AuthenticationException(String message) { super(message); } }
Authenticator.java
package madvirus.spring.chap07.controller; public interface Authenticator { void authenticate(String id,String password); }
LoginCommand.java
package madvirus.spring.chap07.controller; public class LoginCommand { private String id; private String password; private String loginType; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getLoginType() { return loginType; } public void setLoginType(String loginType) { this.loginType = loginType; } }
LoginCommandValidator.java
package madvirus.spring.chap07.controller; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; public class LoginCommandValidator implements Validator{ public void validate(Object target, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "id", "required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "loginType", "required"); } public boolean supports(Class<?> clazz) { return LoginCommand.class.isAssignableFrom(clazz); } }
ValidationUtils.rejectIfEmptyOrWhitespace비어있거나 공백일 경우(?)
LoginController .java
package madvirus.spring.chap07.controller; import java.util.ArrayList; import java.util.List; 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("/login/login.do") public class LoginController { private Authenticator authenticator; @ModelAttribute("login") public LoginCommand formBacking() { return new LoginCommand(); } @RequestMapping(method = RequestMethod.GET) public String form() { return "loginForm"; } @RequestMapping(method = RequestMethod.POST) public String submit(@ModelAttribute("login") LoginCommand loginCommand, BindingResult result) { new LoginCommandValidator().validate(loginCommand, result); if (result.hasErrors()) { return "loginForm"; } try { authenticator.authenticate(loginCommand.getId(), loginCommand.getPassword()); return "loginSuccess"; } catch (AuthenticationException ex) { result.reject("invalidIdOrPassword", new Object[] { loginCommand.getId() }, null); return "loginForm"; } } @ModelAttribute("loginTypes") protected List<String> referenceData() throws Exception{ List<String> loginTypes = new ArrayList<String>(); loginTypes.add("일반회원"); loginTypes.add("기업회원"); loginTypes.add("헤드헌터 회원"); return loginTypes; } public void setAuthenticator(Authenticator authenticator){ this.authenticator = authenticator; } }
try/catch 부분에
예외 발생이 되면 loginform으로 다시 리턴되어지고
try 부분에서 실행이 되어지면은 success로 이동되어 진다
MockAuthenticator .java
package madvirus.spring.chap07.controller; public class MockAuthenticator implements Authenticator{ public void authenticate(String id, String password) { if(!id.equals("madvirus")) throw new AuthenticationException("invalid id"+id); } }
label_en.properties
login.form.title=Login Form login.form.type=Login Type login.form.id=ID login.form.password=Password login.form.submit=Login
label.properties
login.form.title=로그인 login.form.type=로그인 타입 login.form.id=회원Id login.form.password=비밀번호 login.form.submit=전송
validation_en.properties
required=required required.login.id=login id is required required.login.password=login password is requreid required.login.loginType=You have to select login type invalidIdOrPassword.login=Login id and password do not match. (You provided {0})
validation.properties
required=필수입니다. required.login.id=ID는 필수입니다. required.login.password=비밀번호는 필수입니다. required.login.loginType=로그인 타입을 선택하세요. invalidIdOrPassword.login=당신이 입력한 id {0}. 로그인 아이디와 비밀번호가 일치하지 않습니다.
viewjsp/loginForm.jsp
<%@ page contentType="text/html; charset=EUC-KR" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title><spring:message code="login.form.title"/></title> </head> <body> <form:form commandName="login"> <form:errors /> <p> <label for="loginType"><spring:message code="login.form.type" /></label> <form:select path="loginType" items="${loginTypes}" /> </p> <p> <label for="id"><spring:message code="login.form.id" /></label> <form:input id="id" path="id"/> <form:errors path="id" /> </p> <p> <label for="password"><spring:message code="login.form.password" /></label> <form:password id="password" path="password"/> <form:errors path="password" /> </p> <p> <input type="submit" value="<spring:message code="login.form.submit" />"> </p> </form:form> </body> </html>
spring:message가 label properties에 접근하여 메세지를 읽어 온다
viewjsp/loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>로그인 성공</title> </head> <body> 로그인에 성공했습니다. </body> </html>
'JSP > Spring' 카테고리의 다른 글
LocaleChangeInterceptor를 이용한 Locale 변경(국제화) (0) | 2012.03.07 |
---|---|
LocaleResolver를 이용한 Locale 변경(국제화) (0) | 2012.03.07 |
Spring 예외처리 (0) | 2012.03.06 |
MultipartFile 인터페이스 사용하여 파일업로드,다운로드(자카르타 라이브러리) (0) | 2012.03.06 |
@PathVariable 을 이용한 주소 확장기능 (0) | 2012.03.06 |