UserVO.java
package vo;
import java.io.Serializable;
public class UserVO implements Serializable{
private String userId;
private String userName;
private String passWord;
private String zipCode;
private String address;
private String email;
private String mobileNumber;
private String birthDay;
private String regiDt;
private String regiId;
private String modiDt;
private String modiId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public String getRegiDt() {
return regiDt;
}
public void setRegiDt(String regiDt) {
this.regiDt = regiDt;
}
public String getRegiId() {
return regiId;
}
public void setRegiId(String regiId) {
this.regiId = regiId;
}
public String getModiDt() {
return modiDt;
}
public void setModiDt(String modiDt) {
this.modiDt = modiDt;
}
public String getModiId() {
return modiId;
}
public void setModiId(String modiId) {
this.modiId = modiId;
}
}
|
LoginController.java
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import logic.UserInfo;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import vo.UserVO;
public class LoginController extends SimpleFormController {
private UserInfo userInfo = null;
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
//POST요청에 대한 처리
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
// TODO Auto-generated method stub
UserVO userVo = (UserVO)command;
System.out.println("userid : " + userVo.getUserId());
System.out.println("password : " + userVo.getPassWord());
try{
UserVO loginUser = userInfo.getUserInfo(userVo.getUserId(), userVo.getPassWord());
ModelAndView modelAndView = new ModelAndView(getSuccessView(), "loginUser", loginUser);
System.out.println("===>>> " + getSuccessView()); //loginSuccess
return modelAndView; //loginSuccess.jsp로 이동
}catch(DataRetrievalFailureException e){ //Dao에서 발생시킨 exception
System.out.println("exception message : " + e.toString());
//다시 로그인 화면으로 이동
return showForm(request, response, errors); //login.jsp로 이동
}
}
}
====================================== log ======================================
userid : test
password : 1234
exception message : org.springframework.dao.DataRetrievalFailureException: ID 또는 PASSWORD가 정확하지 않습니다.
userid : htc
password : htc11
===>>> loginSuccess
|
.SimpleFormController : 2가지(POST/GET)방식의 전송 지원 -> 즉 POST/GET에 따라 다르게 처리
-POST : onSubmit() 오버라이딩
-GET :
.SimpleFormController 클래스에 설정 가능한 프로퍼티
프로퍼티 |
설명 |
sessionForm |
세션에 command를 유지할지 말지(true/false)를 결정 |
commandName |
리퀘스트에 바인드하는 command명 |
commandClass |
command 클래스의 완전 수식명 |
validator |
command를 검증하는 validator 클래스의 참조 |
formView |
화면 표시 때,
또는 검증에러가 발생한 경우 이동 되는 View |
successView |
정보 송신이 정상적으로 종료한 경우 이동 되는 View |
/* 참조 : myHome02-servlet.xml */
UserInfo.java
package logic;
import vo.UserVO;
public interface UserInfo {
UserVO getUserInfo(String userId, String password);
}
|
UserInfoImpl.java
package logic;
import dao.UserDao;
import vo.UserVO;
public class UserInfoImpl implements UserInfo {
private UserDao userDao = null;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public UserVO getUserInfo(String userId, String password) {
// TODO Auto-generated method stub
return userDao.checkLoginInfo(userId, password);
}
}
|
UserDao.java
package dao;
import vo.UserVO;
public interface UserDao {
UserVO checkLoginInfo(String userId, String password);
}
|
UserDaoImpl.java
package dao;
import java.util.HashMap;
import java.util.Map;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import vo.UserVO;
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao {
@Override
public UserVO checkLoginInfo(String userId, String password) {
// TODO Auto-generated method stub
Map map = new HashMap();
map.put("userId", userId);
map.put("passWord", password);
UserVO userVo = null;
userVo = (UserVO)getSqlMapClientTemplate().queryForObject("user.checkLoginInfo", map);
//프로그램의 흐름을 파악하기 위해 데이터가 없을 시 exception
if(userVo == null || userVo.getUserId().equals("") || userVo.getUserId() == null){
throw new DataRetrievalFailureException("ID 또는 PASSWORD가 정확하지 않습니다.");
}
return userVo;
}
}
|
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">
<display-name>myHome02</display-name>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC-KR</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>myHome02</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myHome02</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
|
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- 1. dataSource 생성 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@000.000.000.000:1521:orcl"></property>
<property name="username" value="XXXXX"></property>
<property name="password" value="YYYYY"></property>
</bean>
<!-- 2. ibatis 코딩을 지원해주는 SqlMapClient 빈 생성 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml"></property>
</bean>
<!-- 3. 공통 빈 생성 - 그외의 빈은 myHome02-servlet.xml에서 설정 -->
</beans>
|
myHome02-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- HandlerMapping 설정 : SimpleUrlHandlerMapping -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/user.html">loginController</prop>
</props>
</property>
</bean>
<!-- ViewResolver 설정 : InternalResourceViewResolver -->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 각종 빈 -->
<bean id="loginController" class="controller.LoginController">
<property name="commandName"><value>user</value></property>
<property name="commandClass"><value>vo.UserVO</value></property>
<property name="formView"><value>login</value></property>
<property name="successView"><value>loginSuccess</value></property>
<property name="userInfo" ref="userInfo"></property>
</bean>
<bean id="userInfo" class="logic.UserInfoImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="dao.UserDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
</beans>
|
.SimpleUrlHandlerMapping 클래스
-Properties형의 mappings 프로퍼티를 갖고 있음
-리퀘스트 url과 리퀘스트 controller를 관련지은 Properties 오브젝트를 설정
.InternalResourceViewResolver 클래스
-viewClass, prefix, suffix 프로퍼티 설정이 가능
-viewClass : View 인터페이스를 구현한 클래스를 정의
--JstlView 클래스 : JSTL을 사용하여 JSP 페이지 작성을 지원하는 View 인터페이스의 구현 클래스
-prefix : 전문자
-suffix : 후문자
.SimpleFormController 클래스에 설정 가능한 프로퍼티
프로퍼티 |
설명 |
sessionForm |
세션에 command를 유지할지 말지(true/false)를 결정 |
commandName |
리퀘스트에 바인드하는 command명 |
commandClass |
command 클래스의 완전 수식명 |
validator |
command를 검증하는 validator 클래스의 참조 |
formView |
화면 표시 때,
또는 검증에러가 발생한 경우 이동 되는 View |
successView |
정보 송신이 정상적으로 종료한 경우 이동 되는 View |
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<sqlMap resource="../classes/sql/oracle/user.xml"/>
</sqlMapConfig>
|
user.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<!-- Mapper 파일 -->
<!--
이름 충돌을 방지하기 위해 namespace를 사용
SqlMapConfig.xml에 <settings useStatementNamespaces="true"/> 설정이 지정되어 있는 경우
sql구문을 찾을 때 반드시 namespace값을 포함해야 한다.
-->
<sqlMap namespace="user">
<select id="checkLoginInfo" parameterClass="java.util.Map" resultClass="vo.UserVO">
SELECT U.USERID,
U.USERNAME,
U.PASSWORD,
U.ZIPCODE,
U.ADDRESS,
U.EMAIL,
U.MOBILENUMBER,
U.BIRTHDAY,
TO_CHAR(U.REGIDT, 'YYYY-MM-DD') AS REGIDT,
U.REGIID,
TO_CHAR(U.MODIDT, 'YYYY-MM-DD') AS MODIDT,
U.MODIID
FROM USERS U
WHERE 1=1
AND U.USERID = #userId#
AND U.PASSWORD = #passWord#
</select>
</sqlMap>
|
login.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>login view</title>
</head>
<body>
<h2>로그인 화면</h2>
<form action="user.html" method="post">
<table>
<tr>
<td>유저ID</td>
<td>
<spring:bind path="user.userId">
<input type="text" name="userId" value=""/>
</spring:bind>
</td>
</tr>
<tr>
<td>패스워드</td>
<td>
<spring:bind path="user.passWord">
<input type="password" name="passWord" value=""/>
</spring:bind>
</td>
</tr>
<tr>
<td><input type="submit" value="로그인"/></td>
<td><input type="reset" value="리셋"/></td>
</tr>
</table>
</form>
</body>
</html> |
.<spring:bind> 태그의 속성 - (spring에서 제공하는 custom 태그)
속성 |
필수 |
설명 |
path |
true |
command 또는 command의 프로퍼티의 경로 |
ignoreNestedPath |
false |
nested된 프로퍼티의 사용 허가 |
htmlEscape |
false |
HTML escape의 사용 유무 |
loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>login success</title>
</head>
<body>
<h2>로그인 화면</h2>
안녕하세요. <c:out value="${loginUser.userName}"></c:out> 님.
</body>
</html> |
[참고자료] Spring 2.5 실무 프로그래밍(성윤정)