[Play]不要在路径中使用多个参数来显示带有多个参数的视图

时间:2014-11-07 11:52:39

标签: forms scala playframework routes

我正在Scala中编写Play 2.3.2应用程序。

在我的应用程序中,我使用的是表示整数范围(开始,结束)的表单。

范围class写成如下:

case class Range (
      startDate: Long,
      finishDate: Long)

我已经定义了如下表单:

val rangeForm: Form[Range] = Form (
    mapping(
      "startDate" -> longNumber(min = 0, max = System.currentTimeMillis()),
      "finishDate" -> longNumber(min = 0, max = System.currentTimeMillis())
    )(Range.apply)(Range.unapply)

  )

在我的StatisticsController class中我定义了三个四个:

  • calculateStatisticsOnRange(range: Range): Future[(Double, Int)]:计算范围内用户点击次数和平均值;
  • getCalculateStatisticsOnRangeForm显示表单
  • calculateGoodAdvicesHtml检查表单是否有错误
  • 显示搜索结果的
  • displayStatistics(average: Double, count:Int)

我的表单视图实现如下:

@(rangeForm: Form[recommendationsystem.models.Range])

@recommendationsystem.views.html.main("Insert a range")(recommendationsystem.views.html.nav.navbar("statistics")) {
    <h1>Insert here the range</h1>
    <div class="container">
        @helper.form(action = recommendationsystem.controllers.manager.routes.StatisticsController.calculateGoodAdvicesHtml()) {

                <label for="startDate">Start Date:</label>
                <input type="number" class="form-control" placeholder = "start"
                                     name="startDate" value="@rangeForm("startDate")" />
                <input type="number" class="form-control" placeholder="finish"
                                    name="finishDate" value="@rangeForm("finishDate")" />
                <button type="submit" class="btn btn-default">Search</button>



        }
    </div>

}

我的控制器方法实现如下:

def getCalculateStatisticsOnRangeForm = Action {
     Ok(recommendationsystem.views.html.manager.statistics.forms.rangeform(rangeForm))
   }



def calculateGoodAdvicesHtml = Action.async { implicit request =>
    val form = rangeForm.bindFromRequest

    form.fold(
      errors => Future{BadRequest(recommendationsystem.views.html.manager.statistics.forms.rangeform(errors))},
      range =>  {calculateStatisticsOnRange(range) flatMap {result => Future{Redirect(routes.StatisticsController.displayStatistics(result._1, result._2))}}}
    )

  }


def displayStatistics(average: Double, count:Int) = Action{
    Ok(recommendationsystem.views.html.manager.statistics.goodadvise(average, count))
  }

在我的路线档案中,我有以下路线:

GET            /statistics/good                        @recommendationsystem.controllers.manager.StatisticsController.getCalculateStatisticsOnRangeForm

POST           /statistics/good                        @recommendationsystem.controllers.manager.StatisticsController.calculateGoodAdvicesHtml

POST           /statistics/good/json                   @recommendationsystem.controllers.manager.StatisticsController.calculateGoodAdvicesJson

GET            /statistics/:average/:count             @recommendationsystem.controllers.manager.StatisticsController.displayStatistics(average: Double, count: Int)

我不想在/ statistics /:average /:count的路径中传递多个参数,还有另一种方法可以做到吗?

0 个答案:

没有答案