简体   繁体   English

覆盖JSP中的errorPage属性

[英]Overwriting the errorPage property in JSP

I've defined <%@ page errorPage="/error.jsp" %> in the header that all JSP files include, to catch any unhandled exceptions and redirect to that error page instead of printing them. 我已经在所有JSP文件包含的标头中定义了<%@ page errorPage="/error.jsp" %> ,以捕获任何未处理的异常并重定向到该错误页面,而不是打印它们。 This works fine with one caveat - if error.jsp itself throws an exception, it will continuously redirect to itself in an infinite loop. 需要注意的是,这很好用-如果error.jsp本身抛出异常,它将在无限循环中不断重定向到自身。 I want to erase the errorPage value for just error.jsp so that it'll just print the exception as normal. 我只想删除error.jsp的errorPage值,以便它像往常一样打印异常。 I tried just redefining the errorPage property to be blank but I get the following error: 我尝试只是将errorPage属性重新定义为空白,但出现以下错误:

Page directive: illegal to have multiple occurrences of errorPage with different values (old: /error.jsp, new: ) Page指令:非法出现具有不同值的errorPage多次出现(旧:/error.jsp,新:)

Is there any way for me to overwrite that property? 我有什么办法可以覆盖该属性? Or any other suggestions on how to prevent this issue? 或关于如何预防此问题的其他建议?

It is indeed illegal to have multiple page declarations with the same attribute. 具有相同属性的多个page声明确实是非法的。 Your choices are: 您的选择是:

  1. Not include your header into your error page. 不要在错误页面中包含标题。
  2. Ensure that your error page doesn't throw any exception on its own. 确保您的错误页面不会自己引发任何异常。 It should really be rather simple and straightforward - error page is no place for business logic. 它实际上应该非常简单明了-错误页面在业务逻辑中不存在。 If you want to do something complicated there, consider redirecting to another page instead. 如果您想在那里做一些复杂的事情,可以考虑重定向到另一个页面。

Why don't you just have a different include header for the error page which do not include it himself?!! 您为什么不对错误页面使用另一个头文件却本身不包含它呢?

So, instead of having: 因此,与其具有:

header.jsp
==========
a
b
c
errorPage=error.jsp

You could have: 你可以有:

commonHeader.jsp
===========
a
b
c

Without the errorPage directive 没有errorPage指令

And modify the header to include the new one. 并修改标题以包括新的标题。

header.jsp
===========
include=commonHeader.jsp
errorPage=error.jsp

That way you don't need to change anything in the rest of your jsp's 这样一来,您无需在其余的jsp文件中进行任何更改

You just need to change your errorPage from: 您只需要从以下位置更改errorPage:

 include="header.jsp"

to

 include="commonHeader.jsp"

And the errorPage won't have an error page anymore .... 而且errorPage不会再有错误页面....

最后,我只是通过在页面周围用<c:catch>标记来避免该重定向,并打印一条准系统消息(例外情况)以确保它不会中断,来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM