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 何云隆 閱讀(3361) 評論(1)  編輯  收藏 所屬分類: Struts2

          評論

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

          6  回復  更多評論   

          主站蜘蛛池模板: 临沧市| 仲巴县| 崇左市| 百色市| 海南省| 龙岩市| 元朗区| 东乡县| 江源县| 乌兰浩特市| 吉林省| 东兰县| 读书| 天气| 平谷区| 江川县| 全州县| 永春县| 腾冲县| 高唐县| 莆田市| 平遥县| 加查县| 宝应县| 乌海市| 黔江区| 道真| 临颍县| 屏东市| 新蔡县| 中西区| 洪泽县| 安宁市| 花莲县| 旌德县| 长汀县| 闽清县| 六安市| 宁安市| 江西省| 五河县|