zoukankan      html  css  js  c++  java
  • Spring和Struct整合的三个方法

    1.使用Spring 的 ActionSupport 。
    2.使用Spring 的 DelegatingRequestProcessor 类。
    3.全权委托。

    无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。 就是在 struts 中加入一个插件。struts-config.xml中

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

    spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml 在WEB-INF目录下面

    1,使用Spring的ActionSupport .
    Spring 的ActionSupport 继承至 org.apache.struts.action.Action
    ActionSupport的子类可以或得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。
    这是一个 servlet 的代码:

    1. public class LoginAction extends org.springframework.web.struts.ActionSupport {
    2. public ActionForward execute(ActionMapping mapping, ActionForm form,
    3. HttpServletRequest request, HttpServletResponse response) {
    4. LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
    5. //获得 WebApplicationContext 对象
    6. WebApplicationContext ctx = this.getWebApplicationContext();
    7. LoginDao dao = (LoginDao) ctx.getBean("loginDao");
    8. User u = new User();
    9. u.setName(loginForm.getName());
    10. u.setPwd(loginForm.getPwd());
    11. if(dao.checkLogin(u)){
    12. return mapping.findForward("success");
    13. }else{
    14. return mapping.findForward("error");
    15. }
    16. }
    17. }

    applicationContext.xml 中的配置

    <beans>
    <bean id=”loginDao” class=”com.cao.dao.LoginDao”/>
    </beans>

    这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自 org.springframework.web.struts.ActionSupport 使得struts和spring耦合在一起。
    但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean(“loginDao”))。

    2,使用Spring 的 DelegatingRequestProcessor 类
    DelegatingRequestProcessor  继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。sturts-config.xml  中  <controller processorClass=”org.springframework.web.struts.DelegatingRequestProcessor”/> 通过 <controller >来替代 org.apache.struts.action.RequestProcessor 的请求处理。

    1. public class LoginAction extends Action {
    2. //利用spring来注入这个对象。
    3. private LoginDao dao ;
    4.  
    5. public void setDao(LoginDao dao) {
    6. System.out.println("执行注入");
    7. this.dao = dao;
    8. }
    9.  
    10. public LoginDao getDao() {
    11. return dao;
    12. }
    13.  
    14. public ActionForward execute(ActionMapping mapping, ActionForm form,
    15. HttpServletRequest request, HttpServletResponse response) {
    16. LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
    17. //这样一改这行代码似乎没有必要了。
    18. //WebApplicationContext ctx = this.getWebApplicationContext();
    19. //LoginDao dao = (LoginDao) ctx.getBean("loginDao");
    20. User u = new User();
    21. u.setName(loginForm.getName());
    22. u.setPwd(loginForm.getPwd());
    23. //直接用dao来调用spring会将这个对象实例化。
    24. if(dao.checkLogin(u)){
    25. return mapping.findForward("success");
    26. }else{
    27. return mapping.findForward("error");
    28. }
    29. }
    30. }

    这里的LoginAction extends Action 说明 struts 每有和spring 耦合。
    看一下applicationContext.xml 中的配置。

    <beans>
    <bean id=”loginDao” class=”com.cao.dao.LoginDao”/>
    <bean name=”/login” class=”com.cao.struts.action.LoginAction”>
    <property name=”dao”>
    <ref local=”loginDao”/>
    </property>
    </bean>
    </beans>

    这里name=”/login”与struts中的path匹配class=”com.cao.struts.action.LoginAction”与struts中的type匹配还要为LoginAction提供必要的setXXX方法。获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。
    3,全权委托:
    Action 的创建和对象的依赖注入全部由IOC容器来完成。使用Spring的DelegatingActionProxy来帮助实现代理的工作
    org.springframework.web.struts.DelegatingActioProxy继承于org.apache.struts.action.Action.

    全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。

    1, <action>中 type指向的是spring 的代理类

    <struts-config>
    <data-sources />
    <form-beans >
    <form-bean name=”loginForm” type=”com.cao.struts.form.LoginForm” />
    </form-beans>
    <global-exceptions />
    <global-forwards />
    <action-mappings >
    <!– type指向的是spring 的代理类 –>
    <action
    attribute=”loginForm”
    input=”login.jsp”
    name=”loginForm”
    path=”/login”
    scope=”request”
    type=”org.springframework.web.struts.DelegatingActionProxy” >
    <forward name=”success” path=”/ok.jsp” />
    <forward name=”error” path=”/error.jsp” />
    </action>
    </action-mappings>
    <message-resources parameter=”com.cao.struts.ApplicationResources” />
    <plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
    <set-property property=”contextConfigLocation” value=”/WEB-INF/applicationContext.xml”/>
    </plug-in>
    </struts-config>

    2, 去掉struts-config.xml中 <controller >

    三种整和方式中我们优先选用 全权委托的方式。
    理由:
    1,第一种使得过多的耦合了Spring和Action .
    2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。

    总结一下:
    整合工作中的步骤:
    1.修改struts-config.xml
    2. 配置applicationContext.xml
    3.为Action添加get/set方法 来获得依赖注入的功能。

  • 相关阅读:
    OSCP Learning Notes Buffer Overflows(3)
    OSCP Learning Notes Buffer Overflows(5)
    OSCP Learning Notes Exploit(3)
    OSCP Learning Notes Exploit(4)
    OSCP Learning Notes Exploit(1)
    OSCP Learning Notes Netcat
    OSCP Learning Notes Buffer Overflows(4)
    OSCP Learning Notes Buffer Overflows(1)
    OSCP Learning Notes Exploit(2)
    C++格式化输出 Learner
  • 原文地址:https://www.cnblogs.com/Mr-Rocker/p/4057316.html
Copyright © 2011-2022 走看看