您好我是scala play框架的初学者。我创建了一个简单的注册表单并连接到mysql以插入行。它工作得很好。现在我想在同一页面上显示那些插入的行,而不使用json刷新页面。请建议我如何在同一页面上插入行。请提前谢谢。这是我的以下代码
Routes:
# Home page
GET / controllers.Application.index
GET /createform controllers.StudentController.createform()
POST /save controllers.StudentController.save()
控制器:学生控制器
package controllers
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import scala.collection.mutable.HashMap
import views.html
import models.Student
object StudentController extends Controller {
val studentform= Form (
tuple(
"firstname"->text,
"lastname"->text
)
)
def createform = Action {
Ok(html.createform(studentform))
}
def save = Action { implicit request=>
studentform.bindFromRequest.fold(
errors=> BadRequest(html.createform(errors)),
{
case(firstname,lastname)=>Student.create(firstname,lastname)
Redirect(routes.Application.index())
}
)
}
}
Application Controller
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
Redirect(routes.StudentController.createform)
//Ok(views.html.index("Your new application is ready."))
}
}
型号:
包装模型
import play.api.db._
import play.api.Play.current
import anorm._
import anorm.SqlParser._
case class Student (
id:Pk[Long]=NotAssigned,
firstname: String,
lastname: String
)
object Student {
def create(firstname: String,lastname:String) : Unit={
DB.withConnection{ implicit Connection=>
SQL("insert into student (Firstname,Lastname)" + "values({firstname},{lastname})"
).on(
'firstname->firstname,
'lastname->lastname
).executeUpdate()
}
}
}
视图 createform.scala.html
@(studentform: Form[(String,String)])
@import helper._
@main(title="Student Registration Form"){
@form(action=routes.StudentController.save){
<fieldset>
<legend>Add Student</legend>
@inputText(
field=studentform("firstname"),
args='_label->"FirstName"
)
@inputText(
field=studentform("lastname"),
args='_label->"LastName"
)
<br/>
<div class="actions">
<input type="submit" value="Submit">
<a href="@routes.Application.index">Cancel</a>
</div>
</fieldset>
}
}
index.scala.html
@main("Welcome to Play 2.0") {
<a href="/createform">Add a new Student</a>
}
请建议我将插入的数据存储在JSON对象中,并在scala中将相同的插入行存储在同一页面上。提前谢谢
答案 0 :(得分:1)
使用json读取和写入然后将student作为对象传递给create方法:
object Student {
implicit object PkFormat extends Format[Pk[Long]] {
def reads(json: JsValue):JsResult[Pk[Long]] = JsSuccess(Id(json.as[Long]))
def writes(id: Pk[Long]):JsNumber = JsNumber(id.get)
}
implicit val studentReads: Reads[Student] = (
(__ \ "id").readNullable[Pk[Long]].map(_.getOrElse(NotAssigned)) ~
(__ \ "firstname").read[String] ~
(__ \ "lastname").read[String]
)(Student.apply _)
implicit val studentWrites = Json.writes[Student]
def create(student: Student): Student = {
DB.withConnection { implicit c =>
val id: Long = student.id.getOrElse {
SQL("select next value for student_id_seq").as(scalar[Long].single)
}
SQL(
"""
insert into student values (
{id}, {firstname}, {lastname}
)
"""
).on(
'id -> id,
'firstname -> student.firstname
'lastname -> student.lastname
).executeUpdate()
student.copy(id = Id(id))
}
}
}
然后“同一页面”可以只是一个ajax提交表单,将带有firstname和lastname的student对象传递给create方法,然后重新呈现或附加到学生列表中。您可以通过返回新添加的学生,然后附加结果,或者再次为整个学生列表调用数据库并呈现整个列表来传回响应。