Struts.xml配置详解

本文介绍了Struts2框架的配置文件结构与关键元素,包括常量配置、包配置、默认操作引用、全局结果配置及异常映射等内容。

<?xml version="1.0" encoding="UTF-8"?>         文档为xml格式 字符编码为UTF-8
<!DOCTYPE struts PUBLIC           (DOCTYPE是文档类型标签)根节点struts
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    公共标识符  "注册//组织//类型 标签//定义 语言"
"http://struts.apache.org/dtds/struts-2.3.dtd">   文档类型定义位置
<struts>   根节点
    <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>  配置属性 详情见constant节点介绍
    <constant name="struts.devMode" value="true"></constant> 
    <package name="default"  extends="struts-default">  name属性为每个package的唯一标识,extends可以指定本package继承另外一个package的所有配置
        <default-action-ref name="index"></default-action-ref> 当没有对应的action时,默认执行的action (name属性的值)
        <global-results> 当没有对应的result 默认的result
            <result name="error">/error.jsp</result>
        </global-results> 
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"></exception-mapping>
        </global-exception-mappings>

global-exception-mappings用于异常处理的标签可以配置在Action中,也可以配置在<global-exception-mappings>,顾名思义就是全局异常,当然执行Action的时候发生异常时,如果在Action中没有捕获异常而是抛出异常的话,struts2会首先在正在执行的Action中查找<exception-mapping>,寻找对应的Exception进行处理,如果找不到,才会去<global-exception-mappings>去寻找对应的Exception处理,如果还是找不到的话,就只好抛出异常了。上面是捕获java.lang.Exception异常 返回error result
        <action name="Login" class="action.LoginAction" method="login">  定义action 以及执行的类和方法


            <result>/loginResult.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>
</struts>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值