Struts2攔截器,簡單的用戶登錄攔截及靜態緩存的使用

1.struts-interceptor.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>
<constant name="struts.action.extension" value="do"/>
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="error.interceptor" extends="struts-default">
<interceptors>

<!--自定義異常的攔截器-->
<interceptor name="exceptionfiter" class="com.taobao.framework.interceptor.ExceptionInterceptorWebwork" />
<!--自定義用戶的攔截器-->
<interceptor name="authority" class="com.taobao.framework.interceptor.AuthorityInterceptor" />
<!--配置內建默認攔截器-->
<interceptor-stack name="defaultStack">
<interceptor-ref name="model-driven" />
<interceptor-ref name="params" />
<!--配置自定義異常的攔截器-->
<interceptor-ref name="exceptionfiter" />
<!--配置自定義的攔截器 --> 
<interceptor-ref name="authority" />    
</interceptor-stack>
</interceptors>
 
<!-- 全局跳轉 -->
<global-results>
<result name="error" type="dispatcher">
/page/main/error.jsp
</result>
<result name="fail" >
/page/main/fail.jsp
</result>
<result name="main" type="dispatcher">
/page/main/main.jsp
</result>
<result name="login" type="dispatcher">
/page/main/login.jsp
</result>
</global-results>

<global-exception-mappings>
<exception-mapping result="error" exception="com.taobao.framework.exception.AppException"></exception-mapping>
</global-exception-mappings>


</package>

<!-- 設置默認的package -->
<package name="default" extends="error.interceptor"></package>

<constant name= "struts.multipart.maxSize" value="15242880" />
</struts>

2.AuthorityInterceptor類

package com.taobao.framework.interceptor;


import java.util.Map;


import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.InitializingBean;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.taobao.api.delivery.DeliveryService;
import com.taobao.api.item.ItemService;
import com.taobao.business.usermanager.paratemplate.ParaTemplateService;
import com.taobao.business.usermanager.setup.SetUpService;
import com.taobao.framework.common.Constants;
import com.taobao.framework.util.Debug;
import com.taobao.system.cathe.CityCache;
import com.taobao.system.cathe.UsersCache;
import com.taobao.system.manager.dto.ManagerDTO;


/**
 * Project: spmanage
 * 
 * Remark: 驗證用戶是否登錄
 * 
 * Author: tanghong
 * 
 * Create Date: 2010-4-17
 */
public class AuthorityInterceptor implements Interceptor, InitializingBean {

private static final long serialVersionUID = 2590058205697988476L;

private  ItemService itemService;

private  SetUpService setUpService;

private  ParaTemplateService paraTemplateService;

private  DeliveryService deliveryService;

public void afterPropertiesSet() throws Exception {

}

public void destroy() {

}

//系統初始化加載
public void init() {
//城市緩存cache
CityCache cityCache = new CityCache();
cityCache.setDeliveryService(deliveryService);
cityCache.inne();

UsersCache usersCache = new UsersCache();
usersCache.setSetUpService(setUpService);
usersCache.setParaTemplateService(paraTemplateService);
usersCache.inne();
}

@SuppressWarnings("unchecked")
public String intercept(ActionInvocation invocation) throws Exception {

//檢查Session中是否存在manager
        Map session = invocation.getInvocationContext().getSession();
//        獲取session中的所有的屬性
        ManagerDTO managerDTO = (ManagerDTO)session.get("manager");
        if (managerDTO!=null) {
        if(managerDTO!=null){
        String s= invocation.getInvocationContext().getName();
    if(s!=null&&s.substring(0,2).equals("do")){
    Debug.logDebug("日誌信息 IP:"+ServletActionContext.getRequest().getRemoteAddr()
    +", 管理員:"+managerDTO.getName()+", 類名:"+invocation.getAction().toString()+", 操作:"+s,"攔截器");
    }
        }
        //存在的情況下進行後續操作。
        return invocation.invoke();
        } else {
        //否則終止後續操作,返回LOGIN
   return Constants.PAGE_LOGIN;
        }
        
        
        //檢查session中是否存在sessionKey,buyNick,sellNick
//        String sessionKey = session.get("sessionKey").toString();
//        String buyNick = session.get("buyNick").toString();
//        String sellNick = session.get("sellNick").toString();
//        if(sessionKey!="" && buyNick!="" && sellNick!=""){
//         return invocation.invoke();
//        }else{
//         return Constants.PAGE_LOGIN;
//        }
       
    }

public ItemService getItemService() {
return itemService;
}

public void setItemService(ItemService itemService) {
this.itemService = itemService;
}


public SetUpService getSetUpService() {
return setUpService;
}


public void setSetUpService(SetUpService setUpService) {
this.setUpService = setUpService;
}


public ParaTemplateService getParaTemplateService() {
return paraTemplateService;
}


public void setParaTemplateService(ParaTemplateService paraTemplateService) {
this.paraTemplateService = paraTemplateService;
}

public DeliveryService getDeliveryService() {
return deliveryService;
}

public void setDeliveryService(DeliveryService deliveryService) {
this.deliveryService = deliveryService;
}

}


3.CityCache 類

package com.taobao.system.cathe;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.taobao.api.delivery.DeliveryService;
import com.taobao.api.delivery.dto.AreaDTO;


public class CityCache {
private static DeliveryService deliveryService;

public static Map<Integer,int[][]> provinceMap = new HashMap<Integer,int[][]>(); //省份id——城市id,狀態

public static Map<Integer,String> cityMap= new HashMap<Integer,String>();  //id——name

public static void inne(){
List<AreaDTO> areaList = deliveryService.findAreaByType(2, 3);
List<Integer> idList= new ArrayList<Integer>();
for (AreaDTO areaDTO : areaList) {
cityMap.put(areaDTO.getId(), areaDTO.getName());
if(areaDTO.getType()==2){
idList.add(areaDTO.getId());
}
}
for(int i=0;i<idList.size();i++){
List<AreaDTO> alist=deliveryService.findAreaByHql(idList.get(i), 3);
int parentIds[][]=new int[alist.size()][2];
for(int j=0;j<alist.size();j++) {
AreaDTO ad=alist.get(j);
parentIds[j][0]=ad.getId();
parentIds[j][1]=0;
}

provinceMap.put(idList.get(i), parentIds);
}

}

public static DeliveryService getDeliveryService() {
return deliveryService;
}

public static void setDeliveryService(DeliveryService deliveryService) {
CityCache.deliveryService = deliveryService;
}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章