其实很简单,只需要两步:
1.在Web.xml文件中加入下面代码,从而加载spring的配置文件。
contextConfigLocation /WEB-INF/applicationContext.xml context org.springframework.web.context.ContextLoaderServlet 1
2.为了使Action得到Bean,写一个BaseAction类,以得到想要的Bean。
BaseAction类
/**
*
*/
package com.leon.struts.action;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;
import com.leon.service.JobService;
import com.leon.service.NewsService;
/**
* @author Leon Sui
*
* Date: Mar 4, 2009
*
*/
public class BaseAction extends ActionSupport {
protected Object getBean( String name ) {
WebApplicationContext ctx = getWebApplicationContext();
return ctx.getBean( name );
}
//得到Service bean
protected JobService getJobService() {
return (JobService)getBean("jobService");
}
}
其他的就是正常的注册bean,正常写struts-config配置文件。例如;
struts-config.xml
<!--Dao-->
com/leon/model/news.hbm.xml com/leon/model/job.hbm.xml com/leon/model/cases.hbm.xml org.hibernate.dialect.MySQLDialect true thread org.hibernate.cache.EhCacheProvider 10 update
Action类:红色的就是得到bean
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/659894/viewspace-559932/ ,如需转载,请注明出处,否则将追究法律责任。