카테고리 없음

Struts 2 Custom Interceptor

Soul-Learner 2014. 4. 6. 22:28

Struts 2 Custom Interceptor example



AuthInterceptor.java

package emp;


import java.util.Map;


import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;


public class AuthInterceptor implements Interceptor {


@Override

public void destroy() {

// TODO Auto-generated method stub

}


@Override

public void init() {

// TODO Auto-generated method stub

}


@Override

public String intercept(ActionInvocation actionInvocation) throws Exception {


               System.out.println("커스텀 인터셉터 실행됨");

               Map<String, Object> sessionMap = actionInvocation.getInvocationContext().getSession();

         

               if(sessionMap.get("logon")==null) {

           System.out.println("로그인 안된 상태");

                   return Action.LOGIN;

               }else{

           System.out.println("로그인 확인됨");

           /*

                   Action action = (Action) actionInvocation.getAction();

            

                   if(action instanceof UserAware){

                       ((UserAware) action).setUser(user);

                   }*/

                   return actionInvocation.invoke();

              }

       }

}




struts.xml

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

<!DOCTYPE struts PUBLIC

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

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


<struts>


    <constant name="struts.enable.DynamicMethodInvocation" value="false" />

    <constant name="struts.devMode" value="true" />


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

    

<interceptors>

            <interceptor name="auth" class="emp.AuthInterceptor"/> <!-- 인터셉터 등록-->

            <interceptor-stack name="authStack">                                <!-- 인터셉터 스택 등록-->

                <interceptor-ref name="auth"/>                                     <!-- 인터셉터 스택에 특정 인터셉터 포함-->

                <interceptor-ref name="defaultStack"></interceptor-ref>    <!-- 인터셉터 스택도 다른 인터셉터 스택에 포함될 수 있다-->

            </interceptor-stack>

        </interceptors>

 

<!--  <default-interceptor-ref name="authStack"></default-interceptor-ref> --> 모든 액션에 자동으로 적용되는 인터셉터 스택 선언


        <action name="empList" class="emp.EmpAction" method="empList">

          <interceptor-ref name="auth"/>                                             <!-- 등록된 인터셉터를 액션에서 사용-->

        <result>emp/empList.jsp</result>

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

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

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

                        <param name="resultMsg">loginRequired</param>

                </result>

        </action>


    </package>


</struts>


위에서 액션을 설정하는 부분에서 <param name="resultMsg">loginRequired</param> 요소는 Login 액션의 속성인 resultMsg 에 문자열을 전달하는 기능이므로 Login 액션에 resultMsg 속성이 선언되어 있어야 하며 또 setResultMsg() 메소드도 선언되어 있어야 한다.

그래야만 Login 후에 화면에 출력되는 loginForm.jsp 에서 <s:property value='resultMsg'/> 등을 사용할 수 있다

결국,  <param name="resultMsg">loginRequired</param> 은 요청시 다음과 같은 URL으로 표현되어 액션으로 전달된다

http://localhost:8080/shop/login.action?resultMsg=loginRequired