Play 2.0.4 Catch-all route always hits

五迷三道 提交于 2019-12-08 06:05:27

问题


At the end of my routes file I put a catch-all route to catch requests which wasn't catch previously and pass it to my own router (for further processing):

GET    /*nameUrl      controllers.Application.router(nameUrl: String)

of course there are many other routes BEFORE that line. The big surprise for me is that the catch-all is hitten every time, even if previous route is hitten as well, so if I'm opening address domain.tld/test it displays me both logs in the console Test action hit! AND Custom router hit! . There is a simplified sample:

public static Result test() {
    Logger.debug("Test action hit!");
    return ok();
}

public static Result router(String nameUrl) {
    Logger.debug("Custom router hit!");
    return ok();
}

Routes (in this order)

GET    /test          controllers.Application.test
GET    /*nameUrl      controllers.Application.router(nameUrl: String)

What do I want to get:

I want to get url's for articles with my router ie domain.tld/category_1/article_title without any prefix before it, of course if I change catch all to something stable it won't get double hits anymore:

GET    /news/*nameUrl      controllers.Application.router(nameUrl: String)
domain.tld/news/category_1/article_title

however I really want to avoid /news/ segment. Is that possible?


回答1:


I repeated it and had the same problem with Chromium (core of Google Chrome), but not with Firefox.

With Global.java I analyzed the request.

public class Global extends GlobalSettings {
    @Override
    public Action onRequest(Http.Request request, Method method) {
        Logger.info("request-path: " + request.path());
        return super.onRequest(request, method);
    }
}
//output:
[info] application - request-path: /favicon.ico

For every GET /test request Chromium tries to load the favicon.

So, include the following in conf/routes:

GET  /favicon.ico   controllers.Assets.at(path="/public", file="favicon.ico")


来源:https://stackoverflow.com/questions/13101352/play-2-0-4-catch-all-route-always-hits

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