How to create a custom 404 page handler with Play 2.0?

后端 未结 7 2086
故里飘歌
故里飘歌 2021-01-03 19:22

What’s the preferred way to handle 404 errors with Play 2.0 and show a nice templated view?

7条回答
  •  一生所求
    2021-01-03 20:04

    If you want to do the same using Java instead of Scala you can do it in this way (this works for play framework 2.0.3):

    Global.java:

    import play.GlobalSettings;
    import play.mvc.Result;
    import play.mvc.Results;
    import play.mvc.Http.RequestHeader;
    
    
    public class Global extends GlobalSettings {
    
        @Override
        public Result onHandlerNotFound(RequestHeader request) {
            return Results.notFound(views.html.error404.render());
        }
    }
    

    Asumming that your 404 error template is views.html.error404 (i.e. views/error404.scala.html).

提交回复
热议问题