葵花宝典 八 Struts2

什么是框架,框架有什么用
框架是实现部分功能的代码,使用框架可以简化企业级开发,提高开发效率。
学习框架,清楚的知道框架能做什么

什么是struts2 框架,它有什么用?
struts2是struts的下一代产品,struts2是apache的产品
struts2是一个标准的mvc框架。javaweb中的model2模式就是一个mvc模式
使用struts2框架 可以简化我们的web开发,并且降低程序的耦合度
xwork 他是webwork的核心
xwork提供了很多核心的功能,前端拦截器,运行时属性的表达验证,类型转换,强大的表达式语言


1.导入jar包
下载struts2的jar包
2.对struts2框架进行配置
1.web.xml文件配置前端控制器(核心控制器)—-就是一个Filter
目的是为了让struts2框架可以运行

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

            <filter-mapping>
                <filter-name>struts2</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>

还要创建一个struts.xml配置文件,这个是struts2框架的配置文件,
目的:是为了struts2框架流程可以执行
名称:struts.xml
位置:src下

创建action类
要求:在action类中创建一个返回值类型是String类型的方法,注意,没有参数

在struts.xml文件中配置action


struts2的流程分析
请求—–strutsPrepareAndExcuteFilter核心控制器——–Interceptors 拦截器(实现代码功能)——Action的excute—–结果页面Result
拦截器在struts—default.xml定义
执行拦截器是defaultstack中引用拦截器

——通过源代码断电调试,证明拦截器是执行

在核心拦截器的init方法对dispather进行了初始化,在dispather类中定义的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.default.properties文件
作用:定义了struts2框架中所有常量
位置: org/apache/struts2/default.properties

        2.struts-default.xml
            作用:配置了bean,interceptor,result等。
            位置:在struts的core核心jar包.

          struts-plugin.xml
            它是struts2框架中所使用的插件的配置文件。
          struts.xml              
            我们使struts2所使用的配置文件。

        3.自定义的struts.properties
            就是可以自定义常量。

        4.web.xml

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

        default.properties
        struts-default.xml
        struts.xml

原因: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指定的类。

2.关于手动配置struts.xml文件中提示操作
struts.xml提示来自于DTD文件约束
“-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
“http://struts.apache.org/dtds/struts-2.3.dtd”>
如果可以上网,自动缓存dtd,提供提示功能
如果不能上网,也可以配置本地DTD提示

导入DTD时,应该和配置的DTD版本一样

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

问题:人为设置常量,可以在哪些位置设置
1.strtus.xml应用最多

2.struts.properties
3.web.xml
配置常量

struts.action.extension
do,,

常用常量
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"/>导入其它的配置文件。

1.关于Action类的创建方式介绍:
有三种方式
1.创建一个POJO类.
简单的Java对象(Plain Old Java Objects)
指的是没有实现任何接口,没有继承任何父类(除了Object)

        优点:无耦合。
        缺点:所以工作都要自己实现。

        在struts2框架底层是通过反射来操作:
            * struts2框架 读取struts.xml 获得 完整Action类名 
            * obj = Class.forName("完整类名").newInstance();
            * Method m = Class.forName("完整类名").getMethod("execute");  m.invoke(obj); 通过反射 执行 execute方法

    2.创建一个类,实现Action接口.  com.opensymphony.xwork2.Action

        优点:耦合低。提供了五种结果视图,定义了一个行为方法。
        缺点:所以工作都要自己实现。

        public static final String SUCCESS = "success";  // 数据处理成功 (成功页面)
        public static final String NONE = "none";  // 页面不跳转  return null; 效果一样
        public static final String ERROR = "error";  // 数据处理发送错误 (错误页面)
        public static final String INPUT = "input"; // 用户输入数据有误,通常用于表单数据校验 (输入页面)
        public static final String LOGIN = "login"; // 主要权限认证 (登陆页面)

    3.创建一个类,继承自ActionSupport类.  com.opensymphony.xwork2.ActionSupport
        ActionSupport类实现了Action接口。

        优点:表单校验、错误信息设置、读取国际化信息 三个功能都支持.
        缺点:耦合度高。

    在开发中,第三种会使用的比较多.

1.通过设置method的值,来确定访问action类中的哪一个方法.

当访问的是book_add,这时就会调用BookAction类中的add方法。

当访问的是book_update,这时就会调用BookAction类中的update方法。

    2.使用通配符来简化配置
        1.在struts.xml文件中
            <action name="*_*" class="cn.itcast.action.{1}Action" method="{2}"></action>
        2.在jsp页面上
            book.jsp
                <a href="${pageContext.request.contextPath}/Book_add">book add</a><br>
                <a href="${pageContext.request.contextPath}/Book_update">book update</a><br>
                <a href="${pageContext.request.contextPath}/Book_delete">book delete</a><br>
                <a href="${pageContext.request.contextPath}/Book_search">book search</a><br>
            product.jsp
                <a href="${pageContext.request.contextPath}/Product_add">product add</a><br>
                <a href="${pageContext.request.contextPath}/Product_update">product update</a><br>
                <a href="${pageContext.request.contextPath}/Product_delete">product delete</a><br>
                <a href="${pageContext.request.contextPath}/Product_search">product search</a><br>

            当访问book add时,这时的路径是  Book_add,那么对于struts.xml文件中.
            第一个星就是   Book
            第二个星就是   add
            对于{1}Action---->BookAction
            对于method={2}--->method=add

        使用通配符来配置注意事项:
            1.必须定义一个统一的命名规范。
            2.不建议使用过多的通配符,阅读不方便。
    ----------------------------------------------
    3.动态方法调用    (了解)
        在struts.xml文件中
             <action name="book" class="cn.itcast.action.BookAction"></action>
        访问时路径: http://localhost/struts2_day01_2/book!add
            就访问到了BookAction类中的add方法。

        对于book!add 这就是动态方法调用。

        注意:struts2框架支持动态方法调用,是因为在default.properties配置文件中设置了
             动态方法调用为true.

            struts.enable.DynamicMethodInvocation = true    

这里写图片描述
这里写图片描述
这里写图片描述

Result结果类型

    <result>标签
        1.name  与action中的method的返回值匹配,进行跳转.

        2.type  作用:是用于定义跳转方式

        对于type属性它的值有以下几种:
            在struts-default.xml文件中定义了type可以取的值

             <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />

        必会: chain  dispatcher  redirect redirectAction  stream

            dispatcher:它代表的是请求转发,也是默认值。它一般用于从action跳转到页面。
            chain:它也相当于请求转发。它一般情况下用于从一个action跳转到另一个action。

            redirect:它代表的是重定向  它一般用于从action跳转到页面
            redirectAction: 它代表的是重定向  它一般用于从action跳转另一个action。

            stream:代表的是服务器端返回的是一个流,一般用于下载。

        了解: freemarker  velocity

    ----------------------------------------------------
    局部结果页面与全局结果页面
         局部结果页面 和 全局结果页面 
        <action name="result" class="cn.itcast.struts2.demo6.ResultAction">
                    <!-- 局部结果  当前Action使用 -->
                    <result name="success">/demo6/result.jsp</result> 
        </action>

        <global-results>
                    <!-- 全局结果 当前包中 所有Action都可以用-->
                    <result name="success">/demo6/result.jsp</result>
        </global-results>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值