play framework route that matches all

一世执手 提交于 2019-12-05 08:21:27

It's not possible to omit usage of matching elements but you can route a client via controller. The route definition looks like this:

GET         /*path               controllers.Application.matchAll(path)

And the corresponding controller can be implemented as follows:

public class Application extends Controller {

    public static Result matchAll(String path) {
        return redirect(controllers.routes.Assets.at("index.html"));
    }

}

Update

If you don't want to redirect a client you can return a static resource as a stream. In this case a response MIME type is required.

public class Application extends Controller {

    public static Result matchAll(String path) {
        return ok(Application.class.getResourceAsStream("/public/index.html")).as("text/html");
    }

}

For this task you can use onHandlerNotFound in Global class which will render some page without redirect:

import play.*;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;

import static play.mvc.Results.*;

public class Global extends GlobalSettings {

    public Promise<Result> onHandlerNotFound(RequestHeader request) {
        return Promise.<Result>pure(notFound(
            views.html.notFoundPage.render(request.uri())
        ));
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!