Bây giờ tôi biết tôi có thể không có mã số sạch nhưng tôi chỉ muốn thứ chết tiệt để làm việc. Nó cho tôi lỗi "Không được phép. Bạn phải được xác thực để truy cập trang này".Không được phép: Bạn phải được xác thực để truy cập trang này. - Khi đi qua các hình thức chơi
Tôi có bộ điều khiển sau
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import models.UserData
import models.Contact
import play.api.i18n._
/**
* This controller creates an `Action` to handle HTTP requests to the
* application's home page.
*/
@Singleton
class HomeController @Inject()(val messagesApi: MessagesApi)extends Controller with I18nSupport {
/**
* Create an Action to render an HTML page.
*
* The configuration in the `routes` file means that this method
* will be called when the application receives a `GET` request with
* a path of `/`.
*/
def index = Action { implicit request =>
Ok("Got request [" + request + "]")
}
val userForm = Form(mapping("name"->nonEmptyText, "age"->number(min=0, max=100))(UserData.apply)(UserData.unapply))
def userPost = Action {
implicit request =>
userForm.bindFromRequest.fold(
formWithErrors => {
BadRequest(views.html.user(formWithErrors))
},
userData => {
val newUser = models.UserData(userData.name, userData.age)
Redirect(routes.HomeController.home())
})
}
def home = Action { implicit request =>
Ok(views.html.index())
}
def user = Action {implicit request =>
Ok(views.html.user(userForm))
}
}
File user.scala.html sau
@(userForm: Form[UserData])(implicit messages: Messages)
@helper.form(action = routes.HomeController.userPost()) {
@helper.inputText(userForm("name"), 'id -> "name", 'size -> 30)
@helper.inputText(userForm("age"))
<input type="submit" value="submit"</input>
}
và các tuyến đường sau file:
# Routes
# This file defines all application routes (Higher priority routes first)
# https://www.playframework.com/documentation/latest/ScalaRouting
# ~~~~
# An example controller showing a sample home page
GET / controllers.HomeController.index
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
GET /home controllers.HomeController.home
GET /user controllers.HomeController.user
POST /user controllers.HomeController.userPost
Ai đó có thể vui lòng giúp tôi có được vượt qua lỗi trang trái phép. Tôi không biết tại sao nó lại xuất hiện. Tôi chỉ muốn vượt qua một hình thức đơn giản.
Cảm ơn bạn.
Cậu cuối cùng thêm một toàn cầu [filter CSRF] (https://www.playframework.com/documentation/2.5.x/ScalaCsrf)? Nếu bạn cần phải thêm mã thông báo CSRF vào biểu mẫu của mình: '@portport helper._ @form (CSRF (routes.HomeController.userPost())) {' –
nhận xét của bạn đã giúp. tôi đã cố gắng khắc phục nó với những gì bạn nói. cảm ơn bạn rất nhiều vì đã giúp đỡ của bạn – zam
@zam: Tôi có cùng một vấn đề. Bạn có thể vui lòng gửi giải pháp của bạn? Khi tôi sử dụng CSRF, tôi nhận được một lỗi khác 'Không thể tìm thấy bất kỳ Tiêu đề Yêu cầu HTTP nào tại đây'. Tôi đang sử dụng phiên bản 2.6.x. – costa