百度广告
开始再看这个crud示例时,居然发现我没有准备数据库信息及数据也可以运行成功,后来看了实现才明白,该示例使用了Map模拟数据库存储操作数据,不过struts2的实现还是又可以观飨的地方,下面就看看它的实现。
public interface Storage extends Serializable ...{
} 很明了,定义了操作数据的几种的方法,然后实现了一种使用map存储数据的方式(当然你可以使用自己存储数据的方式实现,比如xml等等)代码 public class MemoryStorage implements Storage ...{
private static final long serialVersionUID = 8611213748834904125L;
if (entityClass != null) ...{
if (tryMap == null) ...{
tryMap = new HashMap();
}
return tryMap;
return null;
}
private IdEntity intStore( Class entityClass, IdEntity object ) ...{
return object;
if (entityClass != null && id != null) ...{
} else ...{
}
if (object == null) ...{
}
throw new CreateException("Cannot store object with null id");
if (get(object.getClass(), object.getId()) != null) ...{
}
}
public IdEntity update ( IdEntity object ) throws UpdateException ...{
throw new UpdateException("Cannot update null object.");
if ( get(object.getClass(), object.getId())==null ) ...{
}
}
public Serializable merge ( IdEntity object ) throws StorageException ...{
throw new StorageException("Cannot merge null object");
if (object.getId() == null || get(object.getClass(), object.getId())==null) ...{
} else ...{
}
try ...{
getEntityMap(entityClass).remove(id);
} else ...{
}
throw new CreateException(e);
}
public int delete( IdEntity object ) throws CreateException ...{
throw new CreateException("Cannot delete null object");
return delete(object.getClass(), object.getId());
if (entityClass != null) ...{
} else ...{
}
this.memory = new HashMap();
|||
这样一种不使用数据库存储数据的方式就已经算完成了,不过我们需要初始几个数据怎么实现呢?struts2使用了spring的 InitializingBean 接口[Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。]代码
public class TestDataProvider implements Serializable, InitializingBean ...{
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(TestDataProvider.class);
public static final String POSITIONS = ...{
"System Architect",
"CEO"
"Junior",
"Master"
new Skill("WW-SEN", "Struts Senior Developer"),
new Skill("SPRING-DEV", "Spring Developer")
new Employee(new Long(1), "Alan", "Smithee", new Date(), new Float(2000f), true, POSITIONS[0],
new Employee(new Long(2), "Robert", "Robson", new Date(), new Float(10000f), false, POSITIONS[1],
};
private SkillDao skillDao;
this.skillDao = skillDao;
this.employeeDao = employeeDao;
try ...{
for (int i = 0, j = TEST_SKILLS.length; i
点击加载更多评论>>