Struts2의 action에서는 servlet을 사용하지 않기 때문에 자체적으로 ActionContex를 이용해서 session을 처리한다. 그 session 자체가 원래는 Map 방식을 정의하고 있다. 그래서 일명 session data를 만들어서 Map방식을 이용하여 session을 저장하도록 지시한다. session에 저장할 때, Map 방식이기 때문에 앞에는 name 값(key), 뒤에는 value가 들어가야 한다. 로그인 시에 user이름으로 user라는 오브젝트를 저장해 놓고 result로 보낸다. result는 적절할 페이지를 Template를 통해 지정하고, Interceptor로 이동한다. Interceptor에서는 Interceptor의 1단계로 이동하고, 이 때 뷰페이지를 선택해서 데이타들을 세션영역에 생성 시켜 놓는다.


순서 : jsp 만들기 -> action class 만들기 -> login.xml 만들기 -> struts.xml 추가

1. action class 만들기

package com.myhome.login.actions;

import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;;

@SuppressWarnings("serial")
public class LoginMultiAction extends ActionSupport{

 private String id;
 private String passwd;
 
 /**
  * index 부분
  */
 public String index() throws Exception{
  if(ActionContext.getContext().getSession().get("id") == null)//id를 뽑아내서 비교할 수 도 있다.
  //if(ActionContext.getContext().getSession() == null)
   return LOGIN;
  else
   return SUCCESS;
 }
 
 /**
  * login 부분
  */

 @SuppressWarnings("unchecked")
 public String login() throws Exception{
  ActionContext context = ActionContext.getContext();//session을 생성하기 위해
  Map<String, String> session = (Map<String, String>)context.getSession();// Map 사용시
  session.put("id", id);
  session.put("passwd", passwd);
  context.setSession(session);
  
return SUCCESS;
 }
 
 /**
  * logout 부분
  */
 @SuppressWarnings("unchecked")
 public String logout() throws Exception{
  ActionContext context = ActionContext.getContext();
  Map<String, String> session = (Map<String, String>)context.getSession();

  if(session.get("id") != null){
   session.remove("id");
   session.remove("passwd");

  }
  context.setSession(session);//다시 session을 적용 시켜서 초기화 시켜야 한다. 
 
 return SUCCESS;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getPasswd() {
  return passwd;
 }

 public void setPasswd(String passwd) {
  this.passwd = passwd;
 }
 
}

2. login.xml 만들기.

<?xml version="1.0" encoding="euc-kr"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 
    <package name="login" namespace="/login" extends="struts-default"><!-- struts-default 는 Interceptor 정의 문서로 이게 없으면 Interceptor를 타지 않는다 -->
     <action name="index"
                class="com.myhome.login.actions.LoginMultiAction"
                 method="index"><!-- index method 실행 -->

      <result name="success" type="dispatcher">
            /WEB-INF/login/logout.jsp
      </result>

      <result name="login" type="dispatcher">
       /WEB-INF/login/login.jsp
      </result>
     </action>
     
     <action name="login"
                 class="com.myhome.login.actions.LoginMultiAction"
                 method="login">(!-- inogin method 실행 -->
      <result name="success" type="chain">index</result>
     </action>
     
     <action name="logout"
                class="com.myhome.login.actions.LoginMultiAction"
                method="logout"><!-- logout method 실행 -->
      <result name="success" type="chain">index</result>
     </action>
      
    </package>
   
</struts>


3. struts.xml 문서에 추가

<struts>
   <!-- include files -->
     <include file="login.xml"/>  
</struts>

* jsp 페이지에서

<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<!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=EUC-KR">
   <title>Logout</title>
  </head>
  <body>
    <center><br /><br />
      <table border=0 cellpadding=1 cellspacing=1 bgcolor="#a0a0a0">
       <tr height=50 bgcolor="#ffffff">
         <td width=400 align="center">
           <b>${id}님 반갑습니다.</b><!-- action class에서의 property 이름과 같아야 한다. 이러면 바로 로그인한 아이디가 표시된다.-->
         </td>  
       </tr>

자세한 내용은 파일 참조. 지금까지 한 작업과 업로드 까지 포함. 이 파일은 학습용입니다!


Struts2에서 session을 이용한 로그인 로그아웃 해보기 끝!

+ Recent posts