[英]Exception and error handling configuration through web.xml
I am using Apache MyFaces and need to handle ViewExpiredException differently than other internal server errors.But I find that if I include error-code, 500; 我正在使用Apache MyFaces,并且需要以不同于其他内部服务器错误的方式处理ViewExpiredException。但是我发现,如果我包含错误代码,则为500; then in ViewExpiredException error also it takes the path of error-code.
然后在ViewExpiredException错误中也采用错误代码的路径。
Below is web.xml configuration: 以下是web.xml配置:
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/login.xhtml?faces-redirect=true</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.xhtml</location>
</error-page>
How to ensure that i can redirect to different URL for the above two case? 在上述两种情况下,如何确保我可以重定向到其他URL? Referred multiple error-code configuration web.xml and I am fine to replace the location with a single servlet;
引用了多个错误代码配置web.xml ,我可以用单个servlet替换该位置; but how to capture the error in servlet?
但是如何捕获servlet中的错误?
对于ViewExpiredException,为什么不尝试通过CustomExceptionHandler处理异常,并通过handle()方法将用户重定向到所需的任何Page
I had 2 issues here: 我在这里有2个问题:
a. 一种。 Identify javax.faces.application.ViewExpiredException and redirect to /login.xhtml?faces-redirect=true.
确定javax.faces.application.ViewExpiredException并重定向到/login.xhtml?faces-redirect=true。 Without faces-redirect, redirecting to XHTML page will throw error
没有faces-redirect,重定向到XHTML页面将引发错误
b. b。 Ensure that other error with status code 500 is redirected to appropriate page.
确保将状态代码为500的其他错误重定向到适当的页面。
These are resolved by: 这些通过以下方式解决:
1> Created a servlet which checks for HTTP code, and then checks if the error is due to ViewExpiredException or not. 1>创建了一个Servlet ,该Servlet检查HTTP代码,然后检查错误是否归因于ViewExpiredException。 Based upon the error condition, the servlet forwards the request to specific URL.
根据错误情况,Servlet将请求转发到特定的URL。
if (request.getAttribute("javax.servlet.error.status_code") == 500) { excep = (Class<? extends Exception>) request .getAttribute("javax.servlet.error.exception_type"); if (excep != null) { if (excep.getCanonicalName().equalsIgnoreCase( "javax.faces.application.ViewExpiredException")) { //you can forward to another page like /login.xhtml?faces-redirect=true } else { //you can forward to different page with error message } } }
2> Change in web.xml to forward all 500 exception to this servlet 2>更改web.xml以将所有500个异常转发到此servlet
<error-page> <error-code>500</error-code> <location>/error</location><!-- here error is name of servlet --> </error-page>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.