繁体   English   中英

使用web.xml的Java + Vaadin + tomcat处理异常

[英]Java+Vaadin+tomcat Handling Exception using web.xml

需求:

每当我们在服务器控制台中收到任何异常(即,NullPointer,IndexOutofBoundsException等)时,我都想显示一个错误页面。

系统要求:

我们正在使用apache tomcat 7.0.61,maven 3.2.3,Spring和Vaadin 7(用于UI框架)。

我们的WEB.XML文件:

<context-param>
..............
</context-param>

<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
 .....
<listener>
<listener-class>
    org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>Vaadin Application Servlet</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
<init-param>
    <description>Vaadin UI to display</description>
    <param-name>UI</param-name>
    <param-value>com.lone.ui.LoneUI</param-value>
</init-param>
<init-param>
    <description>Application widgetset</description>
    <param-name>widgetset</param-name>
    <param-value>com.lone.ui.AppWidgetSet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

所以我浏览并尝试了一些方案:

场景1)

首先我已经配置

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error</location>
</error-page>
(or)
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error</location>
</error-page>

在web.xml文件中,然后我通过在方法级别使用@RequestMapping(“ / error”)编写了Controller类,但是这种情况不起作用。

场景2)

与上面的web.xml文件相同,我将Servlet类编写为

@WebServlet(value = {"/error"},asyncSupported = true)
public class ExceptionHandling extends VaadinServlet{
@Override
protected VaadinServletService getService() {
    System.out.println("First if i display the sout message then i can  Navigate to the Error Page....");
    return super.getService();
}
}

但这也不起作用。

场景3)

与上述web.xml文件相同,我通过扩展HTTPServlet编写了Servlet类,但仍无法显示sout消息。

场景4)我尝试使用Spring @ControllerAdvice和@Exception处理程序注释仍然无法显示错误页面

主要要求:

我需要在Web.xml文件中配置例外,以便当我们在服务器控制台中获得任何例外时,它应该命中web.xml文件并应导航至错误页面。

您能否建议我完成要求,因为我从过去2天开始就一直在处理此要求。

提前致谢。

Vaadin 7允许您创建额外的错误处理程序。 因此,如果您在UI上遇到任何错误/异常,则Vaadin会调用ErrorHandler。 如果您的想法在出现任何错误的情况下重定向到错误页面,则可以执行以下操作:

        UI.getCurrent().setErrorHandler(new ErrorHandler() {

        @Override
        public void error(com.vaadin.server.ErrorEvent event) {
            Page.getCurrent().setLocation("error");
            // Do the default error handling (optional)
            DefaultErrorHandler.doDefault(event);
        }

    });

您也可以使用任何其他错误处理程序方法来扩展它。

暂无
暂无

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

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