2014-04-25 104 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方法請求。