播放框架工作和scala:将参数值从一个页面传递到另一个页面

时间:2016-01-19 11:28:07

标签: scala playframework

我需要将带有对象的参数传递给另一页的页面

这是我的路由器

GET    /Location/edit:locid             controllers.Locations.editLocation(locid:Int)

我正在从这个页面打电话

@import models._
@import play.api.Play.current
@import play.api.i18n.Messages.Implicits._

@index(Messages("application.name")+" | Locations"){



<div class="container" style="float:left">

    <div class="menu_title">
        <h1>Locations</h1>
    </div>  

    <div class="menu_box">
        <a href="@routes.Locations.locationc"><p>Add new +</p></a>
    </div>      


  <table class="table table-hover">
    <thead>
      <tr>
        <th>Location ID</th>
        <th>Location Name</th>
        <th>Ref No</th>
            <th></th>
      </tr>
    </thead>
    <tbody>



        @for(l <- Location.findAllLocations()){
        <form action="@routes.Locations.editLocation(l.id)">
          <tr>
          <input type="hidden" name="locid" value="@l.id"/> 
        <td>@l.id</td>
        <td>@l.name</td>
        <td>@l.ref</td>
         <td>   <p id="subbut">  <input type="submit" id="logbut" class="btn btn-default" value='Edit'></p></td>

         </tr>
         </form>     

        }


    </tbody>
  </table>
</div>


}

这是我使用的控制器方法

def editLocation(locid: Int) = Action { implicit request =>
  request.session.get("mysession").map { user =>
    val form = if (request.flash.get("error").isDefined) 
                 locationForm.bind(request.flash.data) 
               else 
                 locationForm
    Location.findLocationBydb(locid).map { location =>
       Ok(views.html.location.editLocation(form,location))
    }.getOrElse(NotFound)
  }.getOrElse {
    Redirect(routes.Users.loginUser())
  }

}

这是Model

 def findLocationBydb(id:Int)={
   var locations= new ListBuffer[Location]()
   val conn = DB.getConnection()
   try {
     val stmt = conn.createStatement
     val rs = stmt.executeQuery("SELECT * from m_locations where idLoc='"+id+"'  ")
     if (rs.next()) {
       locations+= Location(rs.getInt("idLoc"),rs.getString("LocDes"),rs.getString("LocRef"));
     }
   } finally {
     conn.close()
   }
 locations.toList.head
}

但我得到了这个错误

  

从stdout读取:   d:\项目\测试\ SimpleRequest22-DBCon_Session \应用\控制器\ Locations.scala:71:   value map不是models.Location的成员   d:\项目\测试\ SimpleRequest22-DBCon_Session \应用\控制器\ Locations.scala:71:   value map不是models.Location从stdout读取的成员:   Location.findLocationBydb(locid).map {location =&gt;                                   Location.findLocationBydb(locid).map {location =&gt;从stdout读取:^                                                                    ^从标准输出读取:   d:\项目\测试\ SimpleRequest22-DBCon_Session \应用\视图\位置\ editLocation.scala.html:7:   找不到:值loc   d:\项目\测试\ SimpleRequest22-DBCon_Session \应用\视图\位置\ editLocation.scala.html:7:   找不到:value loc从stdout读取:@(loc:Location)@(loc:Location)   从stdout读取:^           ^从stdout读取:发现两个错误发现两个错误从stdout读取:(编译:compileIncremental)编译失败   (compile:compileIncremental)编译失败

2 个答案:

答案 0 :(得分:0)

您获得的错误map不是Location的成员。您正尝试在map返回的位置上使用Location.findLocationBydb

而不是

Location.findLocationBydb(locid).map { location =>
  Ok(views.html.location.editLocation(form,location))
}

使用

val location = Location.findLocationBydb(locid)
Ok(views.html.location.editLocation(form,location))

答案 1 :(得分:0)

嗨朋友们,我得到了解决方案, 首先,我改变了这样的控制器方法

 def editLocation(locid: Int) = Action { implicit request =>                  
                request.session.get("mysession").map { user =>

                        val form = if (request.flash.get("error").isDefined) 
                            locationForm.bind(request.flash.data) 
                            else 
                                locationForm
                                Ok(views.html.location.editLocation(locationForm,Location.findLocationBydb(locid)))

                }.getOrElse {
                    Redirect(routes.Users.loginUser())
                }             
  }

此外,我已经通过

更改了视图页面(editLocation.scala.html)
@(locationForm: Form[Location],loc:Location)(implicit flash: Flash, lang: Lang)