原文
第一种集成方法
原理:在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象
缺点:产生了依赖,spring的类在action中产生了依赖查找。(注意和依赖注入的区别(前者主动))。
1、spring和struts依赖库配置
2、在struts的Action中调用如下代码取得BeanFactory
BeanFactoryfactory =WebApplicationContextUti |
使用listener配置beanfactory,将其初始化交给servlet,使其维持在ServletContext中,节省资源。(Listener初始化早于Servlet(Weblogic8除外))
<context-param> </context-param> |
3、通过BeanFactory取得业务对象,调用业务逻辑方法
补充:(Struts1.x相关并和Spring集成)
扩展学习:
lJboss的jar包加载顺序(根据字母),因此可能使得有些包无法加载。
l 使用高级模板创建的jsp文件,由于有
<% String path= request.getContextPath(); StringbasePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> ------------ <basehref="<%=basePath%>"/> |
lServletListener
Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEventse)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:
ServletContextAttributeL
ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEventsce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEventsce)方法。
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发sessionCreated(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed(HttpSessionEvent se)方法。
HttpSessionAttributeList
第二种集成方案
原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询
(客户端请求---->代理action--->取得beanFactory--->getBean(..)创建action示例--->执行exctute方法)
1、spring和struts依赖库配置
2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来
3、在struts-config.xml文件中配置Action
4、在spring配置文件中需要定义struts的Action,如: