Play Framework: Redirect to controller method with arguments

ぐ巨炮叔叔 提交于 2019-12-22 05:33:48

问题


I am building a web application with PLAY framework 2.2.1 and am trying to display all available http get query parameters for the requested site in the address bar, even the ones that are not set in the request. In cases where not all http get parameters are set, I want to add the unset parameters with the default values and make a redirect.

I have a site that can be requested with GET:

GET /test controllers.Application.test(q:String, w:String ?= null, f:String ?= null, o:String ?= null)

Here is the method that I'd like to have in controllers.Application:

public static Result test(String q, String w, String f, String o){

    ...

    // In case not all parameters where set
    if (reload == 1){
            return redirect(controllers.Application.test(qDefault, wDefault, fDefault, oDefault));
    }
    else {
        ok(...);
    }
}

The problem is that redirect() takes a String and not a Result object.

My first solution is to write

return controllers.Application.test(qDefault, wDefault, fDefault, oDefault);

But unfortunately the adress bar does not update.

My second solution is to build the string manually:

return redirect("/test?q=" + query + "&f=" + f + "&w=" + w + "&o=" + showOptions);

This works fine, but is there no other way more elegant way to do this?


回答1:


Use the routes object :

public static Result index() {
    return redirect(controllers.routes.Application.test(qDefault, wDefault, fDefault, oDefault)); 
}

Source : Official Documentation




回答2:


in addition this is also valid

public static Result index() {
    return redirect("path"); 
}


来源:https://stackoverflow.com/questions/21233156/play-framework-redirect-to-controller-method-with-arguments

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