使用自定义拦截器加载字典表

使用自定义拦截器可以直接实现接口com.opensymphony.xwork2.interceptor.Interceptor,也可以继承com.opensymphony.xwork2.interceptor.AbstractInterceptor。
1 定义Interceptor

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import com.config.Constants;
import com.dal.dao.DictDataDao;
import com.vo.DicData;
import com.vo.DicType;

import java.util.*;
/**
* @author CHH
*/
public class LoadDictInterceptor extends AbstractInterceptor{
private DictDataDao dictDataDao;
/* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
@Override
public String intercept(ActionInvocation ai) throws Exception {
Map session = ai.getInvocationContext().getSession();

if(session.get(Constants.AREA_LIST)==null){
List<DicData> areas=dictDataDao.findByType(DicType.TYPE_AREA);
session.put(Constants.AREA_LIST, areas);
}

if(session.get(Constants.ORGTYPE_LIST)==null){
List<DicData> areas=dictDataDao.findByType(DicType.TYPE_ORGANIZATION_TYPE);
session.put(Constants.ORGTYPE_LIST, areas);
}

return ai.invoke();
}
/**
* @return the dictDataDao
*/
public DictDataDao getDictDataDao() {
return dictDataDao;
}
/**
* @param dictDataDao the dictDataDao to set
*/
public void setDictDataDao(DictDataDao dictDataDao) {
this.dictDataDao = dictDataDao;
}


}

2 配置Interceptor
Struts.xml

<package name="pmanage" extends="struts-default">
<!-- 定义Interceptor -->
<interceptors>
<!-- 定义加载字典拦截器,interceptor在spring中配置 -->
<interceptor name="dictData" class="initDictInterceptor" />
<!-- 定义一个包含加载字典的拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="dictData" />
</interceptor-stack>
</interceptors>
<!-- 定义默认拦截器 -->
<default-interceptor-ref name="mydefault" />
...................
</package>

初次调用时候,字典被加载。[img]http://img.tongji.cn.yahoo.com/1083199/ystat.gif[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值