一、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:../spring/beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,struts-plugin.xml,../struts/struts.xml
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
二、测试代码
package junit.test;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import cn.itcast.bean.Employee;
import cn.itcast.bean.Gender;
import cn.itcast.service.EmployeeService;
public class EmployeeTest {
private static EmployeeService employeeService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext act = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/spring/beans.xml");
employeeService = (EmployeeService)act.getBean("employeeServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void save(){
employeeService.save(new Employee("cc", "123456"));
// new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void update(){
Employee employee = employeeService.find("liming");
employee.setGender(Gender.WOMEN);
employeeService.update(employee);
}
@Test
public void delete(){
employeeService.delete("liming");
}
@Test
public void list(){
List<Employee> ems = employeeService.list();
for(Employee em: ems)
System.out.println(em.getPassword());
}
@Test
public void find(){
Employee em = employeeService.find("liming");
System.out.println(em.getPassword());
}
}