struts2+spring+hibernate整合步骤(2)

时间:2022-05-04
本文章向大家介绍struts2+spring+hibernate整合步骤(2),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

*新建测试类

Java代码

1.public class EmployeeTest {    
2.private static EmployeeService employeeService;    
3.    
4.    @BeforeClass 
5.    public static void setUpBeforeClass() throws Exception {    
6.        try {    
7.            ApplicationContext context = new ClassPathXmlApplicationContext(    
8.                    "beans.xml");    
9.            employeeService = (EmployeeService) context    
10.                    .getBean("employeeServiceBean");    
11.        } catch (Exception e) {    
12.            System.out.println(e.getMessage());    
13.        }    
14.    }    
15.    
16.    @Test 
17.    public void createTable() {    
18.        //new ClassPathXmlApplicationContext("beans.xml");   
19.    };    
20.        
21.    @Test 
22.    public void save() {    
23.        boolean result=employeeService.save(new Employee("long","long"));    
24.        if (result) {    
25.            System.out.println("保存成功。。。。。");    
26.        }else{    
27.            System.out.println("保存出错....");    
28.        }    
29.    };    
30.    
31.    @Test 
32.    public void delete() {    
33.        boolean result=employeeService.delete("long");    
34.        if (result) {    
35.            System.out.println("删除成功。。。。。");    
36.        }else{    
37.            System.out.println("删除出错....");    
38.        }    
39.    };    
40.    
41.    @Test 
42.    public void update() {    
43.        boolean result=employeeService.update((new Employee("qing","long"));    
44.        if (result) {    
45.            System.out.println("更新成功。。。。。");    
46.        }else{    
47.            System.out.println("更新出错....");    
48.        }    
49.    };    
50.    
51.    @Test 
52.    public void findAll() {    
53.        List<Employee> elist=employeeService.findAll();    
54.        Iterator itor=elist.iterator();    
55.        while(itor.hasNext()){    
56.            Employee emp=(Employee)itor.next();    
57.            System.out.println(emp.getPassword());    
58.        }    
59.    };    
60.        
61.    @Test 
62.    public void find(){    
63.        Employee employee=employeeService.find("qing");    
64.        System.out.println(employee.getPassword());    
65.    }    
66.}    

*ok 没问题spring和hibernate整合完毕

*再将struts2所需包加入lib中

*创建struts.xml配置文件

Java代码

1.<?xml version="1.0" encoding="UTF-8" ?>

2.<!DOCTYPE struts PUBLIC

3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

4. "http://struts.apache.org/dtds/struts-2.0.dtd">

5.<struts>

6. <!-- 将struts的action交由spring管理 不在由struts的工厂介入 -->

7. <constant name="struts.objectFactory" value="spring" />

8.

9. <package name="employee" namespace="/employee" extends="struts-default">

10. <action name="list" class="employeeAction">

11. <result name="success">

12. /WEB-INF/feapp/employee.jsp

13. </result>

14. </action>

15.

16. <action name="manager_*" class="employeeManagerAction" method="{1}">

17. <result name="success">

18. /WEB-INF/feapp/employeeadd.jsp

19. </result>

20. <result name="message">

21. /WEB-INF/feapp/result.jsp

22. </result>

23. </action>

24. </package>

25.</struts>

*在web.xml中加入

Java代码  
1.<?xml version="1.0" encoding="UTF-8"?>    
2.<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
4.    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
5.    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
6.    
7.    <!--    
8.        指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找    
9.    -->    
10.    <context-param>    
11.        <param-name>contextConfigLocation</param-name>    
12.        <param-value>classpath:beans.xml</param-value><!-- 多个配置文件的写法  classpath:beans1.xml,classpath:beans2.xml,classpath:beans3.xml -->    
13.    </context-param>    
14.    <!-- 对Spring容器进行实例化 -->    
15.    <listener>    
16.        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
17.    </listener>    
18.    
19.    <!-- struts2 的监听器 -->    
20.    <filter>    
21.        <filter-name>struts2</filter-name>    
22.        <filter-class>    
23.            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    
24.    </filter>    
25.    <filter-mapping>    
26.        <filter-name>struts2</filter-name>    
27.        <url-pattern>/*</url-pattern>    
28.    </filter-mapping>    
29.    
30.    <welcome-file-list>    
31.        <welcome-file>index.jsp</welcome-file>    
32.    </welcome-file-list>    
33.</web-app>    

*创建相关jsp和action

Java代码

1.@Controller @Scope("prototype")    
2.public class EmployeeManagerAction extends ActionSupport {    
3.    @Resource EmployeeService employeeService;    
4.    private Employee employee;    
5.            
6.    public String addUI(){    
7.        //System.out.println("user come");   
8.        return SUCCESS;    
9.    }    
10.        
11.    public String add(){    
12.        //System.out.println("--------------");   
13.        boolean result=employeeService.save(employee);    
14.        //ActionContext.getContext().put("genders", Gender.values());   
15.        if(result){    
16.            ActionContext.getContext().put("message", "保存成功!");    
17.        }else{    
18.            ActionContext.getContext().put("message", "保存失败!");    
19.        }    
20.        return "message";    
21.    }    
22.    
23.    public Employee getEmployee() {    
24.        return employee;    
25.    }    
26.    
27.    public void setEmployee(Employee employee) {    
28.        this.employee = employee;    
29.    }    
30.}