今天突发奇想的实现一个小小的cache。把分类categories放入map中,cache起来。
private
void
cache() {
if (log.isDebugEnabled()){
log.debug( " Starting cache the categories " );
}
cacheCategoryMap = new HashMap();
cacheCategoryMap.put( " categories " ,categoryDao.getCategories());
}
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执行完毕之后就把stack中的这个值清空了我又用了。AroundInterceptor 我想在after里面去设置不就可以了。
stack.setValue( " categories " ,categoryManager.getCategories());
return invocation.invoke();
protected
void
after(ActionInvocation dispatcher, String result)
throws
Exception {
final OgnlValueStack stack = ActionContext.getContext().getValueStack();
stack.setValue( " categories " ,categoryManager.getCategories());
}
可是这样还是不可以。我晕了。我想是不是要在action里面声明一下categories。
final OgnlValueStack stack = ActionContext.getContext().getValueStack();
stack.setValue( " categories " ,categoryManager.getCategories());
}
private
List categories;
public List getCategories() {
return categories;
}
public void setCategories(List categories) {
this .categories = categories;
}
然后在before里面去get就可以了。
public List getCategories() {
return categories;
}
public void setCategories(List categories) {
this .categories = categories;
}
protected
void
before(ActionInvocation invocation)
throws
Exception {
final OgnlValueStack stack = ActionContext.getContext().getValueStack();
stack.setValue( " categories " ,categoryManager.getCategories());
}
final OgnlValueStack stack = ActionContext.getContext().getValueStack();
stack.setValue( " categories " ,categoryManager.getCategories());
}
总算实现了。不过还要在每个action里面声明一下categories,这样还是很不好的。刚才有人建议用filter。我在试试吧.
http://leaf.jdk.cn/index.php/archives/91