- 浏览: 254831 次
- 性别:
- 来自: 未定
文章分类
最新评论
-
zl544434558:
最后一行</filter> 多余的
Springmvc 乱码问题 -
imknown:
方法有效,感谢博主!
Springmvc 乱码问题 -
notafreak:
方法有效,感谢博主
Springmvc 乱码问题 -
linvar:
ligangdufs 写道 what about resin ...
Springmvc 乱码问题 -
ligangdufs:
what about resin
Springmvc 乱码问题
近期开发框架选择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);
尽量使用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);
评论
4 楼
linvar
2011-07-06
楼主现在用springmvc + spring + mybatis了
3 楼
gaolinwu
2011-07-06
,唉,一直也是在用这个整合框架,自己也整合过,但是你的却没有看懂
2 楼
sunnyxpy
2010-12-30
刚接触,还看不大懂,楼主给个例子呗~~
1 楼
fisherhe
2010-06-25
例子贴出来?楼主
发表评论
-
Springmvc 乱码问题
2012-08-24 22:54 12047后端的Tomcat server.xml里配置的<Con ... -
logback note
2012-04-01 16:35 01.logback核心 a.Logger(记录者), b.A ... -
java Cookie
2011-11-01 21:33 1343cookie的组成部分: key, value, maxage ... -
java 正则表达式
2011-10-30 14:53 2109正则表达式的关键是 创建用于"在源字符串中匹配出某些 ... -
java email
2011-10-27 17:04 0http://coolshell.cn/articles/42 ... -
java 定时器
2011-10-27 11:26 01.java.util.Timer 1.1 对应单个后台线程 ... -
Resin note1
2011-06-27 23:11 1609<cluster-default> ... -
Maven note2
2011-04-25 14:16 01.The Build Lifecycle A build ... -
Maven note1
2011-04-24 18:07 01.What is Maven Although there ... -
freemarker使用总结
2010-06-20 16:51 34581.freemarker在使用spring MVC时会出现乱 ... -
springmvc 笔记
2010-05-22 10:27 6792struct2很不好用,springmvc比较给力,跟spr ... -
ibatis3 的返回值
2010-05-11 16:43 1149当执行sqlSession.selectOne("& ... -
java static util, helper
2010-04-18 19:50 1543我们经常会写一些util,helper类, 而一般这些类的方 ... -
Quartz 使用笔记
2010-04-16 21:35 13801.Quartz的主要构件: SchedulerFactory ... -
pinyin4j 笔记
2010-04-05 21:00 1766最近需要将城市名称转换为拼音去访问google weather ... -
ibatis3 的变量
2010-04-05 10:12 1713ibatis的变量其实没什么特别,就是使用#{}定义, 比如 ... -
VPS中的resin3.1和tomcat6
2010-03-28 11:00 1721兴冲冲地在VPS安装了jdk1.6, resin-pro-3. ... -
java URL encoding and decoding
2010-03-19 09:48 8647HTML编码规则是: 字符"a"-&quo ... -
Cookie小解
2010-03-18 19:27 1225Cookie是客户端与服务器 ... -
ibatis3的使用参考
2010-03-12 17:03 3908以前用过ibatis2,但是听说ibatis3有较大的性能提升 ...
相关推荐
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)