struts2框架(一) 初探

Struts2 是一个用来开发 MVC应用程序的框架. 它提供了 Web 应用程序开发过程中的一些常见问题的解决方案:
Struts2 = Struts1 + WebWork 1和2没有本质的关系

1.Struts2开发流程

1.1 引入jar文件

commons-fileupload-1.2.2.jar 【文件上传相关包】
commons-io-2.0.1.jar 【io操作相关的包】
struts2-core-2.3.4.1.jar 【struts2核心功能包】
xwork-core-2.3.4.1.jar 【Xwork核心包】
ognl-3.0.5.jar 【Ognl表达式功能支持表】
commons-lang3-3.1.jar 【struts对java.lang包的扩展】
freemarker-2.3.19.jar 【struts的标签模板库jar文件】
javassist-3.11.0.GA.jar 【struts对字节码的处理相关jar】

1.2 配置web.xml

tomcat服务器在启动时,首先会加载自身的web.xml,然后加载所有项目的web.xml。struts2通过在web.xml中引入过滤器来作为入口,在学习过滤器的时候我们知道过滤器的init方法会在tomcat服务器启动时调用。

   <!-- 引入struts核心过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

1.3 编写Action类

action类,也叫做动作类; 一般继承ActionSupport类,即处理请求的类 (struts中的action类取代之前的servlet).
注意: action中的业务方法不能有参数,且必须返回String

import com.opensymphony.xwork2.ActionSupport;

// 开发action: 处理请求
public class HelloAction extends ActionSupport {    
    // 处理请求
    public String execute() throws Exception {
        System.out.println("访问到了action,正在处理请求");
        return "success";
    }
}

1.4 配置struts.xml

该配置文件的头部可以参照struts2-core-2.3.4.1.jar包下面的struts-default.xml来写。该配置文件的主要作用是将提供action的访问名称,并且和action类进行关联。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="xxxx" extends="struts-default">
        <action name="hello" class="cn.acamy.action.HelloAction" method="execute">
            <result name="success">/success.jsp</result>
        </action>
    </package> 
</struts>

2.Struts2执行流程

2.1 服务器启动

  1. 加载项目web.xml
  2. 创建Struts核心过滤器对象, 执行filter的init()
struts-default.xml,    核心功能的初始化
struts-plugin.xml,     struts相关插件
struts.xml             用户编写的配置文件

2.2 用户访问

3.用户访问Action, 服务器根据访问路径名称,找对应的aciton配置, 创建action对象
4.执行默认拦截器栈中定义的18个拦截器
5.执行action的业务处理方法
6.根据action返回的Result来跳转到相应的页面

3. 三大配置文件的加载

从上面可以知道核心过滤器是Struts的入口,tomcat服务器在启动时会执行过滤器的init()方法,现在通过源码展示三大配置文件是如何加载的。

3.1 首先是执行web.xml引入的StrutsPrepareAndExecuteFilter初始化方法

  public void init(FilterConfig filterConfig) throws ServletException {
        InitOperations init = new InitOperations();
        try {
            FilterHostConfig config = new FilterHostConfig(filterConfig);
            init.initLogging(config);
            Dispatcher dispatcher = init.initDispatcher(config);// 在这里跳转
            init.initStaticContentLoader(config, dispatcher);

            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
            this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);

            postInit(dispatcher, filterConfig);
        } finally {
            init.cleanup();
        }

    }

3.2 然后执行InitOperations的initDispatcher方法

public Dispatcher initDispatcher( HostConfig filterConfig ) {
        Dispatcher dispatcher = createDispatcher(filterConfig);
        dispatcher.init();
        return dispatcher;
    }

3.3 最后执行Dispatcher的init方法


/**
     * Load configurations, including both XML and zero-configuration strategies,
     * and update optional settings, including whether to reload configurations and resource files.
     */
    public void init() {

        if (configurationManager == null) {
            configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
        }

        try {
            init_DefaultProperties(); // [1]
            init_TraditionalXmlConfigurations(); // [2]跳转到三大配置文件的相关方法
            init_LegacyStrutsProperties(); // [3]
            init_CustomConfigurationProviders(); // [5]
            init_FilterInitParameters() ; // [6]
            init_AliasStandardObjects() ; // [7]

            Container container = init_PreloadConfiguration();
            container.inject(this);
            init_CheckConfigurationReloading(container);
            init_CheckWebLogicWorkaround(container);

            if (!dispatcherListeners.isEmpty()) {
                for (DispatcherListener l : dispatcherListeners) {
                    l.dispatcherInitialized(this);
                }
            }
        } catch (Exception ex) {
            if (LOG.isErrorEnabled())
                LOG.error("Dispatcher initialization failed", ex);
            throw new StrutsException(ex);
        }
    }

//三大配置文件
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";

private void init_TraditionalXmlConfigurations() {
        String configPaths = initParams.get("config");
        if (configPaths == null) {
            configPaths = DEFAULT_CONFIGURATION_PATHS;
        }
        String[] files = configPaths.split("\\s*[,]\\s*");
       //骤个初始化
        for (String file : files) {
            if (file.endsWith(".xml")) {
                if ("xwork.xml".equals(file)) {
                    configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
                } else {
                    configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
                }
            } else {
                throw new IllegalArgumentException("Invalid configuration file name");
            }
        }
    }

4.struts-default.xml, 详解

位置:struts2-core-2.3.4.1.jar/ struts-default.xml
内容:

1. bean节点指定struts在运行的时候创建的对象类型
2.指定struts-default包  【用户写的package(struts.xml)一样要继承此包 】
    package  struts-default  包中定义了:
        a.  跳转的结果类型
            dispatcher    转发,不指定默认为转发
            redirect       重定向
            redirectAction  重定向到action资源
            stream        (文件下载的时候用)
        b. 定义了所有的拦截器
              定义了32个拦截器!
              为了拦截器引用方便,可以通过定义栈的方式引用拦截器,
            此时如果引用了栈,栈中的拦截器都会被引用!
            
            defaultStack
                默认的栈,其中定义默认要执行的18个拦截器!


        c. 默认执行的拦截器栈、默认执行的action
            <default-interceptor-ref name="defaultStack"/>
           <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
<interceptor name="prepare" 
class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
<interceptor name="params" 
class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

注意:拦截器功能与过滤器功能类似,都拦截资源,但它是struts的概念,只能在struts中用,只拦截action请求;而过滤器是servlet的概念,可以在struts项目、servlet项目用,可以拦截所有的web资源(/index.jsp/servlet/action/img/css/js)。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容