今天突發奇想的實現一個小小的cache。把分類categories放入map中,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
stack.setValue("categories" ,categoryManager.getCategories());
return invocation.invoke();
final OgnlValueStack stack = ActionContext.getContext().getValueStack();
stack.setValue("categories" ,categoryManager.getCategories());
}
public List getCategories() {
return categories;
}
public void setCategories(List categories) {
this.categories = categories;
}
final OgnlValueStack stack = ActionContext.getContext().getValueStack();
stack.setValue("categories" ,categoryManager.getCategories());
}
總算實現了。不過還要在每個action裏面聲明一下categories,這樣還是很不好的。剛纔有人建議用filter。我在試試吧.
http://leaf.jdk.cn/index.php/archives/91