您的位置:首页 > 编程语言 > Java开发

项目中Struts/Spring/Hibernate的基本流程

2008-01-12 00:42 627 查看
Struts+Spring+Hibernate develepment process:

1.Write your business class : DTO,FormBean,Action,Service Interface,Service Implementation.

2.Write JSP pages.

3.struts-config.xml Configuration : FormBean,Action,Forward pages.

4.applicationContext-service.xml Configuration: add your Service Interface and Service Implementation.

5.Add your service factory Get method to ServiceFactory.java

6.Build project and Generate the Description file(*.hbm.xml) of DTO.

7.applicationContext.xml Configuration: add *.hbm.xml file to applicationContext for O/R mapping.

**********************************************************************************************
Spring+hibernate的单元测试Junit
 spring提供的单元测试是强大的,spring的单元测试很简单,封装的很好。我们要用spring的单元测试测试我们写的add,delete等方法时候需要spring提供的一个额外包spring-mock.jar,我已经传上来了。你只要熟悉单元测试,编写一个测试案例,然后把继承改为org.springframework.test.AbstractTransactionalDataSourceSpringContextTests就可以了,此时编译器会提示你要实现
  /**
  * 必须实现的方法
  */
  public String[] getConfigLocations(){
  String[] config = new String[]{"applicationContext.xml","applicationContext-dao.xml","applicationContext-hibernate.xml","applicationContext-service.xml"};
  return config;
  }
  看了大家应该明白,就是把你配置好的xml赋值给它,
  然后大家就可以通过下面方法:
  下面的applicationContext这个变量是你只要继承了刚才那个抽象类就可以得到的一个恒量。
  FriendService friendService = (FriendService)applicationContext.getBean("friendService");
  得到你的实例来进行业务逻辑测试了,是不是很简单,大家试试吧,它在此时完成以后会把数据库回滚一次,不会影响你的数据库记录,非常好。

spring中提供 ContextLoaderListenter类,用来加载context的xml文件。
spring为struts提供ContextLoaderPlugIn类,此类也可以加载context的xml文件。
区别在于,两种方式加载的WebApplicationContext,以不同的Key存放在ServletContext中。而如果你定义了HibernateFilter的话,spring会利用WebApplicationContextUtils来获取WebApplicationContext,而此类并不识别ContextLoaderPlugIn类所加载的上下文,此时便会抛出异常: No WebApplicationContext found: no ContextLoaderListener registered?
利用ContextLoaderListenter来加载dao、service级别的context,而对于struts的action,用ContextLoaderPlugIn加载。
**************************************************************************************************************

Spring MVC的简要开发流程:

1. 先写Controller
2. Controller将业务逻辑委派给Service完成
3. Service返回一个Domain Object Model
4. 将Domail Object Model封装成ModelAndView作为Controller的返回结果,并赋予View的名称。
5. InternalResourceViewResolver根据View名称取出对应的Jsp文件,创建一个包含前缀和后缀的真正的路径
6. 这些定义在spring-servlet.xml文件中
7. 配置文件:首先要在web.xml中配置ContextLoaderListener,介绍这个的文章非常多
<listener>
     <listener-class>
         org.springframework.web.context.ContextLoaderListener
     </listener-class>
</listener>
8. 在web.xml中加入DispatherServlet的配置 <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping> 9. spring会根据这个servlet的名字(在这里是spring)自动寻找 <名字>-servlet.xml(这里将会是:spring-servlet.xml)
10. 在spring-servlet.xml中,将service注射给controller

***********************************************************************************

Spring MVC的流程图



***********************************************************************************************

图解Spring MVC流程



注解:
1.spring mvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作。
2.DispatcherServlet查询一个或多个HandlerMapping,找到处理请求的Controller.
3.DispatcherServlet请请求提交到目标Controller
4.Controller进行业务逻辑处理后,会返回一个ModelAndView
5.Dispathcher查询一个或多个ViewResolver视图解析器,找到ModelAndView对象指定的视图对象
6.视图对象负责渲染返回给客户端。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息