本文介绍了播放框架路由和 Scala 预定义值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在 play 框架 2.2 上开发应用程序我有一个这样的路由文件:

I develop application on play framework 2.2I have a routes file like this:

GET  /posting/          controllers.posting.BlogController.allPosts()
GET  /posting/:number   controllers.posting.BlogController.allPosts(number: Int)

和BlogContriller:

And BlogContriller:

object BlogController extends Controller {

  def allPosts(pageNumber:Int = 1, postsPerPage:Int = 10) = Action{
    val posts = Post.getLastNPosts(postsPerPage, postsPerPage*(pageNumber-1))
    val htmlPosts = new Html(new StringBuilder());

    for (post <- posts){
      val htmlPost = views.html.posting.post(post.getName, post.getText, post.getDate.toString)
      htmlPosts += htmlPost;
    }

    Ok(views.html.posting.index(htmlPosts))
  }
}

当我试图解释这个时,我给出了一个错误:

When I try to comile that, I give a error:

Error:(14, -1) Play 2 Compiler:  C:...conf
outes:14: Compilation error[Using different overloaded methods is not allowed. If you are using a single method in combination with default parameters, make sure you declare them all explicitly.]
GET  /posting/:number   controllers.posting.BlogController.allPosts(number: Int)

我不明白如何解决这个问题.有人可以帮我吗?

And I can't understand how to fix that. Can anyone help me?

推荐答案

您可以使用默认值的参数:

You can must use parameter with default value:

GET  /posting/          controllers.posting.BlogController.allPosts(number: Int = 1)
GET  /posting/:number   controllers.posting.BlogController.allPosts(number: Int)

您不得使用重载方法 allPosts(Int)allPosts.由于您使用两个带有默认值的参数声明了 allPosts,因此 Scala 将此方法视为 4 种不同的方法.您只能使用其中之一.

You may not use overloaded methods allPosts(Int) and allPosts. Since you declared allPosts with two parameters with default values, Scala sees this method as 4 different methods. You may only use one of them.

这篇关于播放框架路由和 Scala 预定义值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:54