-2

这是我的代码:

package controllers

import play.api._
import play.api.mvc._
import play.api.data.Form
import play.api.data.Forms.tuple
import play.api.data.Forms.text


object Application extends Controller {

  def index = Action {
Ok(views.html.index("Your new application is ready."))
}

val form = Form(
tuple(
  "x_card_num" -> text,
    "x_exp_date" -> text,
    "exp_year" -> text,
    "exp_month" -> text,
    "x_card_code" -> text,
    "x_first_name" -> text,
    "x_last_name" -> text,
    "x_address" -> text,
    "x_city" -> text,
    "x_state" -> text,
    "x_zip" -> text,
    "save_account" -> text
  )
)

def payment = Action { implicit request =>
def values = form.bindFromRequest.data


val x_card_num = if (values isDefinedAt "x_card_num") values("x_card_num") else ""
val x_exp_date = if (values isDefinedAt "x_exp_date") values("x_exp_date") else ""
val exp_year = if (values isDefinedAt "exp_year") values("exp_year") else ""
val exp_month = if (values isDefinedAt "exp_month") values("exp_month") else ""
val x_card_code = if (values isDefinedAt "x_card_code") values("x_card_code") else ""
val x_first_name = if (values isDefinedAt "x_first_name") values("x_first_name") else ""
val x_last_name = if (values isDefinedAt "x_last_name") values("x_last_name") else ""
val x_address = if (values isDefinedAt "x_address") values("x_address") else ""
val x_city = if (values isDefinedAt "x_city") values("x_city") else ""
val x_state = if (values isDefinedAt "x_state") values("x_state") else ""
val x_zip = if (values isDefinedAt "x_zip") values("x_zip") else ""
val save_account = if (values isDefinedAt "save_account") values("save_account") else ""

val message = if (save_account=="save_account")   {
    val test = "it is saved"
    "account saved"
} else {
    val test= "it is not saved"
    ""
}

      Ok(views.html.payment(message,x_card_num,x_exp_date,exp_year,exp_month,x_card_code,x_first_name,x_last_name,x_address,x_city,x_state,x_zip,save_account))
}

}

由于某种原因,变量 test 没有在以下 if 语句中定义:

val message = if (save_account=="save_account")   {
    val test = "it is saved"
    "account saved"
} else {
    val test= "it is not saved"
    ""
}

现在变量 test 没有设置。它设置消息变量,但不设置测试变量。

4

1 回答 1

2

val message在代码块中声明并且在外部使用它。

你可以这样做:

val message = if (save_account=="save_account")   {
        "account saved"
    } else {
        ""
    }
于 2012-06-28T16:19:12.013 回答