zoukankan      html  css  js  c++  java
  • Struts2学习笔记(一) Struts2配置文件的配置

    1、配置web.xml文件。

    在Struts2中,struts框架式通过Filter启动的。Filter在web.xml中的配置如下:

     <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <!-- 让Struts2的核心Filter拦截所有请求 -->
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。

    注意:struts2督导struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件。


    2、配置struts.xml文件。

    <struts>
        <constant name="struts.devMode" value="true" />
        <package name="wbb" namespace="/" extends="struts-default">
            <action name="login" class="ruixin.net.wbb.LoginAction" method="execute">
                <result name="input">/login.jsp</result>
                <result name="success">/page/success.jsp</result>
                <result name="error">/error.jsp</result>
            </action>
        </package>
    </struts>

    struts2中用包package来管理Action,其中name属性相当于id是唯一的,方便其他包引用。namespace属性可以减少重复的代码。


    3、Action名称的搜索顺序

    1)获得请求路径的URI,例如url是:http//localhost:8080/struts2/path1/path2/path3/test.action

    2)首先寻找namespace为path1/path2/path3的package,如果不存在这个package则执行步骤三;如果存在这个package,则在这个package中寻找名字为test的action,挡在该package下寻找不到action时,就会直接跑到默认namespace的package里面去寻找action(默认的命名空间为空字符串“”),如果在默认namespace的package里面还寻找不到改action,页面提示找不到action;

    3)寻找namespace为/path1/path2的package,如果不存在这个package,则转至步骤4..............

    4)寻找namespace为/path1的package,如果不存在这个package,则执行步骤五......

    5)寻找namespace为/的package,..........


    4、Action配置中的各项默认值

    1)如果没有为action指定class,默认是ActionSupport。

    2)如果没有为action指定method,默认执行action中的execute()方法。

    3)如果没有指定result的属性,默认值为success。







  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3318144.html
Copyright © 2011-2022 走看看