三大框架综合测试

五、三大框架综合测试

  经过前面的四大步骤,我们已经成功地搭建好基于struts2+hibernate4+spring3这三大框架的整合开发环境,下面我们来综合测试一下三大框架配合使用进行开发的效果。

5.1、完善web.xml文件中的配置
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 6     <display-name></display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.jsp</welcome-file>
 9     </welcome-file-list>
10 
11     <!-- Spring监听器 -->
12     <listener>
13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14     </listener>
15     <!-- Spring配置文件位置 -->
16     <context-param>
17         <param-name>contextConfigLocation</param-name>
18         <param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
19     </context-param>
20     
21     <!-- 防止spring内存溢出监听器 -->
22     <listener>
23         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
24     </listener>
25     
26     <!-- openSessionInView配置 -->
27     <filter>
28         <filter-name>openSessionInViewFilter</filter-name>
29         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
30         <init-param>
31             <param-name>singleSession</param-name>
32             <param-value>true</param-value>
33         </init-param>
34     </filter>
35     <filter-mapping>
36         <filter-name>openSessionInViewFilter</filter-name>
37         <url-pattern>*.action</url-pattern>
38     </filter-mapping>
39     
40     <!-- Struts2的核心过滤器配置 -->
41     <filter>
42         <filter-name>struts2</filter-name>
43         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
44     </filter>
45     <!-- Struts2过滤器拦截所有的.action请求 -->
46     <filter-mapping>
47         <filter-name>struts2</filter-name>
48         <url-pattern>*.action</url-pattern>
49     </filter-mapping>
50     
51     <!-- druid监控页面,使用${pageContext.request.contextPath}/druid/index.html访问 -->
52     <servlet>
53         <servlet-name>druidStatView</servlet-name>
54         <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
55     </servlet>
56     <servlet-mapping>
57         <servlet-name>druidStatView</servlet-name>
58         <url-pattern>/druid/*</url-pattern>
59     </servlet-mapping>
60 </web-app>
5.2、编写测试代码

  在TestAction类中添加一个saveUser方法,如下:

 1 package me.gacl.action;
 2 
 3 import java.util.Date;
 4 import java.util.UUID;
 5 
 6 import me.gacl.model.User;
 7 import me.gacl.service.UserServiceI;
 8 
 9 import org.apache.struts2.convention.annotation.Action;
10 import org.apache.struts2.convention.annotation.Namespace;
11 import org.apache.struts2.convention.annotation.ParentPackage;
12 import org.springframework.beans.factory.annotation.Autowired;
13 
14 @ParentPackage("basePackage")
15 @Action(value="strust2Test")//使用convention-plugin插件提供的@Action注解将一个普通java类标注为一个可以处理用户请求的Action
16 @Namespace("/")//使用convention-plugin插件提供的@Namespace注解为这个Action指定一个命名空间
17 public class TestAction {
18     
19     /**
20      * 注入userService
21      */
22     @Autowired
23     private UserServiceI userService;
24 
25     /**
26      * http://localhost:8080/SSHE/strust2Test!test.action
27      * MethodName: test
28      * Description: 
29      * @author xudp
30      */
31     public void test(){
32         System.out.println("进入TestAction");
33         userService.test();
34     }
35     
36     /**
37      * http://localhost:8080/SSHE/strust2Test!saveUser.action
38      */
39     public void saveUser(){
40         User user = new User();
41         user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
42         user.setName("xdp孤傲苍狼");
43         user.setPwd("123456");
44         user.setCreatedatetime(new Date()); 
45         userService.save(user);
46     }
47 }

  执行【Maven install】操作,重新编译和发布项目,在执行【Maven install】操作之前,需要修改TestSpring这个测试类中的test方法的代码,如下:

 1 package me.gacl.test;
 2 
 3 import me.gacl.service.UserServiceI;
 4 
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 public class TestSpring {
10 
11     @Test
12     public void test(){
13         //通过spring.xml配置文件创建Spring的应用程序上下文环境
14         //ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml");
15         /**
16          *因为已经整合了Hibernate,UserServiceImpl类中使用到了userDao,
17          *userDao是由spring创建并且注入给UserServiceImpl类的,而userDao中又使用到了sessionFactory对象
18          *而创建sessionFactory对象时需要使用到spring-hibernate.xml这个配置文件中的配置项信息,
19          *所以创建Spring的应用程序上下文环境时,需要同时使用spring.xml和spring-hibernate.xml这两个配置文件
20          *否则在执行Maven install命令时,因为maven会先执行test方法中的代码,而代码执行到
21          *UserServiceI userService = (UserServiceI) ac.getBean("userService");
22          *这一行时就会因为userDao中使用到sessionFactory对象无法正常创建的而出错,这样执行Maven install命令编译项目时就会失败!
23          *
24          */
25         ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});
26         //从Spring的IOC容器中获取bean对象
27         UserServiceI userService = (UserServiceI) ac.getBean("userService");
28         //执行测试方法
29         userService.test();
30     }
31 }

  每次执行【Maven install】命令时都会执行Junit单元测试中的代码有时候感觉挺累赘的,有时候往往就是因为一些单元测试中的代码导致【Maven install】命令编译项目失败!

  将编译好的项目部署到tomcat服务器中运行,输入地址:http://localhost:8080/SSHE/strust2Test!saveUser.action进行访问,如下所示:

  

  访问action的过程中没有出现错误,并且后台也没有报错并且打印出了Hibernate执行插入操作时的SQL语句,如下所示:

  

  这说明三大框架整合开发的测试通过了。以上就是使用使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境的全部内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值