[英]Generic Action in Play Framework controller
我在網站的兩頁上有一個請求表:關於我們和聯系方式。 在Home控制器中介紹了針對這兩種方法的操作。 提交表單后,我調用sendRequest方法,該方法檢查表單錯誤並發送請求。 該方法如下所示:
def sendRequest = Action {implicit request =>
requestForm.bindFromRequest.fold(
formWithErrors => {
BadRequest(views.html.Home.contacts(formWithErrors))
},
response => {
Redirect(routes.HomeController.contacts().flashing("success" -> "OK"))
}
)
}
在這種方法中,我定義了視圖和導航路線。 對於“關於我們”頁面,我應該做同樣的工作,但請致電給我們查看和瀏覽路線。
如何創建通用方法並使用具體視圖和路由作為參數來調用它?
// if requestForm is a Form[A]
protected def genericMethod(
view: Form[A] => play.twirl.api.Html, route: play.api.mvc.Call) =
Action {implicit request =>
requestForm.bindFromRequest.fold(
formWithErrors => {
BadRequest(view(formWithErrors))
},
response => {
Redirect(route.flashing("success" -> "OK"))
}
)
}
def sendRequest = genericMethod(
views.html.Home.contacts,
routes.HomeController.contacts())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.