ITeye博客

spring+struts的集成(第一种集成方案)

gundumw100 · 2010-01-19 15:43 · spring · 企业架构 · Spring · Struts · XML · JSP · Web · Markdown

spring+struts的集成(第一种集成方案,不常用,因为具有代码侵入性) 原理:在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象 1、spring和struts依赖库配置 * 配置struts --拷贝struts类库和jstl类库 --修改web.xml文件来配置ActionServlet --提供struts-config.xml文件 --提供国际化资源文件 * 配置spring --拷贝spring类库 --提供spring配置文件 2、在struts的Action中调用如下代码取得BeanFactory BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()); 3、通过BeanFactory取得业务对象,调用业务逻辑方法 配置web.xml文件: [code="java"] index.jsp action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 2 detail 2 2 action *.do contextConfigLocation classpath*:applicationContext-*.xml org.springframework.web.context.ContextLoaderListener [/code] 在Action 中可以如下使用: [code="java"] public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm laf = (LoginActionForm)form; //UserManager userManager = new UserManagerImpl(); //userManager.login(laf.getUsername(), laf.getPassword()); //BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml"); //UserManager userManager = (UserManager)factory.getBean("userManager"); //userManager.login(laf.getUsername(), laf.getPassword()); BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()); //ApplicationContext pc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()); UserManager userManager = (UserManager)factory.getBean("userManager"); userManager.login(laf.getUsername(), laf.getPassword()); return mapping.findForward("success"); } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。