只是自己的想法,不對不要扔雞蛋哦。
今天突發(fā)奇想的實現(xiàn)一個小小的cache。把分類categories放入map中,cache起來。
????private?void?cache()?{
????????if(log.isDebugEnabled()){
????????????log.debug("Starting?cache?the?categories
");
????????}
????????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執(zhí)行完畢之后就把stack中的這個值清空了我又用了。AroundInterceptor 我想在after里面去設(shè)置不就可以了。
????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());
????}
總算實現(xiàn)了。不過還要在每個action里面聲明一下categories,這樣還是很不好的。剛才有人建議用filter。我在試試吧.
http://leaf.jdk.cn/index.php/archives/91
posted on 2006-03-21 11:37
莫多 閱讀(1128)
評論(0) 編輯 收藏 所屬分類:
Webwork