idea+maven+Struts2 之struts.xml中标签介绍

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

<struts>
    <!--修改struts action访问时的后缀,默认为.action;可以省略;多个可以使用英文逗号隔开-->
    <constant name="struts.action.extension" value="action,do,html"></constant>

    <!--include节点是Struts2中组件化的方式,可以将每个功能独立到一个xml配置文件中,
   然后通过include节点引用-->
    <include file="/struts_index.xml"></include>

     <!--package提供了将多个Action组织为一个模块的方式
         package的名字必须是唯一的package可以扩展 当一个package扩展自另一个
         package的时候package会在本身配置的基础上加入扩展的package的配置父package
         必须在子package前配置
         name:package名称
         extends:继承的父package名称
         abstract:设置package的属性为抽象的package不能定义action
         namespace:定义package命名空间 该命名空间影响url的地址,例如:
         namespace="/test" 那么访问路径为http://localhost:8080/ssh/test/login
         其中后缀.action可以省略
         -->
    <package name="null" extends="struts-default" namespace="/test">
        <interceptors>
            <!--定义拦截器
                name:拦截器名称
                class:拦截器类路径
                -->
            <interceptor name="timer" class="com.zxr.timer"></interceptor>
            <interceptor name="logger" class="com.zxr.Logger"></interceptor>
            <!--定义拦截器栈-->
            <interceptor-stack name="mystack">
                <interceptor-ref name="timer"></interceptor-ref>
                <interceptor-ref name="logger"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <!--定义默认的拦截器每个Action都会自动引用,
        如果Action中引用了其它拦截器 默认拦截器将无效-->
        <default-action-ref name="login"></default-action-ref>

        <!--全局results配置-->
        <global-results>
            <result name="input">/error.jsp</result>
        </global-results>

        <!--
        Action配置一个Action可以被多次映射(只要action配置中的name不同)
        name:action名称
        class:对应的类的路径
        method:调用Action中的方法
        -->
        <action name="login" class="com.zxr.action.LoginAction" method="success">
            <!--引用拦截器
            name:拦截器名称或拦截器栈名称
            <interceptor-ref name="timer"></interceptor-ref>
            -->

          <!--  节点配置
            name:result名称 和Action中返回的值相同
            type:result类型 不写则选择superpackage的type struts-default.xml中默认的type
            -->
            <result name="success">/jsp/success.jsp</result>

            <!--参数设置
            name:对应Action中的get/set方法
            -->
            <param name="url">http://www.sina.com</param>

        </action>

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

        <!--配置默认的action-->
        <default-action-ref name="index"></default-action-ref>
        <action name="index">
            <result>/jsp/error/error.jsp</result>
        </action>


        <!--没有指定action中的方法,调用action中的默认方法execute-->
        <action name="demoAction" class="com.zxr.action.IndexAction">
            <result name="success">/jsp/index.jsp</result>
        </action>
        <!--指定方式调用action中的方法通过 method="对应的方法" 指定所要调用的方法-->
        <action name="addAction" class="com.zxr.action.IndexAction" method="add">
            <result name="add">/jsp/add.jsp</result>
        </action>
        <!--使用!调用action中的方法 http://localhost:8080/gthAction!add.action
          struts2 2.5版本必须配置 <allowed-methods>add,edit</allowed-methods> 否则无法通过匹配符访问
          【一般不推荐使用此方法】
        -->
        <action name="gthAction" class="com.zxr.action.IndexAction">
            <result name="add">/jsp/add.jsp</result>
            <result name="edit">/jsp/edit.jsp</result>
            <result name="success">/jsp/success.jsp</result>
            <allowed-methods>add,edit</allowed-methods>
        </action>
        <!--使用通配符调用action中的方法 【提倡使用】
            *表示匹配的参数
            {1}表示匹配第一个参数
            struts2 2.5版本必须配置 <allowed-methods>add,edit</allowed-methods> 否则无法通过匹配符访问
        -->
        <action name="tpfAction_*" class="com.zxr.action.IndexAction" method="{1}">
            <result name="add">/jsp/{1}.jsp</result>
            <result name="edit">/jsp/{1}.jsp</result>
            <result name="success">/jsp/index.jsp</result>
            <allowed-methods>add,edit</allowed-methods>
        </action>
         <!--通过全局通配符的方式动态的调用action和action中的方法 【提倡使用】
          第一个匹配符必须和action中类名相同
          http://localhost:8080/Index_add    Index为IndexAction的Index ;add为IndexAction中的方法
         -->
        <action name="*_*" class="com.zxr.action.{1}Action" method="{2}">
            <result name="add">/jsp/{2}.jsp</result>
            <result name="edit">/jsp/{2}.jsp</result>
            <result name="success">/jsp/index.jsp</result>
            <allowed-methods>add,edit</allowed-methods>
        </action>

    </package>

    <!--使用!调用方法时必须添加此配置
     访问方式:
    -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>


</struts>

 

转载于:https://www.cnblogs.com/binkai/p/9975104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值