2013-06-01 43 views
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提前

+0

至於你我的問題不清楚,你的問題是什麼,你現在不是如何從你的'學生對象'創建'json對象'?或者如何創建Json響應或如何在頁面上操作它? – arussinov

回答

1

使用json讀寫然後通過學生作爲對象到您的創建方法:

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提交的形式,將具有名字和姓氏的學生對象傳遞給create方法,然後重新呈現或附加到學生列表。您可以通過返回新添加的學生,然後附加結果或再次調用數據庫來爲整個學生列表傳遞迴應,並呈現整個列表。