我刚刚开始研究play 2.0,虽然到目前为止它不是很复杂,但我没有得到非常基本的东西,即请求响应周期。我想要一个 JAVA Play 的请求响应周期的最小示例
2 回答
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
我想指出的是,如果你想通过 POST 传递数据,你必须将你的请求绑定到某个模型字段,然后在你的应用程序中检索传递的数据:像这样:
你的html:
<form action="/login" method="POST">
<input name="username"/>
...
</form>
你的路线:
POST /login controllers.Application.login()
你的申请:
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);
}
}
现在输出是您在输入字段中给出的用户名..这将以这种方式工作 python/django:
def login(request):
print(request.POST.get('username'))
:))) 但无论如何,游戏也很漂亮
希望我能帮助你