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  回復  更多評論   

          主站蜘蛛池模板: 湘乡市| 清河县| 芮城县| 滨州市| 兰坪| 临汾市| 伊春市| 监利县| 青州市| 绵阳市| 罗江县| 什邡市| 广丰县| 建湖县| 尉氏县| 元氏县| 广州市| 桃园县| 连州市| 昌平区| 潍坊市| 盐池县| 高邮市| 鹿邑县| 嘉义县| 叙永县| 林甸县| 临沂市| 康定县| 皮山县| 天台县| 怀远县| 东阳市| 宁津县| 乐陵市| 宁德市| 沁源县| 葵青区| 临高县| 望奎县| 抚顺市|