Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework: Redirect to controller method with arguments

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?

like image 941
Matthias Munz Avatar asked Jan 20 '14 11:01

Matthias Munz


People also ask

Is Play framework still used?

Play is rock-solid and used by hundreds of thousands of Java and Scala developers every month. Play is still extremely relevant to today's application and web development and has a passionate and very capable community around it ensuring that it has many good years left.

How do you set content type explicitly on play?

Changing the default Content-Type Will automatically set the Content-Type header to text/plain , while: JsonNode json = Json. toJson(object); Result jsonResult = ok(json); will set the Content-Type header to application/json .

What is controller in Play framework?

The Controller layer reduces the impedance mismatch between HTTP and the Domain Model. Note. There are different architectural models with different strategies. Some protocols give you direct access to the domain model objects. This is typically what EJB or Corba protocols do.

What is action in play framework?

What is an Action? Most of the requests received by a Play application are handled by an action. An action is basically a Java method that processes the request parameters, and produces a result to be sent to the client. public Result index(Http. Request request) { return ok("Got request " + request + "!"


2 Answers

Use the routes object :

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

Source : Official Documentation

like image 149
Isammoc Avatar answered Sep 21 '22 11:09

Isammoc


in addition this is also valid

public static Result index() {
    return redirect("path"); 
}
like image 32
Md Samiul Alim Sakib Avatar answered Sep 21 '22 11:09

Md Samiul Alim Sakib