资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
1、Struts2+Spring 整合博客分类: OA办公自动化系统SpringStrutsXMLJSPWeb Struts2和 Spring整合,创建一个 OA工程1、整合 struts21)导入 strut2的 jar包:commons-logging-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.12.jar,xwork-2.06.jar。暂时导入这些 jar包,到时候需要再倒入。2)将 struts.xml文件放置在 OA工程的 src目录下。3)在 web.xml里面配置 struts2用到的核心过滤器。Xml代码 1. 2. struts2 3. org.apache.struts2.dispatcher.FilterDispatcher 4. 5. 6. struts2 7. /* 8. 2、整合 spring1)导入 spring的 jar包:spring.jar2)将 applicationContext.xml文件放置在 OA工程的 WEB-INF目录下。3)在 web.xml里面配置 spring用到的监听器。Xml代码 1. 2. org.springframework.web.context.ContextLoaderListener 3. 4)添加 struts2-spring整合的插件:struts2-spring-plugin-2.0.12.jar,如果不使用这个插件,则需要在 struts.xml里面配置:Xml代码 1. 3、测试 struts2和 spring整合对不对?写一个 jsp页面 login.jsp来测试:Html代码 1. 2. 3. 4. 5. 6. 7. Insert title here 8. 9. 10. 11. 12. 13. 14. 15. 16. 写一个 LoginActionJava代码 1. package com.struts2.action; 2.3. import com.opensymphony.xwork2.ActionSupport; 4. import com.struts2.service.LoginService; 5. public class LoginAction extends ActionSupport 6. private LoginService loginService; 7. private String userName; 8. private String password; 9. public void setLoginService(LoginService loginService) 10.this.loginService = loginService; 11. 12.public String getUserName() 13.return userName; 14. 15.public void setUserName(String userName) 16.this.userName = userName; 17. 18.public String getPassword() 19.return password; 20. 21.public void setPassword(String password) 22.this.password = password; 23. 24.Override 25.public String execute() throws Exception 26.if(loginService.isLogin(userName, password) 27.return SUCCESS; 28.else 29.return INPUT; 30. 31. 写一个 ServiceJava代码 1. package com.struts2.service; 2.3. public interface LoginService 4. boolean isLogin(String userName,String password); 5. 写一个 Service的实现Java代码 1. package com.struts2.service.impl; 2.3. import com.struts2.service.LoginService; 4. public class LoginServiceImpl implements LoginService 5.6. public boolean isLogin(String userName, String password) 7. if(hello.equals(userName) & world.equals(password) 8. return true; 9. else 10.return false; 11. 12. 在 struts.xml里面配置:Xml代码 1. 2. 3. /result.jsp 4. /login.jsp 5. 6. 在 applicationContext.xml里面配置:Xml代码 1. 2. 3. 4. 启动 tomcat,分别输入 hello和 world,提交,结果为 hello和 world。说明struts2和 spring整合成功。Spring 整合 Struts 的三种方法 2008-01-08 16:33:08| 分类: Java | 标签: |举报 |字号大中小 订阅 无论使用那种方法,都必须先装载环境 .即在 Struts 配置文件中加入:使用 Spring 的 ContextLoaderPlugin 为 Struts 的 ActionServlet 装载 Spring 应用程序环境。方法如下:1. 使用 Spring 的 ActionSupport 类整合 Structs 2. 使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor 3. 将 Struts Action 管理委托给 Spring 框架方法一 : 使用 Spring 的 ActionSupport 类整合 Structs从 Action 类下手 ,就是新建一个 Action 让它派生自 Spring 中org.springframework.web.struts.ActionSupport 类,而不是 Struts 的 Action 类.Spring 的ActionSupport 类提供了一种简便的方法 getWebApplicationContext().代码片段: public class MyAction extends ActionSupport public ActionForward excute(.).ApplicationContext ctx = getWebApplicationContext(); BookService bookService = (BookService) ctx.getBean(bookService);.通过派生 Spring 的 ActionSupport 而不是 Struts 的 Action,创建一个新的 Action.getWebApplicationContext() 方法获得一个 ApplicationContext.然后查找一个 Bean.缺点: 它将 Struts 动作与 Spring 框架耦合在一起。如果您想替换掉 Spring,那么您必须重写代码。并且,由于 Struts 动作不在 Spring 的控制之下,所以它不能获得 Spring AOP 的优势。当使用多重独立的 Spring 环境时,这种技术可能有用,但是在大多数情况下,这种方法不如另外两种方法合适。方法二: 覆盖 Struts 的 RequestProcessor使用 org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖 Struts 的 RequestProcessor 处理程序代码片段: (struts 配置文件)然后在 Spring 的配置文件中注册动作:ca.nexcel.books.actions.SearchSubmit优点:这种设计使 Struts 动作并不知道它正被 Spring 管理,并且使您能够利用 Sping 的动作管理框架的所有优点。由于您的 Struts 动作注意不到 Spring 的存在,所以您不需要重写您的 Struts 代码就可以使用其他控制反转容器来替换掉 Spring.缺点:如果您使用一个不同的 RequestProcessor,则需要手动整合 Spring 的 DelegatingRequestProcessor。添加的代码会造成维护的麻烦并且将来会降低您的应用程序的灵活性。这种改变将会对这种解决方法的使用寿命造成负面的影响。方法三: 将动作管理委托给 Spring一个更好的解决方法是将 Strut 动作管理委托给 Spring。您可以通过在 struts-config 动作映射中注册一个代理来实现。代理负责在 Spring 环境中查找 Struts 动作。由于动作在 Spring 的控制之下,所以它可以填充动作的 JavaBean 属性,并为应用诸如 Spring 的 AOP 拦截器之类的特性带来了可能。代码片段:input=/searchEntry.dovalidate=truename=searchForm.Spring 环境中注册一个 Struts 动作:优点:动作委托解决方法是这三种方法中最好的。Struts 动作不了解 Spring,不对代码作任何改变就可用于非 Spring 应用程序中。RequestProcessor 的改变不会影响它,并且它可以利用 Spring AOP 特性的优点。一旦让 Spring 控制您的 Struts 动作,您就可以使用 Spring 给动作补充更强的活力。例如,没有 Spring 的话,所有的 Struts 动作都必须是线程安全的。如果您设置 标记的 singleton 属性为“false” ,那么不管用何种方法,您的应用程序都将在每一个请求上有一个新生成的动作对象。您可能不需要这种特性,但是把它放在您的工具箱中也很好。您也可以利用 Spring 的生命周期方法。例如,当实例化 Struts 动作时, 标记的 init-method 属性被用于运行一个方法。类似地,在从容器中删除 bean 之前,destroy-method 属性执行一个方法。这些方法是管理昂贵对象的好办法,它们以一种与 Servlet 生命周期相同的方式进行管理。使用了 struts和 spring一段时间.但是对其中他们的整合也用了好几次.就这次机会总结下经验并整理下思绪.整合方式 1:最原始而易懂的方式:Action继承 spring提供的类org.springframework.web.struts.MappingDispatchActionSupportAction中的代码:public class UserAction extends MappingDispatchActionSupport public ActionForward login(ActionMapping mapping, ActionForm form,HttpServletRequest request, Htt
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号