posts - 33, comments - 46, trackbacks - 0, articles - 2

集成struts+spring的新思路

Posted on 2007-01-11 17:27 在路上... 阅读(2179) 评论(7)  编辑  收藏 所属分类: JAVA相关

网上有好多种struts+spring的集成思路,例如 http://dev.csdn.net/author/hql638/35679289a9a94e4f97e999508df064db.html
这篇文章就介绍得很详细,介绍了下面三种方法:

使用 Spring 的 ActionSupport 类整合 Structs
使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor
将 Struts Action 管理委托给 Spring 框架
其实在使用spring+struts时,我们往往就是想使用Ioc的特性,减少业务逻辑组件之间的依赖关系,通过高度灵活的XML配置提高业务的灵活性和扩展性。步骤如下:
首先依旧加入spring的context plugin到struts-config.xml中

< struts-config >
  
< plug-in
         
className ="org.springframework.web.struts.ContextLoaderPlugIn" >
     
< set-property  property ="contextConfigLocation"
         value
="/WEB-INF/applicationContext.xml"   />
  
</ plug-in >
</ struts-config >


然后包装一下struts的DispatchAction,提供一个方法可以直接获取Spring的WebApplicationContext对象。

package  com.cngd.dataview.action; 

import  org.apache.struts.actions.DispatchAction;
import  org.springframework.web.context.WebApplicationContext;
import  org.springframework.web.struts.DelegatingActionUtils; 

/**   */ /**
 * Date: 2007-1-11 16:57:48
 *
 * 
@author  midea0978
 * 
@version  1.0
 
*/

public   class  CommDispatchAction  extends  DispatchAction  {
    
protected  WebApplicationContext getAppContext()  {
        WebApplicationContext context 
=  DelegatingActionUtils.findRequiredWebApplicationContext( this .getServlet(),  null );
        
return  context;
    }

}
 


然后自己的action可以直接从CommDispatchAction继承通过this.getAppContext();获取WebApplicationContext,这样对原有的
struts程序架构体系几乎没有太大的变化,同时可以引入spring的Ioc特性到现有系统中,这个与ActionSupport 中的
getWebApplicationContext()方法类似了,但是可以不必拘泥于在两者之间转来转去的。

package  com.cngd.dataview.action; 

import  com.spring.bo.WeatherService;
import  org.apache.log4j.Logger;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;
import  org.springframework.jdbc.core.JdbcTemplate;
import  org.springframework.jdbc.datasource.DriverManagerDataSource;
import  org.springframework.web.context.WebApplicationContext; 

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse; 

/**   */ /**
 * Date: 2007-1-11 16:19:15
 *
 * 
@author  midea0978
 * 
@version  1.0
 
*/

public   class  DataViewAction  extends  CommDispatchAction  {
    
static  Logger logger  =  Logger.getLogger(DataViewAction. class .getName()); 

    
/**   */ /**
     * 
@param  actionMapping
     * 
@param  actionForm
     * 
@param  request
     * 
@param  response
     * 
@return
     * 
@throws  Exception
     
*/

    
public  ActionForward genReport(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
            
throws  Exception  {
        String yymm 
=  request.getParameter( " yymm " );
        String opname 
=  request.getParameter( " opname " );
        logger.info(
" 参数: "   +  yymm  +   " , "   +  opname);
        WebApplicationContext ctx 
=   this .getAppContext();
        WeatherService srv 
=  (WeatherService) ctx.getBean( " weatherServiceBean " );
        srv.showWeather();
        DriverManagerDataSource ds 
=  (DriverManagerDataSource) ctx.getBean( " datasource " );
        JdbcTemplate jt 
=   new  JdbcTemplate(ds);
        String sql 
=   " select count(*) from tab " ;
        
int  rows  =  jt.queryForInt(sql);
        System.out.println(rows);
        
return  actionMapping.findForward( " viewresult " );
    }
 


}
 

Feedback

# re: 集成struts+spring的新思路  回复  更多评论   

2007-01-12 09:04 by jackhlp
这样集成的话,具体的每一个Action都与Spring耦合在一起了,因为你继承DispatchAction的类里面返回的是一个WebApplicationContext。

# re: 集成struts+spring的新思路  回复  更多评论   

2007-01-13 21:53 by Christ Chang
这个也算新思路?????????

# re: 集成struts+spring的新思路  回复  更多评论   

2007-01-13 22:47 by 千山鸟飞绝
同意一楼的说法,你这样做,并没有很好的利用spring的轻耦合的特点。
IOC的目的之一也就是降低耦合。

所以不觉得你这样做有多好。

# re: 集成struts+spring的新思路[未登录]  回复  更多评论   

2007-01-22 14:02 by hiswing
不管怎么说,还是值得鼓励的。

# re: 集成struts+spring的新思路  回复  更多评论   

2007-01-27 19:02 by 别出风头了你
其实这是模仿webwork和springMVC的一种和手段,目的就是为了,让action中service不需要任何方式,只需要getter和setter的方式就可以得到,其实这也是为病入膏肓的struts1,一种补救,
其实如果有必要的话,你可以尝试使用struts2,基于webwork的技术,完全整合如spring,不需要任何东西,其实struts2的好处,不仅仅这么点,具体的需要你自己领悟了。

# re: 集成struts+spring的新思路  回复  更多评论   

2007-01-28 12:50 by 刚才整合两种
我也是你这么整合的,但好像他们说这样不好。具体我也不知道。。郁闷呀。那大家提供一个好的方法来整合咯。。我要 学习!

# re: 集成struts+spring的新思路  回复  更多评论   

2007-07-15 12:02 by sclsch
如果你可继承一个action,而不是dispatchaction,怎么办?

只有注册用户登录后才能发表评论。


网站导航: