struts2的demo中show case的crud示例

开始再看这个crud示例时,居然发现我没有准备数据库信息及数据也可以运行成功,后来看了实现才明白,该示例使用了Map模拟数据库存储操作数据,不过struts2的实现还是又可以观飨的地方,下面就看看它的实现。
首先,定义持久类存储接口Storage:
public interface Storage extends Serializable {
IdEntity get( Class entityClass, Serializable id );
Serializable create ( IdEntity object ) throws CreateException;
IdEntity update ( IdEntity object ) throws UpdateException;
Serializable merge ( IdEntity object ) throws StorageException;
int delete( Class entityClass, Serializable id ) throws CreateException;
int delete( IdEntity object ) throws CreateException;
Collection findAll( Class entityClass );
} 很明了,定义了操作数据的几种的方法,然后实现了一种使用map存储数据的方式(当然你可以使用自己存储数据的方式实现,比如xml等等)代码如下: public class MemoryStorage implements Storage {
private static final long serialVersionUID = 8611213748834904125L;


private Map memory = new HashMap();
private Map getEntityMap ( Class entityClass ) {
if (entityClass != null) {
Map tryMap = (Map) memory.get(entityClass);
if (tryMap == null) {
synchronized(memory) {
tryMap = new HashMap();
memory.put(entityClass, tryMap);
}
}
return tryMap;
} else {
return null;
}
}
private IdEntity intStore( Class entityClass, IdEntity object ) {
getEntityMap(entityClass).put(object.getId(), object);
return object;
}
public IdEntity get( Class entityClass, Serializable id ) {
if (entityClass != null && id != null) {
return (IdEntity) getEntityMap(entityClass).get(id);
} else {
return null;
}
}
public Serializable create ( IdEntity object ) throws CreateException {
if (object == null) {
throw new CreateException("Either given class or object was null");
}
if (object.getId() == null) {
throw new CreateException("Cannot store object with null id");
}
if (get(object.getClass(), object.getId()) != null) {
throw new DuplicateKeyException("Object with this id already exists.");
}
return intStore(object.getClass(), object).getId();
}
public IdEntity update ( IdEntity object ) throws UpdateException {
if (object == null) {
throw new UpdateException("Cannot update null object.");
}
if ( get(object.getClass(), object.getId())==null ) {
throw new UpdateException("Object to update not found.");
}
return intStore(object.getClass(), object);
}
public Serializable merge ( IdEntity object ) throws StorageException {
if (object == null) {
throw new StorageException("Cannot merge null object");
}
if (object.getId() == null || get(object.getClass(), object.getId())==null) {
return create(object);
} else {
return update(object).getId();
}
}
public int delete( Class entityClass, Serializable id ) throws CreateException {
try {
if (get(entityClass, id) != null) {
getEntityMap(entityClass).remove(id);
return 1;
} else {
return 0;
}
} catch (Exception e) {
throw new CreateException(e);
}
}
public int delete( IdEntity object ) throws CreateException {
if (object == null) {
throw new CreateException("Cannot delete null object");
}
return delete(object.getClass(), object.getId());
}
public Collection findAll( Class entityClass ) {
if (entityClass != null) {
return getEntityMap(entityClass).values();
} else {
return new ArrayList();
}
}
public void reset() {
this.memory = new HashMap();
}
}
其实就是map操作数据的几种方式(get,put...)
这样一种不使用数据库存储数据的方式就已经算完成了,不过我们需要初始几个数据怎么实现呢?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 = {
"Developer",
"System Architect",
"Sales Manager",
"CEO"
};
public static final String[] LEVELS = {
"Junior",
"Senior",
"Master"
};
private static final Skill[] TEST_SKILLS = {
new Skill("WW-SEN", "Struts Senior Developer"),
new Skill("WW-JUN", "Struts Junior Developer"),
new Skill("SPRING-DEV", "Spring Developer")
};
public static final Employee[] TEST_EMPLOYEES = {
new Employee(new Long(1), "Alan", "Smithee", new Date(), new Float(2000f), true, POSITIONS[0],
TEST_SKILLS[0], null, "alan", LEVELS[0], "Nice guy"),
new Employee(new Long(2), "Robert", "Robson", new Date(), new Float(10000f), false, POSITIONS[1],
TEST_SKILLS[1], Arrays.asList(TEST_SKILLS).subList(1,TEST_SKILLS.length), "rob", LEVELS[1], "Smart guy")
};
private SkillDao skillDao;
private EmployeeDao employeeDao;
public void setSkillDao(SkillDao skillDao) {
this.skillDao = skillDao;
}
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
protected void addTestSkills() {
try {
for (int i = 0, j = TEST_SKILLS.length; i < j; i++) {
skillDao.merge(TEST_SKILLS);
}
if (log.isInfoEnabled()) {
log.info("TestDataProvider - [addTestSkills]: Added test skill data.");
}
} catch (StorageException e) {
log.error("TestDataProvider - [addTestSkills]: Exception catched: " + e.getMessage());
}
}
protected void addTestEmployees() {
try {
for (int i = 0, j = TEST_EMPLOYEES.length; i < j; i++) {
employeeDao.merge(TEST_EMPLOYEES);
}
if (log.isInfoEnabled()) {
log.info("TestDataProvider - [addTestEmployees]: Added test employee data.");
}
} catch (StorageException e) {
log.error("TestDataProvider - [addTestEmployees]: Exception catched: " + e.getMessage());
}
}
protected void addTestData() {
addTestSkills();
addTestEmployees();
}
public void afterPropertiesSet() throws Exception {
addTestData();
}
} 注意真正实现初始化的是afterPropertiesSet() 方法,当然要起效,还需要设定spring的配置文件,设定该部分配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="storage" class="org.apache.struts2.showcase.application.MemoryStorage" singleton="true"/>
<bean id="testDataProvider" class="org.apache.struts2.showcase.application.TestDataProvider"
singleton="true" lazy-init="false"/>
<bean id="skillDao" class="org.apache.struts2.showcase.dao.SkillDao"/>
<bean id="employeeDao" class="org.apache.struts2.showcase.dao.EmployeeDao"/> 最后在web.xml文件配置spring的监听器就ok了,现在启动web服务器,TestDataProvider 就会为持久类进行相应的数据初始化。
后记:如果大家比较感兴趣,可以使用xml代替map玩玩看

http://www.zoomhoo.com/thread-138-3-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
struts2漏洞exp利用工具, 2018-08-24: 增加S2-057 Struts 2.3 to 2.3.34,Struts 2.5 to 2.5.16 此漏洞影响范围非常小,要求配置条件比较苛刻,同时,一些特定版本没有看到有沙盒绕过,说以,目前exp只是基于S2-045改写的,所以exp并不是所有版本都能用,正常情况下Struts 2.3.5-2.3.31,Struts 2.5-2.5.10版本可以使用此exp。 2017-07-07: 增加S2-048 Struts 2.3.X 支持检查官方示例struts2-showcase应用的代码执行漏洞,参考地址:http://127.0.0.1:8080/struts2-showcase/integration/saveGangster.action 2017-03-21: 增加S2-046,官方发布S2-046和S2-045漏洞引发原因一样,只是利用漏洞的位置发生了变化,S2-046方式可能绕过部分WAF防护,存在S2-045就存在S2-046。http://struts.apache.org/docs/s2-046.html 2017-03-07: 增加安恒信息研究员nike.zheng发现的S2-045,jakatar处理复杂数据类型时,异常处理不当,导致OGNL代码执行,通过在请求的Content-Type头构造OGNL表达式来执行Java代码。http://struts.apache.org/docs/s2-045.html 2016-04-26: 增加最新的S2-032远程代码执行漏洞,和S2-019很相似。 参考:http://seclab.dbappsecurity.com.cn/?p=924 2015-12-01: 采用scanner读数据流,再也不用担心s16不能执行net user/ipconfig/netstat -an等命令了。 增加复杂数据包(multipart/form-data)提交方式进行漏洞利用,可绕过部分防护。可执行命令,暂时无法上传文件。 2014-11-12: 最近遇到s19这个debug模式开启导致代码执行,这个有点少,但还是有一些,为了方便大家把13版本修改了一下。可以利用这个漏洞执行命令、上传shell。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值