【问题标题】:play framework static resources(assets) for 404 page播放404页面的框架静态资源(资产)
【发布时间】:2015-02-09 12:45:36
【问题描述】:

我使用的是play framework 2.3版本并提供静态html(不使用play模板引擎)。

#将 /public 文件夹中的静态资源映射到 /assets URL 路径 GET /assets/*file controllers.Assets.at(path="/public", file)

当用户尝试访问错误的 url 时,我想提供 404 页面

例如,我在公共资产文件夹中有 2 个 html。
公共/html/a.html
公共/html/b.html
public/error/404.html

网址:localhost/assets/html/a.html -> 200 OK
网址:localhost/assets/html/b.html -> 200 OK
URL: localhost/assets/html/c.html -> 404 NOT FOUND PAGE 所以我想提供这个页面 (URL: localhost/assets/error/404.html)

我已经尝试使用 Global.onHandlerNotFound 和 Global.onBadRequest 但这在静态资源的情况下不起作用。

请帮助我。谢谢。

【问题讨论】:

    标签: playframework static-resource


    【解决方案1】:

    首先,将 404 错误的静态 html 放在 public/ 文件夹中,例如 (public/error404.html)

    然后,在 onHandlerNotFound 方法中,Play 文档告诉它应该被调用

    如果框架没有为请求找到 Action,则 将调用 onHandlerNotFound 操作

    您将返回该页面。所以,你可以这样退货

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

    我想我误解了你的问题。

    请注意,此方法应该处理未找到的操作。如果您想处理未找到的资产,那么我认为您必须修改内置的资产控制器,一旦有,处理资产响应的动作,即 Assets.at 动作。如果没有资产,我可能只是调用 Results.notFound()。它不调用任何全局方法,因此您可以处理错误,这就是重点。

    【讨论】:

    • 感谢您的回复。我正在尝试通过您的回答来解决我的问题。但它不起作用。无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多