我在ehcache.xml中的配置 <cache name="sampleCache" maxElementsInMemory="5"
maxElementsOnDisk="100" eternal="false" timeToIdleSeconds="2"
timeToLiveSeconds="2" overflowToDisk="true">
</cache>
我在ACTION中的代码 public ActionForward test(ActionMapping maping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
System.out.println("=======================");
UserDao ud = new UserDaoImple();
List<User> lu=ud.test();
if(!testCache()){
cache.put(new Element("TX",lu)); //这里报空指针异常
cacheManager.addCache(cache);
}
System.out.println(lu.size());
return maping.findForward("success");
} CacheManager cacheManager;
Cache cache;
public boolean testCache() {
boolean flag = false;
URL url = getClass().getResource("/ehcache.xml");
cacheManager = new CacheManager();
cache = cacheManager.getCache("sampleCache");
System.out.println(cache);
if(null==cache)
return false;
System.out.println(cache);
Element elt = cache.get("TX");
if (null != elt) {
flag=true;
}
return flag;
}
maxElementsOnDisk="100" eternal="false" timeToIdleSeconds="2"
timeToLiveSeconds="2" overflowToDisk="true">
</cache>
我在ACTION中的代码 public ActionForward test(ActionMapping maping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
System.out.println("=======================");
UserDao ud = new UserDaoImple();
List<User> lu=ud.test();
if(!testCache()){
cache.put(new Element("TX",lu)); //这里报空指针异常
cacheManager.addCache(cache);
}
System.out.println(lu.size());
return maping.findForward("success");
} CacheManager cacheManager;
Cache cache;
public boolean testCache() {
boolean flag = false;
URL url = getClass().getResource("/ehcache.xml");
cacheManager = new CacheManager();
cache = cacheManager.getCache("sampleCache");
System.out.println(cache);
if(null==cache)
return false;
System.out.println(cache);
Element elt = cache.get("TX");
if (null != elt) {
flag=true;
}
return flag;
}
解决方案 »
- 各位看过thinking in java 的过来
- JMS的Topic或者Queue满了怎么办
- 请教高手:关于hibernate中父子关系的删除问题!
- XFire Webservices 异常
- java.lang.reflect.InvocationTargetException请高手指教
- java 调用.net webservice的问题,请大家帮忙看看!
- 请问如何在Eclipse中新建一个servlet?
- 求救,CSDN的朋友们!(帮帮出出注意!)
- jbuilder布局问题?
- maven项目install到本地仓库后,再次修改项目如何直接更新本地仓库里的文件
- 求jsp大牛帮我解答一下
- 高手进,String 转换 byte[]?
if (!testCache()) {
cache.put(new Element("TX",lu));
}
等价于
if (null==cache()) {
cache.put(new Element("TX",lu));
}
cache = cacheManager.getCache("sampleCache");
是下面这个.我上面的写错了
cacheManager = new CacheManager(url);