play framework static resources(assets) for 404 page

653 views
Skip to first unread message

DoJin Kim

unread,
Dec 14, 2014, 8:55:10 PM12/14/14
to play-fr...@googlegroups.com

I've already asked http://stackoverflow.com/questions/27415986/play-framework-static-resourcesassets-for-404-page/27422736#27422736
But I still not get proper answer. If some one know my question. Please help me.

This is my question that i asked stackoverflow.

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.



https://www.playframework.com/documentation/2.0/Assets
As i see..

The router will invoke the Assets.at action with the following parameters:

controllers.Assets.at("public", "javascripts/jquery.js")

This action will look-up the file and serve it, if it exists.

Play Assets controller look-up file in public folder and serve But i would like to know if play can not find file how i can control for my 404 page.


James Roper

unread,
Dec 14, 2014, 11:51:15 PM12/14/14
to play-framework
Support for a custom assets not found page has been implemented for Play 2.4:


For now, if you want to return a custom page, you can do this by creating a custom action to serve assets that delegates to the assets controller and changes the result if the returned result is 404:

import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._

object MyAssets extends Controller {  
  def at(path: String, file: String) = EssentialAction { request =>
    controllers.Assets.at(path, file)(request).map { result =>
      if (result.header.status == NOT_FOUND) {
        // Create your custom 404 result here
        NotFound(file + " not found")
      } else result
    }
  }
}

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
James Roper
Software Engineer

Typesafe – Build reactive apps!
Twitter: @jroper

DoJin Kim

unread,
Dec 15, 2014, 1:43:09 AM12/15/14
to play-fr...@googlegroups.com
jroper Thank you for reply. I think your answer looks like i want. But i don't know scala.
Could you explain java souce?.

Thank you.


2014년 12월 15일 월요일 오후 1시 51분 15초 UTC+9, James Roper 님의 말:

raunak

unread,
Dec 25, 2014, 2:05:02 PM12/25/14
to play-fr...@googlegroups.com
You could just create a Scala controller (in your java app) with the name MyAsset and copy/paste the code James provided. After that, create/update route in your routes.conf file. The reason why I say just copy/paste James's code is because I couldn't find the Java's version of controllers.Asset class, and I don't think there is one... 

Alternatively, you could use the code I have written below and update your routes accordingly, through, it might not be considered as a safe solution.

public static Result at(String path, String file) {
    File asset = Play.application().getFile(Paths.get(path, file).toString());
    if(asset.exists()) {
        return ok(asset, true);
    } else return ok(Play.application().getFile("/public/error/404.html"), true);
}

Hope this helps.

DoJin Kim

unread,
Dec 25, 2014, 9:37:44 PM12/25/14
to play-fr...@googlegroups.com
Raunak Thank you.

I've been already using  like your answer. Check file exists and return data.
But i'm trying to find more clear and a safe solution.

And i would like to use java version as i can.

Anyway thank you all.

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/GmBxttVBTXY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Reply all
Reply to author
Forward
0 new messages