struts2简单入门

    1. 框架是什么,框架有什么作用
      1. 框架 实现部分功能的代码.
      2. 作用 控制请求和响应.
    2. 相对于WEB项目的执行流程
    3. struts2项目搭建流程
      1. 配置web.xml 配置前端控制器[核心控制器] ---一个filter.
        1. 包名:org.apache.struts2.dispatcher.ng.filter.
        2. 类名:StrutsPrepareAndExecuteFilter
        3. 1  <filter>
          2        <filter-name>struts2</filter-name>
          3        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
          4  </filter>
          5 
          6  <filter-mapping>
          7        <filter-name>struts2</filter-name>
          8        <url-pattern>/*</url-pattern>
          9  </filter-mapping>
      2. 配置struts.xml --struts2框架配置文件
        1. 作用:控制struts2框架执行的流程
        2. 文件名称:struts.xml
        3. 路径:src
        4. struts配置文件的加载顺序
          1. init_DefaultProperties(); // [1] ---------- org/apache/struts2/default.properties
          2. init_TraditionalXmlConfigurations(); // [2] --- struts-default.xml,struts-plugin.xml,struts.xml
          3. init_LegacyStrutsProperties(); // [3] --- 自定义struts.properties
          4. init_CustomConfigurationProviders(); // [5] ----- 自定义配置提供 init_FilterInitParameters() ; // [6] ----- web.xml
          5. init_AliasStandardObjects() ; // [7] ---- Bean加载
      3. 创建Action类的三种方式
        1. POJO类 --除了继承Object类 ,不存在其他继承,实现的类 优点:无耦合 缺点:都要自己去实现.
        2. 实现Action类接口 --实现接口的execute方法,并且提供5个返回视图. 优点:耦合度低 缺点:都要自己去实现.

        3. 实现ActionSupport类 --在Action接口的基础上加强了 表单校验,错误信息设置,获取国际化信息 优点:耦合低 提供的功能多.
      4. struts.xml配置
        1. 1 <?xml version="1.0" encoding="UTF-8"?>
          2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
          3 <struts>
          4 
          5 <!--各种配置-->
          6 
          7 </struts>

           

      5. Action配置
        1. 1 <package> //声明一个包,管理Action.
          2 name //这个包的名称[唯一值]
          3 namespace//和Action的name 属性合并确定一个唯一的地址
          4 extends//要继承的其他包明
          5 abstrace//这个包是否可以被继承 true可以继承
          1 <action> //声明一个Action.
          2 name //和包的namespace 合并 确定一个唯一地址
          3 class //这个action要去调用类的类全名 查看Action创建的三种方式 默认第三种
          4 method //这个action要去执行这个类的方法 默认[execute]
          5  
          6 <result>用于确定返回结果返回视图
          7 name //和action[method]返回结果对应,返回一个视图.默认[success]

          1. 演示:
          2. 1  <package name="default" namespace="/" extends="struts-default">
             2         <action name="hello" class="cn.itcast.action.DefaultAction">
             3             <result>/hello.jsp</result>
             4         </action>
             5     </package>
             6 
             7 此时输入: http://localhost/struts2_day01_2/a/b/c/hello 也访问到了action。
             8 
             9     * 原因:struts2中的action被访问时,它会首先查找
            10         * 1.namespace="/a/b/c"  action的name=hello  没有.
            11         * 2.namespace="/a/b     action的name=hello  没有
            12         * 3.namespace="/a"      action的name=hello  没有
            13         * 4.namespace="/"       action的name=hello  查找到了.
            14     * 如果最后也查找不到,会报404错误.
          3. 默认Action的配置和作用
          4. 1 作用 处理其他Action处理不了的路径.
            2 <default-action-ref name="action的名称" />
            3 class 指定当前包默认执行的类.

             

          
          
posted @ 2017-07-17 00:20  你真的好美  阅读(225)  评论(0编辑  收藏  举报