로그인처리
시작 : login_form.jsp(login.do요청)
Ctr : login(id,password,HttpSession session) : ModelAndView
service : getMemberById() : MDTO
View : 성공 : /res/login_success.jsp
실패 : 오류 : /res/error.jsp
id/pwd 틀린경우 : /login_form.jsp
로그아웃 처리
시작 : logout.do로 요청시(menu.jsp, login_success.jsp
Ctr : logout(HttpSession)
View : 성공/실패 - login_form.jsp
<?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">
<sqlMap>
<typeAlias alias="mdto" type="member.dto.MemberDTO"/>
<sql id="select" >
select id,
password,
name,
register_number1 as registerNumber1,
register_number2 as registerNumber2,
mileage
from member
</sql>
<resultMap class="mdto" id="memberRM">
<result property="id" columnIndex="1"/>
<result property="password" columnIndex="2"/>
<result property="name" columnIndex="3"/>
<result property="registerNumber1" columnIndex="4"/>
<result property="registerNumber2" columnIndex="5"/>
<result property="mileage" columnIndex="6" nullValue="-1"/>
</resultMap>
<select id="selectMemberById" parameterClass="string" resultMap="memberRM">
<include refid="select"/>
where id=#id#
</select>
<select id="selectMemberByRegisterNumber" parameterClass="hashmap" resultMap="memberRM">
<include refid="select"/>
where register_Number1=#registerNumber1#
and register_Number2=#registerNumber2#
</select>
<insert id="insertMember" parameterClass="mdto">
insert into member
(id, password, name, register_number1, register_number2, mileage)
values(#id#,#password#,#name#,#registerNumber1#,#registerNumber2#,#mileage#)
</insert>
<select id="selectAllMember" resultMap="memberRM">
<include refid="select"/>
</select>
</sqlMap>
<!-- xxxx.insert("insertMember", mdto); -->
<!-- Object obj = xxx.queryForObject("selectMemberById","abcde"); 추후 MemberDTO로 캐스팅해서 사용 -->
package member.controller;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import member.dto.MemberDTO;
import member.exception.DuplicatedIdException;
import member.exception.DuplicatedRegisterNumberException;
import memver.model.service.MemberManageService;
@Controller
public class MemberController {
private MemberManageService service;
public MemberController(MemberManageService service){
this.service = service;
}
@RequestMapping(value="/registerMember.do")
public ModelAndView registerMember(MemberDTO mdto){
ModelAndView mv = null;
mdto.setMileage(1000);
try {
System.out.println(mdto);
service.registerMember(mdto);
System.out.println("****************");
mv = new ModelAndView("/res/register_success","mdto",mdto);
} catch (Exception e) {
e.printStackTrace();
mv = new ModelAndView("/res/error","error_message",e.getMessage());
}
return mv;
}
@RequestMapping(value="/getAllMemberInfo.do")
public ModelAndView getAllMemberInfo(){
ModelAndView mv = null;
try{
System.out.println("getMemberList()실행");
ArrayList<MemberDTO> list = service.getMemberList();
mv = new ModelAndView("/member_list","member_list",list);
}catch (Exception e) {
mv = new ModelAndView("/res/error", "error_message",e.getMessage());
}
return mv;
}
@RequestMapping(value="/login.do")
public ModelAndView login(String id, String password, HttpSession session){
ModelAndView mv = null;
try {
MemberDTO mdto = service.getMemberById(id);
if(mdto!=null){
if(mdto.getPassword().equals(password)){
System.out.println("login_success.jsp실행");
session.setAttribute("memberDTO", mdto);
mv = new ModelAndView("/res/login_success");
}else{
mv = new ModelAndView("/login_form","error_message",password+"는 없는 비밀번호입니다.");
}
}else{
mv = new ModelAndView("/login_form","error_message",id+"는 없는 아이디입니다.");
}
} catch (SQLException e) {
e.printStackTrace();
mv = new ModelAndView("/res/error","error_message",e.getMessage());
}
return mv;
}
@RequestMapping(value="/logout.do")
public ModelAndView logout(HttpSession session){
ModelAndView mv = null;
MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO");
if(mdto!=null){
mv = new ModelAndView("/login_form");
session.invalidate();
}else{
mv = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다.");
}
return mv;
}
}
package member.dto;
public class MemberDTO {
private String id;
private String password;
private String name;
private String registerNumber1;
private String registerNumber2;
private int mileage;
public MemberDTO() {
super();
}
public MemberDTO(String id, String password, String name,
String registerNumber1, String registerNumber2, int mileage) {
super();
this.id = id;
this.password = password;
this.name = name;
this.registerNumber1 = registerNumber1;
this.registerNumber2 = registerNumber2;
this.mileage = mileage;
}
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegisterNumber1() {
return registerNumber1;
}
public void setRegisterNumber1(String registerNumber1) {
this.registerNumber1 = registerNumber1;
}
public String getRegisterNumber2() {
return registerNumber2;
}
public void setRegisterNumber2(String registerNumber2) {
this.registerNumber2 = registerNumber2;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
@Override
public String toString() {
return "MemberDTO [id=" + id + ", password=" + password + ", name="
+ name + ", registerNumber1=" + registerNumber1
+ ", registerNumber2=" + registerNumber2 + ", mileage="
+ mileage + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + mileage;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((registerNumber1 == null) ? 0 : registerNumber1.hashCode());
result = prime * result
+ ((registerNumber2 == null) ? 0 : registerNumber2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MemberDTO other = (MemberDTO) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (mileage != other.mileage)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (registerNumber1 == null) {
if (other.registerNumber1 != null)
return false;
} else if (!registerNumber1.equals(other.registerNumber1))
return false;
if (registerNumber2 == null) {
if (other.registerNumber2 != null)
return false;
} else if (!registerNumber2.equals(other.registerNumber2))
return false;
return true;
}
}
package member.exception;
public class DuplicatedIdException extends Exception{
public DuplicatedIdException(){}
public DuplicatedIdException(String message){
super(message);
}
}
package member.exception;
public class DuplicatedRegisterNumberException extends Exception{
public DuplicatedRegisterNumberException(){}
public DuplicatedRegisterNumberException(String message){
super(message);
}
}
package member.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import member.dto.MemberDTO;
public class MemberDAO {
private SqlMapClientTemplate sqlMap;
public MemberDAO(SqlMapClientTemplate sqlMap){
this.sqlMap = sqlMap;
}
public MemberDTO selectMemberById(String id) {
// TODO Auto-generated method stub
return (MemberDTO) sqlMap.queryForObject("selectMemberById", id);
}
//주민번호로 조회
public MemberDTO selectMemberByRegisterNumber(String rN1, String rN2) throws SQLException{
HashMap<String, String> map = new HashMap<String, String>();
map.put("registerNumber1", rN1);
map.put("registerNumber2", rN2);
System.out.println("selectMemberByRegisterNumber ");
return (MemberDTO)sqlMap.queryForObject("selectMemberByRegisterNumber",map);
}
//회원데이터 insert메소드
public Object insertMember(MemberDTO mdto) throws SQLException{
return sqlMap.insert("insertMember", mdto);
}
public ArrayList<MemberDTO> selectAllMember() {
return (ArrayList<MemberDTO>) sqlMap.queryForList("selectAllMember");
}
}
package memver.model.service;
import java.sql.SQLException;
import java.util.ArrayList;
import member.dto.MemberDTO;
import member.exception.DuplicatedIdException;
import member.exception.DuplicatedRegisterNumberException;
import member.model.dao.MemberDAO;
//회원 관리 비지니스 로직을 처리하는 Business Service 클래스
public class MemberManageService {
private MemberDAO dao;
public MemberManageService(MemberDAO dao){
this.dao = dao;
}
//가입 처리 메소드
public void registerMember(MemberDTO mto)throws SQLException, DuplicatedIdException, DuplicatedRegisterNumberException{
//1. 등록된 ID가 있는 지 체크
// - 이미 등록된 ID라면 DuplicatedIdException를 발생시킨다.
if(dao.selectMemberById(mto.getId())!=null){
throw new DuplicatedIdException(mto.getId()+"는 이미등록된 아이디 입니다.");
}
//2. 등록된 주민번호가 있는 지 체크
// - 이미 등록된 주민번호라면 DuplicatedRegisterNumberException 발생시킨다.
if(dao.selectMemberByRegisterNumber(mto.getRegisterNumber1(), mto.getRegisterNumber2())!=null){
throw new DuplicatedRegisterNumberException("는 이미 가입된 주민번호 입니다.");
}
//3. 디비에 회원정보 등록 처리
dao.insertMember(mto);
}
//전체 회원목록 조회
public ArrayList<MemberDTO> getMemberList() throws SQLException{
return dao.selectAllMember();
}
// //마일리지 범위로 조회
// public ArrayList<MemberDTO> selectMemberByMileageRange(int startMileage, int endMileage)throws SQLException {
// return dao.selectMemberByMileageRange(startMileage, endMileage);
// }
//ID로 회원 목록조회
public MemberDTO getMemberById(String id) throws SQLException{
return dao.selectMemberById(id);
}
// public void modifyMember(MemberDTO mto) throws SQLException{
// dao.updateMember(mto);
//
// }
// public void removeMemberById(String id)throws SQLException {
// dao.deleteMemberById(id);
//
// }
// public ArrayList<MemberDTO> getSearchByMemberName(String name) throws SQLException{
// return dao.searchByMemberName(name);
//
// }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="/menu.jsp"/>
<c:if test="${requestScope.error_message!=null }">
실행 도중 오류가 발생했습니다.<br>
오류 내용 ${error_message }
</c:if>
<a href='/${initParam.context_root }/index.jsp'>메인페이지로 이동</a>
</body>
</html>
<%@page import="member.dto.MemberDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="/menu.jsp"/>
${sessionScope.memberDTO.id } 님환영합니다.<p>
이름 : ${sessionScope.memberDTO.name }<br>
패스워드 : ${sessionScope.memberDTO.password }<br>
주민번호 : ${sessionScope.memberDTO.registerNumber1 }-${sessionScope.memberDTO.registerNumber2 }<br>
마일리지 : ${sessionScope.memberDTO.mileage }<br>
<a href='/${initParam.context_root }/logout.do'>로그아웃</a>
<a href='/${initParam.context_root }/controller?command=modify_member_list'>회원정보조회(로그인한 회원의 정보)</a>
<!--<input type="button" value="회원정보조회" onClick="location.href='/${initParam.context_root }/getMemberInfo'"> -->
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<a href='/${initParam.context_root }/index.jsp'>메인페이지</a>
<c:choose>
<c:when test="${sessionScope.memberDTO == null }">
<a href='/${initParam.context_root }/register_form.jsp'>회원가입폼</a>
<a href="/${initParam.context_root }/login_form.jsp">로그인폼</a>
<a href="/${initParam.context_root }/search_form.jsp">검색</a>
</c:when>
<c:otherwise>
<a href='/${initParam.context_root }/controller?command=logout'>로그아웃</a>
<a href="/${initParam.context_root }/controller?command=modify_member_list">회원정보조회</a>
<a href="/${initParam.context_root }/res/modify_form.jsp">회원정보 수정</a>
<a href="/${initParam.context_root }/controller?command=member_remove">회원 탈퇴</a>
</c:otherwise>
</c:choose>
<a href="/${initParam.context_root }/getAllMemberInfo.do">전체 회원정보 조회</a>
<hr width="700" align="left" noshade="noshade">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:if test="${sessionScope.memberDTO != null }">
<jsp:forward page="/res/member_details.jsp"/>
</c:if>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function loginCheck(){
var id = document.login_form.id;
var pwd = document.login_form.password;
if(id.value==""){
alert("아이디를 입력해 주세요.");
id.focus();
return false;
}
if(pwd.value==""){
alert("비밀번호를 입력해 주세요.");
pwd.focus();
return false;
}
}
</script>
</head>
<c:if test="${requestScope.error_message!=null }">
<font color="red">${requestScope.error_message }</font>
</c:if><br>
<body>
<jsp:include page="/menu.jsp"/>
<form action="/${initParam.context_root }/login.do" method="post" name="login_form" onsubmit="return loginCheck()">
<input type="hidden" name="command" value="login">
<table>
<tr>
<td><h2>로그인</h2><p></td>
</tr>
<tr>
<td>id</td><td><input type="text" name="id"></td>
</tr>
<tr>
<td>password</td><td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="로그인"><input type="reset" value="초기화"></td>
</tr>
</table>
</form>
</body>
</html>
* 결과
'프로그래밍 > Spring MVC' 카테고리의 다른 글
FileUpload(파일 업로드) (0) | 2012.07.04 |
---|---|
회원정보 수정 폼, 회원정보 수정 처리, 이름으로 검색, 마일리지 범위로 검색(Spring MVC, ibatis 이용) (0) | 2012.07.02 |
spring, jstl, ibatis 연동하여 사용하기 실습- member_springmvc (0) | 2012.06.29 |
Annotation - HttpServletRequest, HttpServletResponse, HttpSession, @CookieValue[실습] (0) | 2012.06.28 |
@RequestParam Annotation 사용(value, required, defaultValue)[실습] (0) | 2012.06.28 |