gpt4 book ai didi

scala - Play Framework 路由和scala predef值

转载 作者:行者123 更新时间:2023-12-03 12:40:37 25 4
gpt4 key购买 nike

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

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

和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))
}
}

当我尝试对其进行编译时,出现错误:
Error:(14, -1) Play 2 Compiler:  C:\...\conf\routes: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)

而且我不知道该如何解决。谁能帮我?

最佳答案

您必须使用带默认值的参数:

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

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

关于scala - Play Framework 路由和scala predef值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23314907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com