play framework static resources(assets) for 404 page

604 views Asked by At

I am using play framework 2.3 version and providing static html(Not using play template engine).

#Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file)

I would like to provide 404 page when user try to access wrong url

for example i have 2 html in public assets folder.
public/html/a.html
public/html/b.html
public/error/404.html

URL: localhost/assets/html/a.html -> 200 OK
URL: localhost/assets/html/b.html -> 200 OK
URL: localhost/assets/html/c.html -> 404 NOT FOUND PAGE so i would like to provide this page (URL: localhost/assets/error/404.html)

I already try to use Global.onHandlerNotFound and Global.onBadRequest But this is not working in case of static resources.

Please help me. Thank you.

1

There are 1 answers

1
user7197 On

First of all, put your static html for 404 error at public/ folder, e.g (public/error404.html)

Then, at onHandlerNotFound method, which Play docs tell is supposed to be called

If the framework doesn’t find an Action for a request, the onHandlerNotFound operation will be called

You'll return that page. So, you can return it that way

return Results.redirect(routes.Assets.at("error404.html"));

I think i had misunderstood your question.

Take notice that this method is supposed to handle not found Actions. If you want to handle not found assets, then I think you must modify the built-in Assets controller, once there it is, the action that handle assets responses , the Assets.at action. If there's no asset ir probably just calls Results.notFound(). It doesn't call any global method so you could handle the error, that's the point.