What is the best way to handle default page not found error when a user requests a url that doesn't have any mapping in the application (e.g. a url like /aaa/bbb that there is no mapping for it in the application) while be able to add model attributes to the page?
相关问题
- Dependency injection into Logback Appenders with S
- Deserialize duplicate keys to list using Jackson
- Rails how to handle error and exceptions in model
- org.apache.commons.fileupload.disk.DiskFileItem is
- How can I access the repository from the entity in
相关文章
- spring-mvc a标签带参怎样向controller发送请求。路径格式?
- laravel create model from custom stub when using p
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
- Spring 5 Web Reactive - Hot Publishing - How to us
- Using Spring Dynamic Languages Support from Groovy
- Spring JMS : Set ErrorHandler for @JmsListener ann
There is some anserws in SO but those have caused me other problems and more importantly they don't state how to add model attributes to the error page. The best solution I have found is this:
ErrorController
and overrides itsgetErrorPath()
method.@RequestMapping("/error")
It's in this method that you can add whatever model attributes you want and return whatever view name you want:
Now if you want to handle other specific exceptions you can create
@ExceptionHandler
methods for them:Notes:
If you add specific exception handlers in the class, don't forget to annotate the controller with
@ControllerAdvice
(along with the@Controller
)The overridden
getErrorPath()
method can return any path you want as well as/error
.