struts2框架的第一部分的总结

1.介绍struts2框架
struts2=struts1+xwork
2.struts2的快速入门
index.jsp—helloaction—hello.jsp

第一步:导入jar包
第二部:创建jsp页面
第三部:对struts2框架进行配置
1.web.xml文件中配置前端控制器(核心控制器)—- 就是一个Filter
目的:是为了让sturuts2框架可以运行。

2.创建一个struts.xml文件,这个是struts2框架配置文件,你的struts2框架才会生效。
目的:是为了struts2框架的流程可以执行。
这里写图片描述
这里写图片描述
如上图 :是核心拦截器的位置。

创建一个struts.xml文件
名称必须叫struts.xml
位置:src下(classes)

第四部:创建一个HelloAction类,创建一个public无参数的方法。返回值是String类型的方法。叫什么无所谓

第五步配置struts.xml

第六步 在index.jsp添加链接 测试

struts2的流程分析以及工具配置
1.流程分析
请求—–StrutsPrepareAndExecuteFileter 核心控制器—–Intercepter 拦截器(实现代码功能)—-Action的execute—-结果页面Result
拦截器在Struts-default.xml定义
执行拦截器是defaultStack中引用拦截器

2.关于关于手动配置struts.xml文件中提示操作

     如果安装Aptana编辑器 ,请不要用Aptana自带xml编辑器 编写struts2配置文件 
     struts.xml提示来自于 DTD约束, 
        <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
        如果可以上网,自动缓存dtd,提供提示功能
        如果不能上网,也可以配置本地DTD提示 

    *** 导入DTD时,应该和配置DTD版本一致 

3.关联struts2源文件
如果是com.opensymphony.xxx 在xwork-core下
如果是org.apache.struts2 在core下


struts2配置(重点)
1.struts2配置文件加载顺序
struts2框架要能执行,必须先加载strutsPrepareAndExecuteFilter.
在strutsprepareAndexecuteFilter的init方法中对dispather进行了初始化
在Dispatcher类中定义的init方法内就描述了struts2配置文件加载的顺序


    init_DefaultProperties(); // [1]   ----------  org/apache/struts2/default.properties 
       init_TraditionalXmlConfigurations(); // [2]  --- struts-default.xml,struts-plugin.xml,struts.xml
            init_LegacyStrutsProperties(); // [3] --- 自定义struts.properties 
            init_CustomConfigurationProviders(); // [5]  ----- 自定义配置提供
            init_FilterInitParameters() ; // [6] ----- web.xml 
            init_AliasStandardObjects() ; // [7] ---- Bean加载 

1.作用:定义了struts2框架中所有常量
位置: org/apache/struts2/default.properties
这里写图片描述
这里写图片描述
2.2.struts-default.xml
作用:配置了bean,interceptor,result等。
位置:在struts的core核心jar包.
struts-plugin.xml
它是struts2框架中所使用的插件的配置文件。
struts.xml
我们使struts2所使用的配置文件。
3. 自定义的struts.properties
就是可以自定义常量。
4.web.xml

在开发中,后加载文件中的配置会将先加载文件中的配置覆盖。


2.关于Action的配置

    1.<package>  作用:是用于声明一个包。用于管理action。
        1.name     它用于声明一个包名,包名不能重复,也就是它是唯一的。 
        2.namespace  它与action标签的name属性合并确定了一个唯一访问action的路径。
        3.extends  它代表继承的包名。
        4.abstrace 它可以取值为true/false,如果为true,代表这个包是用于被继承的。
    2<action>  用于声明 一个action
        1.name  就是action的一个名称,它是唯一的(在同包内) 它与package中的namespace确定了访问action的路径。
        2.class Action类的全名
        3.method 要访问的Action类中的方法的名称,方法无参数 ,返回值为String.
    3.<result> 用于确定返回结果类型
        1.name  它与action中的方法返回值做对比,确定跳转路径。

    关于action配置其它细节:
        1.关于默认值问题
            <package namespace="默认值"> namespace的默认值是""
            <action class="默认值"  method="默认值">
                class的默认值是  com.opensymphony.xwork2.ActionSupport

                method的默认值是  execute
            <result\d   X name="默认值"> name的默认值是 "success"       

        2.关于访问action的路径问题   
            现在的action的配置是:
            <package name="default" namespace="/" extends="struts-default">
                <action name="hello" class="cn.itcast.action.DefaultAction">
                    <result>/hello.jsp</result>
                </action>
            </package>

            当我们输入:
            http://localhost/struts2_day01_2/a/b/c/hello
            也访问到了action。

            原因:struts2中的action被访问时,它会首先查找
             1.namespace="/a/b/c"  action的name=hello  没有.
             2.namespace="/a/b     action的name=hello  没有
             3.namespace="/a"      action的name=hello  没有
             4.namespace="/"        action的name=hello  查找到了.

             如果最后也查找不到,会报404错误.

        3.默认的action。
            作用:处理其它action处理不了的路径。

            <default-action-ref name="action的名称" />
            配置了这个,当访问的路径,其它的action处理不了时,就会执行name指定的名称的action。

        4.action的默认处理类
            在action配置时,如果class不写。默认情况下是 com.opensymphony.xwork2.ActionSupport。

            <default-class-ref class="cn.itcast.action.DefaultAction"/>
            如果设置了,那么在当前包下,默认处理action请的的处理类就为class指定的类。

关于常量配置
default.properties 它声明了struts中的常量。

    问题:人为设置常量,可以在哪些位置设置 ?
        1.struts.xml(应用最多)
            <constant name="常量名称" value="常量值"></constant>
        2.struts.properties(基本不使用)          
        3.web.xml(了解)
            配置常量,是使用StrutsPrepareAndExecuteFilter的初始化参数来配置的.
            <init-param>
                <param-name>struts.action.extension</param-name>
                <param-value>do,,</param-value>
            </init-param>

    常用常量
        struts.action.extension=action,, 
        这个常量用于指定strus2框架默认拦截的后缀名.

        <constant name="struts.i18n.encoding" value="UTF-8"/>  
            相当于request.setCharacterEncoding("UTF-8"); 解决post请求乱码 

        <constant name="struts.serve.static.browserCache" value="false"/> 
            false不缓存,true浏览器会缓存静态内容,产品环境设置true、开发环境设置false  

        <constant name="struts.devMode" value="true" />  
            提供详细报错页面,修改struts.xml后不需要重启服务器 (要求)

struts.xml文件的分离:

    目的:就是为了阅读方便。可以让一个模块一个配置文件,在struts.xml文件中通过
    <include file="test.xml"/>导入其它的配置文件。

这里写图片描述
这里写图片描述
struts.xml文件的分离:

    目的:就是为了阅读方便。可以让一个模块一个配置文件,在struts.xml文件中通过
    <include file="test.xml"/>导入其它的配置文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值