struts2

特点  使用Fitle作为控制器的MVC框架

环境配置  基本的13个包

      配置web.xml  配置Struts2的Fitle过滤器  可以从按转包中复制,版本要符合

      配置Struts.xml  

        根节点<struts>

          <package name=“包名” extends=“struts-default”>

          <constant name=“配置名” value=“新的配置值”>  (覆盖默认配置值action,空,)

          <action name=“请求名”>  <result>链接路径</result></action>

                         action中有默认的class=“类的全路径” method=“类中的方法” 不写即为默认,可自行更换

        <result name=“method的返回值”> name的值与method返回值匹配,在class类中返回          

         <result type=“响应的结果类型”>相应内容</result>

           type  dispatcher 转发  默认值,默认属性<param name=“location”>重定向的url</param>

               redirect 重定向  默认属性<param name=“location”>内部或外部的URL</param>

                        (外部url已http开头,不能重定向web-inf下的)

               redirectAction 重定向到action   默认属性<param name=“actionName”>action名字</param>

                               (不能传递action的状态)

               chain 转发到action  默认属性<param name=“actionName”>action名字</param>

                              <param nam=“method”>指定目标action类的方法</param>

                              (这个method优先级高于action设置的method)

                              (可以传递action的状态)

                          为了实现action链

action  action请求  代表应用程序的操作

     action类  处理action请求的类

           特点  属性方法定义符合POJO的要求

               form表单映射到action类的属性上,表单组件的name和属性名一至

               必须有无参的构造方法,需要进行反射

               至少有一个处理action请求的方法,要有String类型的返回值和配置文件中的result的name值进行匹配

               为每个请求创建一个action实例

      开发模式(实现了action类把form表单的字段值向实体类的转化)

        域模型方式(DomainModel)

            在action类中定义持久化类的类型属性

            form表单的字段名要使用  action持久化类名.属性名

        模型驱动方式(ModelDriven)

            action类要实现ModeDriven<实体类>  实现方法getMode()返回实体类的实例

            form表单的字段名要使用实体类的属性名

action访问web资源  

    解耦方式  对web资源进行封装,便于单元测试 

       actionContext方式  action执行的上下文对象,保存了执行action所需的的所有对象

       actionContext方式API  获取actionContext  actionContext.getContext()  调用自身的静态方法得到的实例,采用单例模式

                 采用Map<String,Object>进行封装  getParameters()  Object是String类型的数组,为了能接收同名的多个参数,用put()放入新值无效

                                 getApplication()

                                 getSession() 

                                 (Map<String,Object>)get("request")  得到封装的request集合,不能访问得到集合前的数据

       接口方式  实现接口方法,由struts2自动放入web资源,不需要重复获取

       接口方式API  ApplicationAware  void setApplication(Map<String,Object> arg0)要实现的方法

                SessionAware  void setSession(Map<String,Object> arg0)要实现的方法

                RequestAware  void setRequset(Map<String,Object> arg0)要实现的方法  不能取得之前的request值

                ParameterAware  void setParameter(Map<String,String[]> arg0)要实现的方法

    耦合方式  获取原生的web资源,必须有servlet容器的支持

      ServletActionContext方式  提供静态方法获得原始web资源

                 API  getServletContext()  得到原始的ServletContext,相当于内置对象application

                      getRequest()  得到原始的ServletRequset  可以用来得到getSession和getParameter

                             不能得到之前传入的request属性值

      接口方式  实现接口方法,由struts2自动放入原始web资源,不需要重复获取

        接口方式API  ServletRequsetAware  void setServletRequest(HttpServletRequest arg0)

                 ServletContextAware  void setServletContext(ServletContext arg0)

原文地址:https://www.cnblogs.com/jingfengling/p/6061327.html