struts2的demo中showcase的crud示例
百度广告
开始再看这个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
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
编辑推荐:
下载Word文档
温馨提示:因考试政策、内容不断变化与调整,长理培训网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准! (责任编辑:长理培训)
-
国家电网30270试题
-
湖南中烟7463试题
-
湖南统招专升本不连网,流畅做题
-
长沙理工大学考研培训4532试题
13年品牌值得信赖
已有 22658 名学员学习以下课程通过考试
网友评论(共0条评论)
精品课程
更多
10781人学习
相关推荐
图书更多+
- 电网书籍
- 财会书籍
- 其它工学书籍
拼团课程更多+
- 电气拼团课程
- 财会拼团课程
- 其它工学拼团
热门排行
-
- 长理培训微信公众号
- 每日推送精彩考试资讯
长按二维码识别
微信搜索“ 长理培训”
-
- 加入QQ群一起来考国网!
- QQ群号:223940140
点击进入
长理培训客户端 资讯,试题,视频一手掌握
去 App Store 免费下载 iOS 客户端
点击加载更多评论>>