如何防止 Spring MVC 3.0 应用程序中的 JSP 上出现原始异常信息?
在测试我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以前没有做过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.
使用 HandlerExceptionResolver。
创建一个新的 jsp
jsp_to_show_error_message.jsp
并将${message}
放入其中的某个位置。在 xml 中配置MyExceptionResolver
。如果您使用基于注释的控制器,请阅读Use HandlerExceptionResolver.
Create a new jsp
jsp_to_show_error_message.jsp
and put${message}
somewhere within it. ConfigureMyExceptionResolver
in your xml. If you are using annotations based controller, read here.将 Spring 排除在外,Servlet API 提供了将自定义错误页面附加到某些异常的可能性。在此特定示例中,只需将以下条目添加到
web.xml
即可。通过这种方式,您可以在默认网页布局中以
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
.This way you can supply a fully custom error page in default webpage layout in flavor of
errors/upload.jsp
.