简体   繁体   English

通过web.xml进行异常和错误处理配置

[英]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.

相关问题 web.xml中的JSF异常处理 - Exception handling with JSF in web.xml 针对JSF应用程序的web.xml中的错误页面配置 - Error Page configuration in web.xml for JSF application 来自web.xml的JSFViewExpiredException处理不起作用 - JSFViewExpiredException handling from web.xml is not working JSF a4j +战斧+ myfaces错误:资源框架未初始化,请检查web.xml进行过滤器配置 - Error with JSF a4j + tomahawk + myfaces: Resources framework is not initialised, check web.xml for Filter configuration 使用JBoss AS7上的web.xml正常处理NullPointerException - Handling NullPointerException gracefully with web.xml on JBoss AS7 无法通过web.xml呈现JSF <welcome-file> 只要 - JSF not rendered through web.xml <welcome-file> only 正在处理的页面仅在错误/异常中途留空,而不是转发到web.xml中指定的错误页面 - Pages being processed are merely left blank half way through on errors/exceptions instead of forwarding to the error page specified in web.xml 使用“HttpOnly”和“Secure”属性时出现 web.xml 错误 - web.xml error when using 'HttpOnly' and 'Secure' attributes 使用web.xml和pretty-faces重定向到自定义错误页面 - Redirecting to custom error page with web.xml and pretty-faces web.xml错误页面不起作用,为什么? - web.xml error-pages are not working, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM