Dev/Struts

Struts1/iBatis - 회원가입 및 수정/로그인/비밀번호찾기 기능 구현

창문닦이 2019. 3. 23. 20:30

Struts1와 iBatis 구조

1. namespace가 member인 SQLMAP을 찾아가서 id가 getReadData인 SQL문을 실행하고 반환값 저장

2. 클라이언트의 요청 URL에 join이 있을 경우 MemberAction클래스에 가서 파라미터값으로 전달받은 method를 실행. ( *.do에 대한 url패턴은 web.xml파일에 등록함, form-bean으로 MemberForm을 설정해놨으므로 이 데이터도 함께 전송)

3-1. Action 클래스의 forward 반환값이 'login'이라면 login.jsp 페이지를 연결

3-2. Action 클래스의 forward 반환값이 'login_ok'이라면 게시판 리스트 페이지를 연결

3-2. Action 클래스의 forward 반환값이 'searchpw'이라면 searchpw.jsp 페이지를 연결

1. DataBase 테이블 생성

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. iBatis - sqlMapConfig 등록

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


3. iBatis - member-sqlMap.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라고 함 -->

<sqlMap namespace="member">

<!-- 회원가입 -->

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

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

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

</insert>

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

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

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

from mem where userId=#userId#

</select>

<!-- 로그인 -->

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

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.MemberForm">

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

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

</select>

<!-- 회원정보수정 -->

<update id="updateData" parameterClass="com.join.MemberForm">

update mem set userName=#userName#,userPwd=#userPwd#,userBirth=#userBirth#,userTel=#userTel#

where userId=#userId#

</update>

</sqlMap>

 4. Struts1 - web.xml 등록

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>

/WEB-INF/struts-config.xml,

/WEB-INF/struts-config_test.xml,

/WEB-INF/struts-config_board.xml,

/WEB-INF/struts-config_boardTest.xml,

/WEB-INF/struts-config_fileTest.xml,

/WEB-INF/struts-config_member.xml,

</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

 <servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

5. Struts1 - struts-config_member.xml 포워드 매핑정보 등록

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

<!DOCTYPE struts-config PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"

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

<struts-config>

<form-beans>

<form-bean name="MemberForm" type="com.join.MemberForm"/>

</form-beans>

<action-mappings>

<!-- /join.do가 오면  MemberAction 클래스로 가라. -->

<!-- parameter는 변수. 찾아가야하는 메소드명을 기재 -->

<action path="/join" type="com.join.MemberAction"

name="MemberForm" scope="request" parameter="method">

<!-- 컨트롤러의 역할. 분배기 -->

<forward name="created" path="/member/created.jsp"/>

<forward name="created_ok" redirect="true" path="/join.do?method=login"/>

<forward name="login" path="/member/login.jsp"/>

<forward name="login_ok" redirect="true" path="/boardTest.do?method=list"/>

<forward name="searchpw" path="/member/searchpw.jsp"/>

</action>

</action-mappings>

</struts-config>         

6. MemberForm 클래스 생성

package com.join;

import org.apache.struts.action.ActionForm;

public class MemberForm extends ActionForm {

private static final long serialVersionUID = 1L;

private String userId;

private String userPwd;

private String userName;

private String userBirth;

private String userTel;

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public String getUserPwd() {

return userPwd;

}

public void setUserPwd(String userPwd) {

this.userPwd = userPwd;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserBirth() {

return userBirth;

}

public void setUserBirth(String userBirth) {

this.userBirth = userBirth;

}

public String getUserTel() {

return userTel;

}

public void setUserTel(String userTel) {

this.userTel = userTel;

}

}

7. MemberAction 클래스 생성

package com.join;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

import com.util.dao.CommonDAO;

import com.util.dao.CommonDAOImpl;

public class MemberAction extends DispatchAction{

//회원가입,수정 페이지 포워드

public ActionForward created(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

//입력/수정구분 파라미터

String mode = request.getParameter("mode");

if(mode==null){

//insert

request.setAttribute("mode", "save");

}else{

//update

CommonDAO dao = CommonDAOImpl.getInstance();

MemberForm f = (MemberForm)form;

MemberForm dto = (MemberForm)dao.getReadData("member.getReadData",f);

if(dto==null){

request.setAttribute("message", "일치하는 회원정보가 없습니다!");

return mapping.findForward("login");

}

request.setAttribute("dto", dto);

request.setAttribute("mode", "updateok");

}

return mapping.findForward("created");

}

//회원데이터 입력완료시

public ActionForward created_ok(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

CommonDAO dao = CommonDAOImpl.getInstance();

MemberForm f = (MemberForm)form; //페이지에서 MemberForm 데이터 넘어오는걸 downcast하여 f에 할당

String mode = request.getParameter("mode");//save, updateok

if(mode.equals("save")){

//입력

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

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

dao=null;

}else{

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

request.setAttribute("mode", "save");

return mapping.findForward("created");

}

}else{

//수정

dao.updateData("member.updateData", f);

}

return mapping.findForward("created_ok");

}

//로그인 페이지 포워드

public ActionForward login(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

return mapping.findForward("login");

}

//로그인정보 입력시

public ActionForward login_ok(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

CommonDAO dao = CommonDAOImpl.getInstance();

MemberForm f = (MemberForm)form;

f = (MemberForm)dao.getReadData("member.getReadData",f);

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

if(f==null){

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

return mapping.findForward("login");

}

//세션 올리기

HttpSession session = request.getSession();

session.setAttribute("MemberForm", f);

return mapping.findForward("login_ok");

}

//비밀번호찾기 페이지 포워드

public ActionForward searchpw(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

return mapping.findForward("searchpw");

}

//비밀번호 찾기

public ActionForward searchpw_ok(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

CommonDAO dao = CommonDAOImpl.getInstance();

MemberForm f = (MemberForm)form;

MemberForm dto = (MemberForm)dao.getReadData("member.getPwd",f);

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

if(dto==null){

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

return mapping.findForward("searchpw");

}else{

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

request.setAttribute("message", str);

return mapping.findForward("searchpw");

}

}

}


회원가입

DATABASE 반영조회


회원정보 수정

DATABASE 반영조회


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 %>/join.do";

f.submit();

}

</script>

</head>

<body>

<div id="bbs">

<div id="bbs_title">

<c:if test="${mode=='save' }">

회원가입

</c:if>

<c:if test="${mode=='updateok' }">

회원정보수정

</c:if>

</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="method" value="created_ok">

<input type="hidden" name="mode" value="${mode }">

<c:if test="${mode=='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 %>/join.do?method=login';"/>

</c:if>

<c:if test="${mode=='updateok' }">

<input type="button" value="수정하기" class="btn2" onclick="sendIt();"/>

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

onclick="javascript:location.href='<%=cp%>/join.do?method=login';"/>

</c:if>

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

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

</div>

</div>

</form>

</div>

</body>

</html>


8. 로그인 페이지(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 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 %>/join.do?method=searchpw_ok";

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="button" value="확인" class="btn2" onclick="sendIt();">

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

onclick="javascript:location.href='<%=cp %>/join.do?method=login';">

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

onclick="javascript:location.href='<%=cp %>/join.do?method=created';">

</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 %>/join.do?method=searchpw">비밀번호 찾기</a>

</td></tr>

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

</c:when>

</c:choose>

</table>

</form>

</body>

</html>

비밀번호 찾기 페이지

9. 비밀번호 찾기 (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 %>/join.do?method=searchpw_ok";

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="button" value="확인" class="btn2" onclick="sendIt();">

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

onclick="javascript:location.href='<%=cp %>/join.do?method=login';">

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

onclick="javascript:location.href='<%=cp %>/join.do?method=created';">

</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 %>/join.do?method=searchpw">비밀번호 찾기</a>

</td></tr>

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

</c:when>

</c:choose>

</table>

</form>

</body>

</html>