Dev/Struts

Struts2/iBatis - 회원가입, 로그인, 비밀번호찾기 기능 구현

창문닦이 2019. 3. 26. 20:46

1. DB 테이블 만들기


create table mem

(userId varchar2(10) not null,

userPwd varchar2(10) not null,

userName varchar2(20) not null,

userBirth date,

userTel varchar2(20),

constraint pk_mem_userId primary key(userId));


2. sqlmap 생성

<?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 -->

<sqlMap namespace="member">

<!-- 회원가입 -->

<insert id="insertData" parameterClass="com.join.MemberDTO">

insert into mem (userId,userPwd,userName,userBirth,userTel)

values (#userId#,#userPwd#,#userName#,#userBirth#,#userTel#)

</insert>

<!-- 회원정보찾기 -->

<select id="getId" resultClass="com.join.MemberDTO">

select userId,userPwd,userName,to_char(userBirth,'yyyy-mm-dd') userBirth,userTel

from mem where userId=#userId#

</select>

<!-- 로그인 -->

<select id="getReadData" resultClass="com.join.MemberDTO">

select userId,userPwd,userName,to_char(userBirth,'yyyy-mm-dd') userBirth,userTel

from mem where userId=#userId# and userPwd=#userPwd#

</select>

<!-- 비밀번호찾기 -->

<select id="getPwd" resultClass="com.join.MemberDTO">

select userId,userPwd,userName,to_char(userBirth,'yyyy-mm-dd') userBirth,userTel

from mem where userId=#userId# and userTel=#userTel#

</select>

</sqlMap>

3. sqlmap config 등록

<sqlMap resource="com/util/sqlMap/member_sqlMap.xml"/>

4. struts-member.xml 작성

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="member" extends="struts-default" namespace="/mem" >       

<!-- 회원가입 -->

<action name="created" method="created" class="com.join.MemberAction">

<result name="input">/member/created.jsp</result>

<!-- 작성완료시 반환값이 success -->

<result name="success" type="redirectAction">login</result>

</action>

<!-- 로그인 -->

<action name="login" method="login" class="com.join.MemberAction">

<result name="input">/member/login.jsp</result>

<interceptor-ref name="prepare"/>

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="params"/>

<result name="success" type="chain">

<param name="actionName">list</param>

<param name="namespace">/img</param>

</result>

</action>

<!-- 로그아웃 -->

<action name="logout" method="logout" class="com.join.MemberAction">

<result name="success" type="redirectAction">login</result>

</action>

<!-- 비밀번호찾기 -->

<action name="searchpw" method="searchpw" class="com.join.MemberAction">

<result name="input">/member/searchpw.jsp</result>

</action>

</package>

</struts>

5. struts.xml 에 등록

<include file="struts-member.xml"/>

6. Member DTO 생성

package com.join;

public class MemberDTO {

private String userId;

private String userPwd;

private String userName;

private String userBirth;

private String userTel;

private String mode;

getter, setter 생성

}

7. Member Action 생성

package com.join;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

import com.opensymphony.xwork2.Preparable;

import com.util.dao.CommonDAO;

import com.util.dao.CommonDAOImpl;

public class MemberAction extends ActionSupport

implements Preparable,ModelDriven<MemberDTO>{

private static final long serialVersionUID = 1L;

private MemberDTO dto;

private String message;

public MemberDTO getDto() {

return dto;

}

@Override

public MemberDTO getModel() {

return dto;

}

@Override

public void prepare() throws Exception {

dto = new MemberDTO();

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

//회원가입

public String created() throws Exception {

//dto.getMode()==null||dto.getMode().equals("") 순서 바뀌면 오류남

if(dto==null||dto.getMode()==null||dto.getMode().equals("")){

return INPUT;

}

HttpServletRequest request = ServletActionContext.getRequest();

CommonDAO dao = CommonDAOImpl.getInstance();

//입력

if(dao.getReadData("member.getId",dto)==null){

dao.insertData("member.insertData", dto);

dao=null;

}else{

request.setAttribute("message", "동일한 아이디가 존재합니다!!");

return INPUT;

}

return SUCCESS;

}

//로그인

public String login() throws Exception {

if(dto==null||dto.getMode()==null||dto.getMode().equals("")){

return INPUT;

}

HttpServletRequest request = ServletActionContext.getRequest();

CommonDAO dao = CommonDAOImpl.getInstance();

MemberDTO loginDTO = (MemberDTO)dao.getReadData("member.getReadData",dto);

//dto==null일 경우 아이디가 없음

if(loginDTO==null){

request.setAttribute("message", "아이디 또는 패스워드를 정확히 입력하세요!");

return INPUT;

}

HttpSession session = request.getSession();

session.setAttribute("MemberDTO", loginDTO);

return SUCCESS;

}

//로그아웃시

public String logout() throws Exception {

HttpServletRequest request = ServletActionContext.getRequest();

HttpSession session = request.getSession();

session.removeAttribute("dto");

session.removeAttribute("MemberDTO");

session.invalidate();

return SUCCESS;

}

//비밀번호찾기 페이지

public String searchpw() throws Exception {

if(dto==null||dto.getMode()==null||dto.getMode().equals("")){

return INPUT;

}

CommonDAO dao = CommonDAOImpl.getInstance();

HttpServletRequest request = ServletActionContext.getRequest();

MemberDTO searchDTO = (MemberDTO)dao.getReadData("member.getPwd",dto);

//dto==null일 경우 해당정보 없음

if(searchDTO==null){

request.setAttribute("message", "해당하는 정보가 없어요!");

}else{

String str = "비밀번호는 ["+searchDTO.getUserPwd()+"]입니다.";

request.setAttribute("message", str);

}

return INPUT;

}

}

8. Created.jsp - 회원가입 페이지

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>

<!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>회원 가입</title>

<link rel="stylesheet" href="<%=cp %>/member/data/style.css" type="text/css"/>

<link rel="stylesheet" href="<%=cp %>/member/data/created.css" type="text/css"/>

<script type="text/javascript" src="<%=cp %>/member/data/util.js"></script>

<script type="text/javascript">

function sendIt(){

var f = document.myForm;

str = f.userId.value;

if(!str){

alert("아이디를 입력하세요");

f.userId.focus();

return;

}

f.userId.value = str;

str = f.userPwd.value;

if(!str){

alert("패스워드를 입력하세요");

f.userPwd.focus();

return;

}

f.userPwd.value = str;

str = f.userName.value;

if(!str){

alert("이름을 입력하세요");

f.userName.focus();

return;

}

f.userName.value = str;

str = f.userBirth.value;

if(!str){

alert("생년월일을 입력하세요");

f.userBirth.focus();

return;

}

f.userBirth.value = str;

str = f.userTel.value;

if(!str){

alert("연락처를 입력하세요");

f.userTel.focus();

return;

}

f.userTel.value = str;

f.action = "<%=cp %>/mem/created.action";

f.submit();

}

</script>

</head>

<body>

<div id="bbs">

<div id="bbs_title">

회원가입

</div>

<form action="" method="post" name="myForm">

<div id="bbsCreated">

<div class="bbsCreated_bottomLine">

<dl>

<dt>아&nbsp;이&nbsp;디</dt>

<dd>

<input type="text" name="userId" size="35" maxlength="20" class="boxTF"

<c:if test="${mode=='updateok'}"> readonly="readonly" value="${dto.userId }"</c:if>

/>

</dd>

</dl>

</div>

<div class="bbsCreated_bottomLine">

<dl>

<dt>패스워드</dt>

<dd><input type="password" name="userPwd" value="${dto.userPwd}" size="35" maxlength="20" class="boxTF"/></dd>

</dl>

</div>

<div class="bbsCreated_bottomLine">

<dl>

<dt>이&nbsp;&nbsp;&nbsp;&nbsp;름</dt>

<dd><input type="text" name="userName" value="${dto.userName}" size="35" maxlength="50" class="boxTF"/></dd>

</dl>

</div>

<div class="bbsCreated_bottomLine">

<dl>

<dt>생&nbsp;&nbsp;&nbsp;&nbsp;일</dt>

<dd><input type="text" name="userBirth" value="${dto.userBirth}" size="35" maxlength="50" class="boxTF"/></dd>

</dl>

</div>

<div class="bbsCreated_bottomLine">

<dl>

<dt>전&nbsp;&nbsp;&nbsp;&nbsp;화</dt>

<dd><input type="text" name="userTel" value="${dto.userTel}" size="35" maxlength="50" class="boxTF"/></dd>

</dl>

</div>

</div>

<div id="bbsCreated_footer">

<input type="hidden" name="mode" value="save">

<input type="button" value="가입하기" class="btn2"

onclick="sendIt();"/>

<input type="reset" value="다시입력" class="btn2"

onclick="document.myForm.userId.focus();"/>

<input type="button" value="로그인" class="btn2"

onclick="javascript:location.href='<%=cp %>/mem/login.action';"/>

오류메세지 출력용

<div style="height: 30px; ">

<font color="red"><b>${message }</b></font>

</div>

</div>

</form>

</div>

</body>

</html>

회원가입 페이지 출력

 이미 등록된 아이디일 경우 회원가입 오류 메세지 출력

9. login.jsp - 로그인페이지

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>

<!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>로그인</title>

<link rel="stylesheet" href="<%=cp %>/member/data/style.css" type="text/css"/>

<script type="text/javascript">

function login(){

var f = document.myForm;

if(!f.userId.value){

alert("아이디를 입력하세요!");

f.userId.focus();

return;

}

if(!f.userPwd.value){

alert("패스워드를 입력하세요!");

f.userPwd.focus();

return;

}

f.action = "<%=cp %>/mem/login.action";

f.submit();

}

</script>

</head>

<body>

<br/><br/>

<form action="" method="post" name="myForm">

<table align="center" cellpadding="0" cellspacing="0">

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="30">

<td colspan="2" align="center"><b>로그인</b></td>

</tr>

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="25">

<td width="80" bgcolor="#e6e4e6" align="center">아이디</td>

<td width="120" style="padding-left: 5px;">

<input type="text" name="userId" maxlength="10" size="15"

style="width: 150px; height: 22px;"/>

</td>

</tr>

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="25">

<td width="80" bgcolor="#e6e4e6" align="center">패스워드</td>

<td width="120" style="padding-left: 5px;">

<input type="password" name="userPwd" maxlength="10" size="15"

style="width: 150px; height: 22px;"/>

</td>

</tr>

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="30">

<td colspan="2" align="center">

<input type="hidden" name="mode" value="login">

<input type="button" value="로그인" class="btn2" onclick="login();">

<input type="button" value="회원가입" class="btn2"

onclick="javascript:location.href='<%=cp %>/mem/created.action';">

<input type="button" value="게시판" class="btn2"

onclick="javascript:location.href='<%=cp %>/img/list.action';">

</td>

</tr>

오류메세지 출력용

<tr height="30">

<td colspan="2" align="center">

<font color="red"><b>${message }</b></font>

</td>

</tr>

<c:choose>

<c:when test="${!empty message}">

<tr height="1"><td colspan="2" bgcolor="#cccccc"></tr>

<tr><td align="center" colspan="2">

<a href="javascript:location.href='<%=cp %>/mem/searchpw.action';">비밀번호 찾기</a>

</td></tr>

<tr height="1"><td colspan="2" bgcolor="#cccccc"></tr>

</c:when>

</c:choose>

</table>

</form>

</body>

</html>

로그인 페이지 출력

아이디/패스워드 오류 시 출력

10. searchpw.jsp - 비밀번호 찾기 페이지

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

%>

<!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>비밀번호찾기</title>

<link rel="stylesheet" href="<%=cp %>/member/data/style.css" type="text/css"/>

<script type="text/javascript">

function sendIt(){

var f = document.myForm;

if(!f.userId.value){

alert("아이디를 입력하세요!");

f.userId.focus();

return;

}

if(!f.userTel.value){

alert("연락처를 입력하세요!");

f.userTel.focus();

return;

}

f.action = "<%=cp %>/mem/searchpw.action";

f.submit();

}

</script>

</head>

<body>

<br/><br/>

<form action="" method="post" name="myForm">

<table align="center" cellpadding="0" cellspacing="0">

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="30">

<td colspan="2" align="center"><b>비밀번호 검색</b></td>

</tr>

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="25">

<td width="80" bgcolor="#e6e4e6" align="center">아이디</td>

<td width="120" style="padding-left: 5px;">

<input type="text" name="userId" maxlength="10" size="15"

style="width: 150px; height: 22px;"/>

</td>

</tr>

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="25">

<td width="80" bgcolor="#e6e4e6" align="center">전화번호</td>

<td width="120" style="padding-left: 5px;">

<input type="text" name="userTel" maxlength="13" size="25"

style="width: 150px; height: 22px;"/>

</td>

</tr>

<tr height="2"><td colspan="2" bgcolor="#cccccc"></tr>

<tr height="30">

<td colspan="2" align="center">

<input type="hidden" value="save" name="mode">

<input type="button" value="확인" class="btn2" onclick="sendIt();">

<input type="button" value="취소" class="btn2"

onclick="javascript:location.href='<%=cp %>/mem/login.action';">

<input type="button" value="회원가입" class="btn2"

onclick="javascript:location.href='<%=cp %>/mem/created.action’;">

</td>

</tr>

오류메세지 출력용

<tr height="30">

<td colspan="2" align="center">

<font color="red"><b>${message }</b></font>

</td>

</tr>

<c:choose>

<c:when test="${!empty message}">

<tr height="1"><td colspan="2" bgcolor="#cccccc"></tr>

<tr><td align="center" colspan="2">

<a href="<%=cp %>/mem/searchpw.action">비밀번호 찾기</a>

</td></tr>

<tr height="1"><td colspan="2" bgcolor="#cccccc"></tr>

</c:when>

</c:choose>

</table>

</form>

</body>

</html>

비밀번호 검색 페이지 출력

비밀번호 출력