hyljava

          Struts2登錄攔截器

          UserLoginInterceptor 攔截器類(千萬不要寫成abstract類,開始我就寫成這樣了就是加載不上去)

          package com.hyl.inter;

          import java.util.Map;
          import javax.servlet.http.HttpServletRequest;
          import org.apache.struts2.ServletActionContext;
          import com.hyl.action.UserInfoAction;
          import com.opensymphony.xwork2.ActionInvocation;
          import com.opensymphony.xwork2.interceptor.Interceptor;

          public class UserLoginInterceptor implements Interceptor {
           public void destroy() {
            System.out.println("我是登錄攔截器銷毀");
           }

           public void init() {
            System.out.println("我是登錄驗證攔截器初始化");
           }

           public String intercept(ActionInvocation invocation) throws Exception {
            System.out.println("我正在進行登錄攔截功能");
            String result = "";
            // 判斷當前要調用的Action實例(對象) 是否是 登錄驗證的Action
            if (invocation.getAction() instanceof UserInfoAction) {
             
             result = invocation.invoke();
            } else {
             //當前要調用的不是登錄的Action
             //從session中取值,判斷是否是空(登錄)
             //登錄的Action中將登錄用戶信息存儲到session中,key值是當前的sessionID
             HttpServletRequest request = ServletActionContext.getRequest();
             Map session = invocation.getInvocationContext().getSession();
             if (session.get(request.getSession().getId()) != null) {
              result = invocation.invoke();
             } else {
              //session中如果不存在就跳到登錄頁面
              return "error";
             }
            }
            return result;
           }
          }

          Struts配置文件內容

          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "<struts>
           <package name="userInfo" namespace="/userInfo"  extends="struts-default">
            <interceptors>
             <interceptor name="userInter" class="com.hyl.inter.UserLoginInterceptor"></interceptor>
             <interceptor-stack name="myStack">
          <!--加載Struts默認攔截器-->
             <interceptor-ref name="defaultStack"></interceptor-ref>
             <interceptor-ref name="userInter"></interceptor-ref>
             </interceptor-stack>
            </interceptors>
            
            <default-interceptor-ref name="myStack"></default-interceptor-ref>

            <action name="*userInfoMan" class="com.hyl.action.UserInfoAction" method="{1}">
             <result name="success1" type="redirect">/menu.jsp</result>
             <result name="error" type="redirect">/user_login.jsp</result>
            </action>

           </package>

          </struts>   


           

          posted on 2012-08-10 18:58 何云隆 閱讀(3360) 評論(1)  編輯  收藏 所屬分類: Struts2

          評論

          # re: Struts2登錄攔截器 2012-10-15 11:13 6

          6  回復  更多評論   

          主站蜘蛛池模板: 双辽市| 唐山市| 黄冈市| 县级市| 桃源县| 永川市| 尼木县| 永昌县| 甘洛县| 平果县| 句容市| 镇赉县| 西盟| 宜黄县| 崇明县| 蓝田县| 安陆市| 沙河市| 长顺县| 二连浩特市| 泊头市| 偃师市| 子洲县| 同江市| 曲阜市| 吴忠市| 无为县| 柘城县| 越西县| 乳源| 洞头县| 武冈市| 紫金县| 云南省| 越西县| 沂水县| 金坛市| 新乐市| 水富县| 平和县| 行唐县|