当前位置: 代码迷 >> Web前端 >> Web应用和Struts1的报错异常页面机制
  详细解决方案

Web应用和Struts1的报错异常页面机制

热度:107   发布时间:2012-10-30 16:13:36.0
Web应用和Struts1的报错错误页面机制

决心好好把Web页面的报错机制搞清楚

?

1. web应用的错误页面

?? ?web.xml中可以设置error-page, 其中这个标记可以有这样的子标签

?? ? ? ??error-code,exception-type:HTTP错误码,或者异常信息的类型

?? ? ? ? ? ? ? ? ? ? ? ? ? ?exception-type指定的必须是一个Throwable,并且这个类在容器的类路径下能够被找到

?? ? ? ? location:错误页面的位置?? ? ? ??

?? ? ? ??<!ELEMENT error-page ((error-code | exception-type), location)>

?

?? ? ? ??<!ELEMENT exception-type (#PCDATA)>

?? ? ? ??<!ELEMENT error-code (#PCDATA)>

?? ?那么在错误页面我们可以拿到什么信息呢?

?? ? ? ? ?首先,在这个页面,我们可以知道出现了什么异常,这个有error-page的配置决定

?

2. 在JSP页面指定错误页面

?? ?exception-type指定的必须是一个Throwable,并且这个类在容器的类路径下能够被找到

?? ? 容器会为错误页面提供一个exception对象,不过要想在错误页面里访问这个对象,

?? ? 必须在错误页面里指定isErrorPage属性:?

?? ? error.jsp?

?? ? <%@ page isErrorPage="true" %>???
?? ? 然后,在scriptlet中就可以使用隐式对象exception,在EL中可以使用EL隐式对象${pageContext.exception}。

?

3.stuts中设置错误页面(版本1.2.4)

?? 在struts-config.xml的global-exceptions标签中可以设置

?

?? ?<exception?key="all.excption"?path="/jsp/error/errorAll.jsp"?type="java.lang.Exception"/>

?

?

?? dtd

?? ? ??<!ELEMENT global-exceptions (exception*)>

?? ? ? <!ATTLIST global-exceptions id ? ? ? ? ?ID ? ? ? ? ? ? ?#IMPLIED>

?

?? ? ? <!ELEMENT exception (icon?, display-name?, description?, set-property*)>

?? ? ? <!ATTLIST exception ? ? ?id ? ? ? ? ? ? ID ? ? ? ? ? ? ?#IMPLIED>

?? ? ? <!ATTLIST exception ? ? ?bundle ? ? ? ? %AttributeName; #IMPLIED>

?? ? ??<!ATTLIST exception ? ? ?className ? ? ?%ClassName; ? ? #IMPLIED>

?? ? ??<!ATTLIST exception ? ? ?handler ? ? ? ?%ClassName; ? ? #IMPLIED>

?? ? ??<!ATTLIST exception ? ? ?key ? ? ? ? ? ?CDATA ? ? ? ? ? #REQUIRED>

?? ? ??<!ATTLIST exception ? ? ?path ? ? ? ? ? %RequestPath; ? #IMPLIED>

?? ? ??<!ATTLIST exception ? ? ?scope ? ? ? ? ?CDATA ? ? ? ? ? #IMPLIED>

?? ? ??<!ATTLIST exception ? ? ?type ? ? ? ? ? %ClassName; ? ? #REQUIRED>

?

?

?

?? ?bundle ? ? ? ? ? Servlet context attribute for the message resources bundle

?? ? ? ? ? ? ? ? ? ? associated with this handler. The default attribute is the

?? ? ? ? ? ? ? ? ? ? value specified by the string constant declared at

?? ? ? ? ? ? ? ? ? ? Globals.MESSAGES_KEY.

?? ? ? ? ? ? ? ? ? ? [org.apache.struts.Globals.MESSAGES_KEY]

?

?? ?className ? ? ? ?The configuration bean for this ExceptionHandler object.

?? ? ? ? ? ? ? ? ? ? If specified, className must be a subclass of the default

?? ? ? ? ? ? ? ? ? ? configuration bean

?? ? ? ? ? ? ? ? ? ? ["org.apache.struts.config.ExceptionConfig"]

?

?? ?handler ? ? ? ? ?Fully qualified Java class name for this exception handler.

?? ? ? ? ? ? ? ? ? ? ["org.apache.struts.action.ExceptionHandler"]

?

?? ?key ? ? ? ? ? ? ?The key to use with this handler's message resource bundle

?? ? ? ? ? ? ? ? ? ? that will retrieve the error message template for this

?? ? ? ? ? ? ? ? ? ? exception.

?

?? ?path ? ? ? ? ? ? The module-relative URI to the resource that will complete

?? ? ? ? ? ? ? ? ? ? the request/response if this exception occurs.

?

?? ?scope ? ? ? ? ? ?The context ("request" or "session") that is used to access

?? ? ? ? ? ? ? ? ? ? the ActionError object [org.apache.struts.action.ActionError]

?? ? ? ? ? ? ? ? ? ? for this exception.

?

?? ?type ? ? ? ? ? ? Fully qualified Java class name of the exception type to

?? ? ? ? ? ? ? ? ? ? register with this handler.

?

?? ?这里注意几点

?? ?1.机制

?? ? ? 映射异常和错误页面,struts找到异常对应的配置后,按照配置,根据key找到resource_bundle,

?? ? ? 然后转到错误页面,错误页面应该使用<html:errors/>标记将错误信息显示出来

?? ?2.key是用于找到resource bundle对应信息的关键字,由于这里规定死了,所以要显示一种错误信息就要配置一个

?? ?3.resource bundle的配置

?? ? ? <message-resources parameter="resources.MessageResources"/>

?? ? ? 这里,bundle文件应放置在WEB-INF/classes/resource/目录,

?? ? ? 并且名称类似于MessageResources_zh_CN.properties?

?? ? 4.在action中也可以设置exception

?

参考

1.隔叶黄莺 《JSP的errorPage 指令异常转向错误页的实现机制及应用》??http://www.blogjava.net/Unmi/archive/2008/05/17/200963.html

2.孙卫琴 《精通Struts:基于MVC的Java Web设计与开发》

?

?

?

?

  相关解决方案