moCap
moCap

Reputation: 969

What is Request Response Cycle in Play

I have just started working on play 2.0 although it is not very complex so far but I'm not getting very basic thing i.e Request Response Cycle. I would love a minimal example of Request Response Cycle for JAVA Play

Upvotes: 2

Views: 384

Answers (2)

doniyor
doniyor

Reputation: 37904

what i want to point out is that if you want to pass data thru POST, you have to bind your request to some model field and then retrieve the passed data in your application: something like this:

your html:

<form action="/login" method="POST">
<input name="username"/>
...
</form>

your routes:

POST  /login      controllers.Application.login()

your Application:

public static Result login(){
Form<User> passedform = form(User.class).bindFromRequest();
if(passedform.hasErrors){
return badRequest("shit").as("text/plain");
} else {
User user = passedform.get();
System.out.print(user.username);
  }
}

now output is the username you have given in input field.. this will work python/django in this way:

def login(request):
  print(request.POST.get('username'))

:))) but anyway, play is beautiful also

hope i could help you

Upvotes: 2

szegedi
szegedi

Reputation: 873

Basically a request is handled by the HTTP router, which gets an url, eg: mydomain.com/details/. Then it tries to look this entry up in your routes config file. At the first matching line in the routes file, there's a corresponding method for that (a controller method), so it invokes the controller method, which will return an html response parameteriized by a view to be rendered.

Simplified: request (url) -> find the route in routes table -> call static controller method -> return an html response with the view

(also you can parameterize the url, eg: /details/12 and in the routes table: /details/:id, so you can pass the id to the controller method)

Another thing: it is also possible to do "reverse routing" which is parameterizing eg. a button to call a controller method directly, and it will find the corresponding url from the routes file

the official documentation is pretty good in this topic: http://www.playframework.org/documentation/2.0.2/JavaRouting

Upvotes: 3

Related Questions