Question

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

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

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\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)

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

Was it helpful?

Solution

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)

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.

OTHER TIPS

You must have all the parameters defined for same function. In your case allPosts. Give a default value for routes where you don't need that parameter.

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

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

You can also use Optional Parameters. Something like

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

Then you can call this with or without a parameter in the query:

/posting?number=1
/posting

Make sure you declare an Option as well in your controller

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top