如何防止 Spring MVC 3.0 应用程序中的 JSP 上出现原始异常信息?

发布于 2024-10-30 20:06:40 字数 423 浏览 2 评论 0原文

在测试我的 Spring 应用程序时,我上传的文件超出了应用程序中配置的最大允许大小。

因此,JSP 上出现了用户可见的以下消息:

Error
Exception: org.springframework.web.multipart.MultipartException: 
something went wrong here; nested exception is 
org.apache.commons.fileupload.FileUploadBase$FileUploadIOException

我需要更改什么才能在 JSP 上为用户呈现更加用户友好的消息?

例如:

Uploaded file exceeds maximum allowed size of 25MB.

While testing my Spring app, I uploaded a file that exceeded the maximum allowed size configured in the app.

Consequently, the following message appeared on the JSP, visible to the user:

Error
Exception: org.springframework.web.multipart.MultipartException: 
something went wrong here; nested exception is 
org.apache.commons.fileupload.FileUploadBase$FileUploadIOException

What would I need to change to present a more user-friendly message on the JSP for the user?

For example:

Uploaded file exceeds maximum allowed size of 25MB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-11-06 20:06:40

我以前没有做过Spring MVC,但我认为Spring可以做到

使用消息资源(ResourceBundle)。您必须捕获异常并创建一个错误页面,在其中可以显示错误消息。

Spring 使用 ResourceBundleMessageSource 作为消息资源。这个示例(但是太简单了)向您展示了如何配置一个并使用它。

I have not done Spring MVC before but I think Spring can do it.

Use Message Resources (ResourceBundle). You will have to catch the exception and create a error page where the message of the error can be displayed.

Spring uses ResourceBundleMessageSource for message resources. This example (however too simple) shows you how to configure one and use it.

夜夜流光相皎洁 2024-11-06 20:06:40

使用 HandlerExceptionResolver

public class MyExceptionResolver implements HandlerExceptionResolver{
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
        return new ModelAndView("jsp_to_show_error_message.jsp").
                 addObject("message", ex.getMessage());
    }
}

创建一个新的 jsp jsp_to_show_error_message.jsp 并将 ${message} 放入其中的某个位置。在 xml 中配置 MyExceptionResolver。如果您使用基于注释的控制器,请阅读

Use HandlerExceptionResolver.

public class MyExceptionResolver implements HandlerExceptionResolver{
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
        return new ModelAndView("jsp_to_show_error_message.jsp").
                 addObject("message", ex.getMessage());
    }
}

Create a new jsp jsp_to_show_error_message.jsp and put ${message} somewhere within it. Configure MyExceptionResolver in your xml. If you are using annotations based controller, read here.

我纯我任性 2024-11-06 20:06:40

将 Spring 排除在外,Servlet API 提供了将自定义错误页面附加到某些异常的可能性。在此特定示例中,只需将以下条目添加到 web.xml 即可。

<error-page>
    <exception-type>org.springframework.web.multipart.MultipartException</exception-type>
    <location>errors/upload.jsp</location>
</error-page>

通过这种方式,您可以在默认网页布局中以 errors/upload.jsp 的风格提供完全自定义的错误页面。

Leaving Spring outside consideration, the Servlet API offers the possibility to attach custom error pages to certain exceptions. In this particular example, it's a matter of adding the following entry to web.xml.

<error-page>
    <exception-type>org.springframework.web.multipart.MultipartException</exception-type>
    <location>errors/upload.jsp</location>
</error-page>

This way you can supply a fully custom error page in default webpage layout in flavor of errors/upload.jsp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文