网上有好多种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 " );
} 

}