interceptor和ognlvaluestack

本文介绍了一种在Struts框架中通过拦截器将分类信息缓存到OgnlValueStack的方法,解决了每次请求都需要重新获取数据的问题,并探讨了不同实现方式的优劣。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只是自己的想法,不对不要扔鸡蛋哦。

今天突发奇想的实现一个小小的cache。把分类categories放入map中,cache起来。
     private   void  cache() {
        
if (log.isDebugEnabled()){
            log.debug(
" Starting cache the categoriesdot.gif " );
        }
        cacheCategoryMap 
=   new  HashMap();
        cacheCategoryMap.put(
" categories " ,categoryDao.getCategories());
    }

然后我想在interceptor里面把categories写到ognlvaluestack里面这样我在ftl里面就可以<#list categories>....</#list>了。因为这个是在每个页面的header.ftl里面的。我也就不需要再每个action里面去get一下了。
刚开始我implements Interceptor

         final  OgnlValueStack stack  =  ActionContext.getContext().getValueStack();
        stack.setValue(
" categories "  ,categoryManager.getCategories());
        
return  invocation.invoke();
可是这样也不可以。后来我想到是不是action执行完毕之后就把stack中的这个值清空了我又用了。AroundInterceptor 我想在after里面去设置不就可以了。
     protected   void  after(ActionInvocation dispatcher, String result)  throws  Exception {
        
final  OgnlValueStack stack  =  ActionContext.getContext().getValueStack();
        stack.setValue(
" categories "  ,categoryManager.getCategories());
    }
可是这样还是不可以。我晕了。我想是不是要在action里面声明一下categories。
     private  List categories;

    
public  List getCategories() {
        
return  categories;
    }


    
public   void  setCategories(List categories) {
        
this .categories  =  categories;
    }

然后在before里面去get就可以了。
     protected   void  before(ActionInvocation invocation)  throws  Exception {
        
final  OgnlValueStack stack  =  ActionContext.getContext().getValueStack();
        stack.setValue(
" categories "  ,categoryManager.getCategories());
    }

总算实现了。不过还要在每个action里面声明一下categories,这样还是很不好的。刚才有人建议用filter。我在试试吧.


http://leaf.jdk.cn/index.php/archives/91
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值