在struts中配置文件中配置异常的跳转路径

本文详细解析了Struts框架的配置文件struts-config.xml,包括全局异常处理、全局转发、动作映射及资源文件配置。并通过示例展示了如何在HTML文件中使用Struts标签库显示错误信息。

 下面是struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <global-exceptions>
        <exception key="prompt.errors" type="java.lang.NullPointerException" path="/errors.jsp"></exception>
            <!--配置异常的类型,path是跳转页面,页面显示什么消息,是以Key 从资源文件中检索-->
    </global-exceptions>
    
    <global-forwards>
    <forward name="message" path="/message.jsp"></forward><!--配置一个全局的forward-->
</global-forwards>

    <action-mappings>
    
        <action path="/register" 
                name="UserFormBean" 
                scope="request"
                type="cn.itcast.web.action.RegiserAction"
                input="/1.jsp"
                validate="true">
                
                
        </action>
        
        <action path="/xxx" type="cn.itcast.web.action.DemoAction"></action>
    </action-mappings>
       
    <message-resources parameter="MessageResource"></message-resources>
 </struts-config>

下面是配置文件:

prompt.birthday=\u65E5\u751F
errors.twofields=The '{0}' field must have the same value as the '{1}' field.
prompt.errors=\u4F60\u51FA\u4E86\u7A7A\u6307\u9488\u9519

下面是html文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'errors.jsp' starting page</title>
  </head>
  
  <body>
       <html:errors/>
  </body>
</html>

当然可以在action中配置一个局部的异常,配置方式同上面的全局异常,上面的xml文件还给出了配置全局的forward的方式

转载于:https://my.oschina.net/cys21/blog/494184

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值