我在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;
}