Dev/Struts

Struts1/iBatis - 로그아웃 기능 구현

창문닦이 2019. 3. 24. 13:29

 1. 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="logout_ok" redirect="true" path="/boardTest.do?method=list"/>

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

</action>

</action-mappings>

</struts-config>         

 2. Action 클래스 logout메소드 추가

//로그인 페이지 포워드

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);

//dto==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 logout(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

//세션에 회원정보 지우기

HttpSession session = request.getSession();

session.removeAttribute("MemberForm");

session.invalidate();

return mapping.findForward("logout_ok");

}

3. 게시판 리스트 JSP 수정 

세션에 올라온 MemberForm이 있을 경우 인사메세지 출력

로그아웃 버튼 누를 경우 logout메소드 호출

<%@ 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%>/boardTest/css/style.css" type="text/css"/>

<link rel="stylesheet" href="<%=cp%>/boardTest/css/list.css" type="text/css"/>

<script type="text/javascript">

function searchData() {

var f= document.searchForm;

f.action = "<%=cp%>/boardTest.do?method=list";

f.submit();

}

</script>

</head>

<body>

<div id="bbsList">

<div id="bbsList_title">

게 시 판(Struts1 + iBatis)

</div>

<div id="bbsList_header">

<div>

<c:if test="${!empty sessionScope.MemberForm.userName }">

${sessionScope.MemberForm.userName }님 반갑습니다 !

</c:if>

</div>

<div id="leftHeader">

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

<select name="searchKey" class="selectField">

<option value="subject">제목</option>

<option value="name">이름</option>

<option value="content">내용</option>

</select>

<input type="text" name="searchValue" class="textField"/>

<input type="button" value=" 검 색 " class="btn2" onclick="searchData();"/>

</form>

</div>

<div id="rightHeader">

<c:if test="${!empty sessionScope.MemberForm.userName }">

<input type="button" value="로그아웃" class="btn2"

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

</c:if>

<c:if test="${empty sessionScope.MemberForm.userName }">

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

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

</c:if>

<input type="button" value=" 글올리기 " class="btn2"

onclick="javascript:location.href='<%=cp%>/boardTest.do?method=created${params}';"/>

</div>

</div>

<div id="bbsList_list">

<div id="title">

<dl>

<dt class="num">번호</dt>

<dt class="subject">제목</dt>

<dt class="name">작성자</dt>

<dt class="created">작성일<dt>

<dt class="hitCount">조회수</dt>

</dl>

</div>

<div id="lists">

<c:forEach var="dto" items="${lists }">

<dl>

<dd class="num">${dto.num}</dd>

<dd class="subject">

<a href="${urlArticle}&num=${dto.num}">${dto.subject}</a>

</dd>

<dd class="name">${dto.name}</dd>

<dd class="created">${dto.created}</dd>

<dd class="hitCount">${dto.hitCount}</dd>

</dl>

</c:forEach>

</div>

<div id="footer">

<p>

<c:if test="${totalDataCount!=0}">

${pageIndexList}

</c:if>

<c:if test="${totalDataCount==0}">

등록된 게시물이 없습니다.

</c:if>

</p>

</div>

</div>

</div>

</body>

</html>


로그인 페이지

로그인 완료 후 포워딩 페이지(게시판 리스트)

로그아웃 완료 후 포워딩 페이지(게시판 리스트)