Struts2 配置xml文件

一、配置web.xml文件

在web.xml中配置一个struts的过滤器
2.5版本:

    <filter>
        <filter-name>struts-prepare</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
    </filter>

    <filter>
        <filter-name>struts-execute</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts-prepare</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>struts-execute</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2.3版本:

    <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>

二、配置struts.xml文件

1.文件声明
2.5版本:

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
            "http://struts.apache.org/dtds/struts-2.5.dtd">

2.3版本:

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
            "http://struts.apache.org/dtds/struts-2.3.dtd">

2.正文:

相关知识点:

(1) struts.xml文件中所有的元素的定义都是以“<struts>”开始,“</struts>”结束。<struts>主要的子元素有:

1) 设置常量:

<constant name="常量名" value="常量值"/>

2) package:
struts.xml文件中package元素的各大属性讲解
action等package子元素的配置简介
3) 导入子文件

<include file="子文件名" />
    <struts>

        <!--<constant />用于配置常量,属性name和value分别对应常量的名和值 -->

        <!-- 设置匹配时Action默认的后缀,默认值为“action”,举例:
            若未设置该常量的值,请求URL为http://localhost:8080/ssh001/one/Index
            则默认访问名为Index.action的action,
            同http://localhost:8080/ssh001/one/Index.action;
            Filter接受请求后,采用默认值去匹配时,将以action的name属性值.action
            的形式去匹配URL的Index.action;若设置常量值为“do”,则请求URL为
            http://localhost:8080/ssh001/one/Index时将报404错误,找不到/ssh001/one/Index
            因为此时没有默认的后缀,必须http://localhost:8080/ssh001/one/Index.do
            此时Filter将以action的name属性值.do的形式去匹配URL的Index.do   -->
    <constant name="struts.action.extension" value="action"/>

        <!-- 使用"!"进行动态方法调用,必须设置该常量 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

            <!--package的name属性必需设置,若不设置namespace属性,则该包使用默认
            的命名空间--“ ”,查找Action时,系统会先在设置的命名空间里进行匹配,
            若匹配不成功,则再到默认空间里进行匹配
                如下例:进行请求的URL格式应为 根目录/one/Action名,若jsp是位于根目录
                下,则<result>应为../error.jsp -->

    <package name="one" extends="struts-default" namespace="/one">

            <!-- 全局导航页面映射定义,这里定义的<result>被多个Action共用,
                如果一个具体Action在<action>里找不到对应的<result>,则会去
                <global-result>里寻找是否有对应的<result>
            -->
        <global-results>
            <result name="error">error.jsp</result>
        </global-results>
            <!--全局异常页面定义,根据其result属性的值,在<global-result>中进行匹配 -->
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error" />
        </global-exception-mappings>

            <!--设置默认的action,属性name的值为要指定的Action的name值,
                当用户请求的URL在容器中找不到对应的Action时,系统将使用默认的
                Action来处理用户请求-->
        <default-action-ref name="Index" />

            <!--根据返回结果,决定将要跳转到的页面-->
        <action name="Index" class="action.IndexAction">
                <!--若不设置result的name属性,则默认为“success”,可不设置属性type,
                    这时将使用默认值dispatcher(用于jsp整合的结果类型) -->
            <result>success.jsp</result>
            <result name="error">error.jsp</result>
        </action>

            <!--直接跳转到指定页面-->
        <action name="Index">
            <result>login.jsp</result>
        </action>
            <!--定义一个通用Action -->
        <action name="*">
            <result>/{1}.jsp</result>
        </action>

            <!-- 跳转至其它action-->
        <action name="redirectAction" class="action.RedirectAction">
            <result>../WEB-INF/redirect.jsp</result>
        </action>
            <!--跳转至redirectAction并给属性tip赋值,调用其redirect()方法 -->
        <action name="redirectTest">
            <result type="redirect">redirectAction!redirect?tip=redirect</result>
        </action>

    </package>
        <!-- 从其他地方导入一个xml的子文件-->
    <include file="struts2-login.xml"/>
</struts>

注:

1.使用通配符“*”时,可能同时匹配到多个Action,这时将由第一个匹配成功的Action来处理用户请求
2.通配符可用于<result.../>元素,但当<result name="*">/{1}.jsp</result>时
    表达式{1}无法得到result的name属性的值,举例:
    <action name="*" class="IndexAction" method="{1}">
        <result name="*">/{1}.jsp</result>
    </action>
    两个表达式{1}获得的值都是action的name属性值
3.同一个命名空间里不能有同名的Action,所有在同一个包里,不能定义多个name属性值相同的Action,
  即使定义了多个,前面的也会被后面的所覆盖
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值