2014-04-25 56 views
-1

当反向路由到一个GET路线,我根本就斯卡拉播放模板:反转与HTTP POST路由

<a href="@controllers.routes.Application.sayHello()">Hello</a> 

这意味着那里有一个GET路线sayHello的函数。如果它是POST并需要附加的有效负载呢?是隐式的吗?有效载荷数据如何附加?

回答

1

反向路由器的HTTP方法源自您的路由配置文件。

下面是从一个路由配置的实例,其中我有相同的URL两个不同的请求,但不同的HTTP方法指向不同的方法:

GET  /login        controllers.Application.login 
POST /login        controllers.Application.authenticate 

登录()在应用程序的控制器方法简单地流出来的HTML形式:

def login = Action { implicit request => 
    Ok(html.loginForm(loginForm)) 
} 

authenticate()的但是方法结合该请求到表单允许进一步PROCES唱:

def authenticate = Action { implicit request => 
    loginForm.bindFromRequest.fold(
     formWithErrors => BadRequest(html.loginForm(formWithErrors)), 
     user => { 
      // OTHER CODE HERE 
      Redirect(routes.Blog.createPost).withSession("user" -> user) 
     } 
    )  
} 

第二种方法需要一个控制器中的表单定义:

val loginForm = Form(
    tuple(
     "username" -> text, 
     "password" -> text 
     ) verifying ("Invalid username or password", result => result match { 
      case (username, password) => Account.authenticate(username, password).isDefined 
     }) 
    ) 

所以要根据您放置在您的看法相反的路由器的方法,也就是将要使用的HTTP方法请求。