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

被折叠的 条评论
为什么被折叠?



