• 『转』Struts2总结(二)


        1.对于struts.xml中的package:
            a.设置<package abstract="true">,则表明该包中没有action的配置,只定义一个基础公共的组件。
            b.package的namespace可以将不同的请求路径分隔开,在多人合作项目中用到比较多。当接收到一个请求,
                若在当前的namespace中没有相应的请求地址,则会到默认的namespace中去寻找匹配的地址。


        2.模型驱动机制(ModelDriven),将一个Action中的各个参数封装成为一个JavaBean(类似struts1.x中的
          ActionForm)。需要实现ModelDriven<T>接口,并实现getModel方法。当一个Action实现 ModenDriven接口后,
           该Action会被ModenDrivenInterceptor拦截,进行相应的参数设置。


        3.防止表单的重复提交:在jsp的表单中设置<s:token name="***"></s:token>标签。并且在相应的Action
           中设置token拦截器和名为invalid.token的返回结果。相应出错信息的key为 struts.message.invalid.token。
           配置该key,来实现国际化。


        4.struts2中Action与Servlet容器的耦合。主要有三种方式:
             a.实现ServletRequestAware或ServletResponseAware接口。并提供对request或者response熟悉的设置方法。
             b.使用ActionContext(但不能获得response对象)。改方法方便单元测试。
             c.使用ServletActionContext。ServletActionContext是ActionContext的子类。
           首选使用ActionContext,其次ServletActionContext。


        5.整合多个struts配置文件,在struts2的配置文件中使用include元素包含其他的配置文件。用于多模块开发。


        6.可以在web.xml中对struts2的核心过滤器FilterDispatcher设置初始化参数(不推荐使用)。


        7.动态方法调用(DynamicMethodInvocation).
             a.action配置中指定:<action name="×××" method="">
             b.在客户端即页面指定:<s:form action="method!actionName">
             c.使用通配符:<action name="*Login" class="com.action.LoginAction" method="{1}">,
                若Action的url为helloLogin,则调用LoginAction的hello来处理业务。此种方法简化了配置,但是
                使得程序不明了,不提倡使用该方法。


        8.struts2的结果类型。在struts2-core包中,有几种struts2已经配置好的结果类型。
            其中默认的类型为dispatcher,此外还有:redirectAction,chain,freemaker,httpheader,redirect,
            redirectAction,stream,velocity,xslt,plainText。


       9.struts2与spring的整合。导入struts2-spring-plugin包,在web.xml中设置spring的监听器,
           spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener
           struts2-spring-plugin包为我们将struts2的对象工厂设置为spring的IoC容器,其代码为:
           <struts>
          <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
          <!-- Make the Spring object factory the automatic default -->
          <constant name="struts.objectFactory" value="spring" />
          <package name="spring-default">
              <interceptors>
                  <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
                  <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
              </interceptors>
          </package>  
       </struts>
       很明显,将struts.objectFactory定位为 org.apache.struts2.spring.StrutsSpringObjectFactory
       其余的工作就交给spring的IoC容器去做了。
       另外:当我们需要增加spring的配置文件时,需要在web.xml中设定contextConfigLocation参数。代码如下:
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>其他的spring配置文件名,用逗号隔开</param-value>
        </context-param>

  • 相关阅读:
    【推荐】com.alibaba方式xml转json,能将xml的所有属性方法,全部转化为json
    关于使用JAXB读取xml文档转换为java对象报错:类的两个属性具有相同名称com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsExc...
    java正则,将<a或者</a,尖括号后面的字母改成大写
    javaben转xml
    三次握手四次挥手
    RPC接口测试(六)RPC协议解析(重要!重要!重要!)
    os.popen(cmd) 与 os.system(cmd) 的区别
    jmeter测试webSocket接口
    Ubuntu20.04 体验和美化
    API可视化管理平台YApi
  • 原文地址:https://www.cnblogs.com/minideas/p/1945257.html
Copyright © 2020-2023  润新知