扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月24日
关键字:
在本页阅读全文(共2页)
其实就是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[i]);
}
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[i]);
}
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();
}
}
<?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"
<bean id="skillDao" class="org.apache.struts2.showcase.dao.SkillDao"/>
<bean id="employeeDao" class="org.apache.struts2.showcase.dao.EmployeeDao"/>如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。