interceptor攔截器

最近做一個 電商前段網站,操作訂單相關操作的時候,需要自定義攔截器:

攔截器代碼如下:

package com.eshore.emall.web.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
   
import com.eshore.emall.pub.base.BaseInterceptor;
import com.eshore.emall.pub.constant.EmallConstants;
import com.eshore.emall.web.model.UserInfoBean;
import com.opensymphony.xwork2.ActionInvocation;

public class LoginInterceptor extends BaseInterceptor  {
    
    @SuppressWarnings("rawtypes")
    public String intercept(ActionInvocation invocation) throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        if (session == null) { //session已經過期
            return getReturn("login");
        }

        UserInfoBean userInfoBean = (UserInfoBean)session.getAttribute("userInfo");
        
        if(userInfoBean == null || !EmallConstants.USER_TYPE_REG.equals(userInfoBean.getUserType())){            
            return getReturn("login");
        }
           
        return invocation.invoke();
        
    }
    
    private String getReturn(String defaultReturn) {
        String sessionOutMessage = "對不起,您還沒有登錄或登陸超時,請重新登陸!";
        
        // ajax請求處理
        if (isAjaxRequest()) {
            //原始的方法
            //setErrorTipMessage(TIMEOUT_TYPE, encode(sessionOutMessage));
            //return "none";
            //下面是修改後的方法
            PrintWriter printWriter = null;
            HttpServletResponse response = ServletActionContext.getResponse();
            try {
                printWriter = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }  
            response.setCharacterEncoding("text/html;charset=utf-8");
            response.setContentType("text/html;charset=utf-8");
            printWriter.print("NO_LOGIN");  
            printWriter.flush();  
            printWriter.close();
            return null;
        } else {

//如果不是ajax請求
            return defaultReturn;
        }
    }

}


isAjaxRequest方法:

protected boolean isAjaxRequest() {
        HttpServletRequest request = ServletActionContext.getRequest();
        return "XMLHttpRequest".equalsIgnoreCase(request
                .getHeader("x-requested-with"));
    }


這樣攔截器部分就完成了,還有一點需要處理:如果你的請求時ajax請求:

實現代碼如下:

$(".btn-shop").click(function(){
            var storeId = $(this).attr("storeId")
            var catentryId = $(this).attr("catentryId");
            $.post(contextPath + "order/addToShoppingCart.action?purchaseCatEntryList[0].storeId="+storeId+"&purchaseCatEntryList[0].catEntryId="+catentryId+"&purchaseCatEntryList[0].quantity="+1 ,function(result){
                if(result == "NO_LOGIN"){    //爛機器返回的ajax信息
                    alert("對不起,您還沒有登錄或登陸超時,請重新登陸!");
                }else{
                    var results = result.split("@@poson@@");
                    if(results[0] == 'true'){
                        //alert(results[1]);
                        location.href=contextPath + "order/findShoppingCart.action";
                    } else {
                        alert(results[1]);
                    }
                }
            });
        });   
如果不是ajax請求,而是全局請求,則以上這不操作可以不要,直接設置action即可。

發佈了24 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章