问题
I'm currently working on a web app with the play2 framework.
Most of the time everything works fine. However when I decide to rebuild the project I get the error: "type HomeController is not a member of package controllers".
I've done some research on the web and encountered discussions and topics about this error. However, on all of them this seems to be the answer:
"Play 2.4, by default, generates a dependency injected router, unlike previously, when it used a static router. You have two options, remove the routesGenerator line from build.sbt so play will generate a static router, or (better) make your controllers classes instead of objects, and use dependency injection."
But, in my project, the HomeController is of type class instead of object and there doesn't seem to be a routesGenerator lin in build.sbt.
It is kinda bugging me out, so if someone has an answer to it, that would be super awesome.
I managed to get around it two times already but I don't remember how :(.
Ok. Time for some specs.
- Intellij IDEA 2016.2.5
- Play 2.5.9
- Windows 10
- Java Version 8 Update 111
And now for the code:
HomeController:
package controllers;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.Model;
import models.Strike;
import play.data.Form;
import play.data.FormFactory;
import play.mvc.*;
import views.html.*;
import javax.inject.Inject;
import java.util.List;
import static play.libs.Json.toJson;
/**
* This controller contains an action to handle HTTP requests
* to the application's home page.
*/
public class HomeController extends Controller {
@Inject FormFactory formFactory;
public Result index() {
return ok(index.render("", formFactory.form(Strike.class)));
}
public Result addStrike()
{
Form<Strike> st = formFactory.form(Strike.class).bindFromRequest();
st.get().save();
return redirect(routes.HomeController.index());
}
public Result getStrikes(){
List<Strike> strikes = new Model.Finder(Strike.class).all();
return ok(toJson(strikes));
}
}
Routes:
GET / controllers.HomeController.index()
POST /strike controllers.HomeController.addStrike()
GET /strikes controllers.HomeController.getStrikes()
Build.sbt:
name := ""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs
)
Anyone have any idea?
Thanks in advance!
来源:https://stackoverflow.com/questions/40508995/type-is-not-a-member-of-package-controllers