Is there a controller for 404 pages (Play! Framework)

I work with Play! Framework 1.2.4 and has no problem creating static 404 pages. However, I would like to make the 404 page a little more dynamic.

Is there a "controller" for 404 pages? If not, then where do I do all my logic, which is traditionally included in the controller?

+4
source share
5 answers

There is no controller on page 404. Try to find a route, and if this is not possible, you will get a NotFound result that shows page 404.

You can create a catch controller that performs this task so that this controller handles the 404 case instead of the playback itself.

* /.* NotFoundRouter.notFoundPage 
+4
source

As Nasir said, you can respond with a NotFound action on any controller to return a 404 response. Usually we respond with Ok Action for a proper 200 Http response code. Example (play 2 - not sure if it works to play 1):

 object BadgeEvidence extends Controller { def index(badgeName: String, earner: String) = Action { val badgeResponse = BadgeDao.getBadgeDetail(badgeName) val evidence = BadgeDao.getBadgeEvidence(badgeName, earner) if (evidence.length == 0) { NotFound(earner + " is not valid or has not earned " + badgeName); } else { badgeResponse match { case badgeDetail: BadgeRow => Ok(views.html.badgeEvidence(earner, badgeDetail, evidence)) case errorMessage: String => Ok(views.html.showError(errorMessage)) } } } } 
+2
source

You can use the GlobalSettings.onHandlerNotFound () function to handle requests that receive a 404 response.

http://www.playframework.org/documentation/api/2.0.3/java/play/GlobalSettings.html

+1
source

You can do some encoding inside the app/views/errors/404.html template file.

0
source

Well, I'm not sure how dynamic you are. In depth, the NotFound class is responsible for loading 404.html. Perhaps this may be useful for you.

0
source

Source: https://habr.com/ru/post/1431997/


All Articles