2
你好,我是初學者到scala玩框架。我創建了一個簡單的註冊表單,並連接到mysql來插入rows.It運行良好。現在我想在不使用json刷新頁面的情況下在同一頁面上顯示這些插入的行。請建議我一個想法,如何讓在advance.Here同一page.Thanks插入行是我下面的代碼在Scala中使用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.Thanks提前
至於你我的問題不清楚,你的問題是什麼,你現在不是如何從你的'學生對象'創建'json對象'?或者如何創建Json響應或如何在頁面上操作它? – arussinov