struts2+spring2.5+velocity+junit整合培训.ppt_第1页
struts2+spring2.5+velocity+junit整合培训.ppt_第2页
struts2+spring2.5+velocity+junit整合培训.ppt_第3页
struts2+spring2.5+velocity+junit整合培训.ppt_第4页
struts2+spring2.5+velocity+junit整合培训.ppt_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Struts2 Spring2 5 velocity整合 Spring是什么 Spring是一个开源框架 Spring是一个轻量级的控制反转 IoC 和面向切面编程 AOP 容器框架 他的主要目的是简化企业开发 提供功能强大的基于javaBeans的配置管理 他使组织应用变的容易和迅速 为什么要使用Spring 降低组建之间的耦合度 实现软件各层之间的解耦 可以使用容器提供的众多服务 如 事务管理服务 消息服务等等 当我们使用容器管理事务时 开发人员就不需要手工控制事务 也不需要控制复杂的事务传播Spring对于主流的应用框架提供了集成支持 如 Struts Hibernate webwork等 大大的提升了开发效率和应用性能 搭建Spring开发环境 到http www springsource org download下载spring 让后进行解压缩 在解压缩目录中找到下面jar文件 拷贝到工程lib中 dist spring jar包含了所有标准的spring模块lib jakart commons commons logging jar使用该库输出日志信息lib log4j 1 2 14 jar如果使用切面编程 AOP 还需要下列jar文件lib aspectj aspectjweaver jar和aspectjrt jarlib cglib cglib nodep 2 1 3 jarSpring的配置文件 一个典型的Spring项目需要创建一个或多个Bean配置文件 这些配置文件用于在SpringIOC容器里配置Bean 配置文件可以放在classpath下 也可以放在其它目录applicationContext xml 在web容器中实例化Spring容器 contextConfigLocationclasspath applicationContext xmlorg springframework web context ContextLoaderListener 使用Spring JDBC集成 classpath jdbc properties Spring注入依赖 Spring测试框架 首先定义一个测试类的父类BaseTestCase 它继承自spring框架的AbstractTransactionalDataSourceSpringContextTests类 该类主要用来做集成测试 它封装了junit packagecom baihang base importorg springframework test AbstractTransactionalDataSourceSpringContextTests publicclassBaseTestCaseextendsAbstractTransactionalDataSourceSpringContextTests protectedString getConfigLocations returnnewString classpath applicationContext xml publicBaseTestCase setDependencyCheck false 告知不进行属性依赖性检查 设置事务不会回滚super setDefaultRollback false 设置是否自动装配 按名字装配 只要在spring配置文件中有个bean的idsetAutowireMode AUTOWIRE BY NAME Spring测试框架 packagecom baihang test importcom baihang base BaseTestCase importcom baihang dao TestDao publicclassDaoTestCsaeextendsBaseTestCase privateTestDaotestDao 测试dao是否为空publicvoidtestDaoNotNull throwsException assertNotNull testDao publicvoidtestgetStr System err println testDao returnStr 测试Junit publicTestDaogetTestDao returntestDao publicvoidsetTestDao TestDaotestDao this testDao testDao 启动测试 选中测试的类右键RunAs JUnitTest 正确 错误 Struts2工作原理 一个请求在Struts2框架中的处理大概分为以下几个步骤1客户端初始化一个指向Servlet容器 例如Tomcat 的请求2这个请求经过一系列的过滤器 Filter 这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器 这个过滤器对于Struts2和其他框架的集成很有帮助 例如 SiteMeshPlugin 3接着FilterDispatcher被调用 FilterDispatcher询问ActionMapper来决定这个请是否需要调用某个Action4如果ActionMapper决定需要调用某个Action FilterDispatcher把请求的处理交给ActionProxy5ActionProxy通过ConfigurationManager询问框架的配置文件 找到需要调用的Action类6ActionProxy创建一个ActionInvocation的实例 7ActionInvocation实例使用命名模式来调用 在调用Action的过程前后 涉及到相关拦截器 Intercepter 的调用 8一旦Action执行完毕 ActionInvocation负责根据struts xml中的配置找到对应的返回结果 返回结果通常是 但不总是 也可能是另外的一个Action链接 一个需要被表示的JSP FreeMarker Velocity的模版 在表示的过程中可以使用Struts2框架中继承的标签 在这个过程中需要涉及到ActionMapper在上述过程中所有的对象 Action Results Interceptors等 都是通过ObjectFactory来创建的 搭建struts2开发环境 搭建Struts2环境时 我们需要做以下几个步骤的工作 1 找到开发Struts2应用需要使用到的jar文件 放到工程lib下 2 编写Struts2的配置文件即 struts xml3 在web xml中加入Struts2框架启动配置strutsorg apache struts2 dispatcher FilterDispatcherstruts encodingFilterorg springframework web filter CharacterEncodingFilterencodingUTF 8encodingFilter Struts2应用 struts xml Struts2默认的配置文件为struts xml 这里我们结合前边所讲到的spring整合一起做 test show vmgetStr action Struts2应用 Action packagecom baihang action importcom baihang service TestService importcom opensymphony xwork2 ActionSupport publicclassTestActionextendsActionSupport privatestaticfinallongserialVersionUID 1L privateTestServicetestService 业务接口privateStringmsg 返回一个字符串显示到前台velocity模版 authorliyanbo publicStringgetStr msg testService returnStr spring struts2 velocity整合应用 returnSUCCESS publicStringgetMsg returnmsg publicvoidsetMsg Stringmsg this msg msg publicTestServicegetTestService returntestService publicvoidsetTestService TestServicetestService this testService testService Struts2应用 Velocity Spring struts velocity整合 msg结果页面显示 spring struts2 velocity整合应用 Velocity配置 1 找到开发velocity需要使用到的jar文件 放到工程lib下velocity 1 5 jar velocity tools 1 4 jar2 在web xml中加入velocity的配置velocityorg apache velocity tools view servlet VelocityLayoutServletorg apache velocity toolbox WEB INF toolbox xmlvelocity vm Velocity语法 1 变量赋值 set foo velocity变量赋值 输出结果为 foo velocity变量赋值 2 循环 嵌套循环 单层循环 set list 测试一 测试二 foreach elementin list element end输出的结果为 测试一测试二3 条件语句 if condition elseif condition else end4 注释 单行注释 多行注释 文档注释 5 include与 parse include和 parse的作用都是引入本地文件 Velocity语法 区别 1 与 include不同的是 parse只能指定单个对象 而 include可以有多个如果您需要引入多个文件 可以用逗号分隔就行 include one gif two txt three htm 2 include被引入文件的内容将不会通过模板引擎解析 而 parse引入的文件内容Velocity将解析其中的velocity语法并移交给模板 意思就是说相当与把引入的文件copy到文件中 parse是可以递归调用的 例如 如果dofoo vm包含如下行 递归调用Begin set cou

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论