strust2.1 + spring3.0 + ibatis3.0 + annotation
文章分类:Java编程
近期开发框架选择strust2.1 + spring3.0 + ibatis3.0
尽量使用annotation,少使用xml,ibatis除外
1.strust2.1
主要使用到的plugin是Convention Pluin,因为他是实现Restful和annotation的关键。
ServletActionContext静态类可以得到很多运行环境
filterServlet接收到請求,使用ActionProxy分析配置文件,进而确定这个请求执行哪些interceptors,再调用对应action完成请求,返回请求前还可能需要执行一些interceptors,所以Strust的功能都是使用interceptor方式提供的,我们要处理的只是相应的action.strust提供了默认的interceptors stack,这样每个action都不用定义这些interceptors而直接使用他们的功能,而一些其他interceptor,或自定义的 interceptor则需要在action中定义.
暂时可能用到的interceptors: file-upload,object-population,servlet-config
如果有多个interceptors 也就是interceptor stack,他们的执行顺序有时挺重要的
(1).object popublation对象自动装载params
须有modelDriven,params这两个interceptor
action implements ModelDriven
public Person getModel(){
this.person = new Person();
return this.person;
}
(2).servlet config
须有servletConfig interceptor,
主要是为了得到HttpServletRequest,HttpServletResponse,
action 分别 implements ServletRequestAware,ServletResponseAware
(3).file-upload
须有fileUpload interceptor
在html中
<form name="myForm" enctype="multipart/form-data">
<input type="file" name="myDoc" value="Browse ..." />
<input type="submit" />
</form>
在action中
public void setMyDoc(File myDoc)
public void setMyDocContentType(String contentType)
public void setMyDocFileName(String filename)
注意setMyDoc跟html中的name="myDoc"对应
上传多个"myDoc"时,就用setMyDoc(File[] myDoc),下同
Struts2的action是有状态的,必须保证其线程的安全性,
也就是一个请求new一个action,如果使用spring管理则使用@Scope("prototype")修饰action.
如果主要使用annotation配置struts的功能,action类需要用@ParentPackage("default")修饰才能用到“default"package定义的东西,
比如interceptor,global-result等.
应该也可以直接在"default"那个package上标明namespace="/"
另一个比较郁闷的地方是使用Convention后并不能include其他文件,
比如<#include "/include/meta.html">
虽然默认的根模板是/WEB-INF/content/,但是必须使用
<#include "/WEB-INF/content/include/meta.html">也就是必须指定全路径
最后的解决方法是在action类上使用@ResultPath("/WEB-INF/content/")来指定根模板目录.
convention会在没有相对应的action类定义路径时直接返回模板文件,这有时对于返回静态文件很有用,这样就不用每个請求都需要一个action类对应着.但是这时的include又必须使用全路径了.
2.spring3.0
需要额外的类库:aopalliance.jar, aspectjweaver.jar, cglib-nodep-2.1_3.jar
主要使用到事务控制,依赖注入(DI)
通过在类型字段之上定义@AutoWare,spring会自动帮我们注入类型的对象到相应变量。
在类定义上使用@Component表示这个类让spring管理,另外还有几个相似的annotation
@Service表示业务层,@Controller表示控制层(类似strust的Action),
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。
3.ibatis3.0
SqlSessionFactory
每个应用对应一个SqlSessionFactory实体对象,在整个应用过程都会存在
要构建一个SqlSessionFactory对象,可以通过SqlSessionFactoryBuilder.build(reader)构建,
reader是Reader对象,读取ibatis的配置文件如SqlMapConfig.xml
SqlSession
从SqlSessionFactory.openSession()拿到一个sqlSession,
这个sqlSession是执行SQL Mapper命令的全部,
他的应用scope应该是线程的,可以在service层控制关闭session
接着就是ibatis的配置文件,影响到ibatis的行为,定义顺序不能乱,如
导入sql mapper文件就必须放到最后,
4.关于整合strust2.1,spring3.0,ibatis3.0
strust2.1与spring3.0集成比较简单
将strust的spring插件放到lib中,设置strust常量
<constant name="struts.objectFactory" value="spring" />
strust2.1的action是无状态的,而他的interceptor才是有状态的,也就是必须线程安全的,所以Action类不需要@Scope(“prototype”),一开始我以为需要.
而Strust的核心类,interceptor,results有没有交给spring管理呢,
按道理是有的,因为strust有个专门的spring插件
ibatis3.0没有提供与spring集成的插件,
spring提供的是ibatis 2版本,
所以我自己写了一个Ibatis3SqlSessionFactoryBean,
这个bean的任务是配置ibatis的基本属性,
enviroment,transactionManager,dataSource不再配置,
使用spring提供的dataSource,事务控制留给spring去管理.
这个bean implements FactoryBean<SqlSessionFactory>, InitializingBean
在afterPropertiesSet()方法中定义怎样build这个SqlSessionFactory,
其实在build这个sqlsessionFactory的时候可以参考ibaits3本身的build实现.
问题是那个environment在ibatis的配置文件上不存在了,因为都交给spring去管理了,
所以可以自己构造environment这个对象,再set到configuration中
再return new DefaultSqlSessionFactory(configuration);
其中的关键点在于那个datasource,因为我们的事务是交给spring管理了,所以这个datasource应该包装成 dataSourceToUse = new TransactionAwareDataSourceProxy(this.dataSource);
不然的话spring不会帮我们处理事务,也就是不会commit,close之类的.
变成要调用者完成这些复杂的事.
还有个问题是从sqlSessionFactory.openSession();拿到的sqlSession,
可以将它设置成ThreadLocal,这样在一个线程中拿到的sqlSession是相同的.
//------
经测试使用ThreadLocal会发生两次请求拿到的connection相同,导致第二次抛出connection已关闭的错误
//------
还可以使用
(SqlSession)TransactionSynchronizationManager.getResource(sqlSessionFactory);
分享到:
相关推荐
spring2.5+strust1+hibernate3.0JAR包集合
Strust2+Spring+ibatis整合开发实例的一个简单应用。
Strust+spring+hibernate新闻发布系统 Strust+spring+hibernate新闻发布系统 Strust+spring+hibernate新闻发布系统
Struts2.1、Spring3.0、Hibernate3.3整合与S2SH的XML文件拆分 很不错的整合实例 包括了s2sh框架的xml拆分
strust2+spring3+ibatis2 +oracle10g 完整整合框架,只要换个数据库连接和表名就可以运行,不像那些王八蛋骗人的,绝对可用
strust2+spring2.5+hibernate框架例题, 手动配的框架,lib下有详细的包.
Strust2.0+Spring+hiberante小例子,整合.
本资源是strust2.1.6+spring2.5+hibernate3.3整合开发的jar包
这是里面涉及很多技术,要有一定的基础知识、 里面这个系统可以直接使用 注意:本系统只做参考学习,禁止一切的商业行为
spring3+strust2+hibernate4
本人系统学习Struts2过程中的全部例子(推荐给初学者,能够系统的掌握Struts2的全部特性) 简单的例子,国际化,拦截器,类型转换,输入校验,OGNL,标签库,文件上传与下载,整合Spring 开发环境:Myeclipse7.0
NULL 博文链接:https://purelilium.iteye.com/blog/1697478
strust1.2+MYSQL5.0 实现strstrust1.2+MYSQL5.0 实现ust1.2+MYSQL5.0 实现
struts2.0 + spring + ibatis + json完整jar
Spring3+Strust2+Hibernate+Maven,测过了,没问题。
日常SSHF开发中,经常因为Jar包问题头痛。本文档收集了struts2+spring2.5+hibernate+freemarker 所用到的最基本依赖包,对解决Jar包问题非常有帮助。
strust1.2+MYSQL5.0 实现strust1.2+MYSQL5.0 实现strust1.2+MYSQL5.0 实现
Strust2+Json+ExtJs 6个程序代码(经典) Strust2+Json+ExtJs 6个程序代码(经典)
spring+strust2+hibernate 很全的jar包,可以下载后直接解压使用
SSH2框架搭建实例(spring3.2+strust2.3.4+hibernate4.2)