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;
}
}