问题
I've a little problem, i want to create a web app and i learn PlayFramework with java documentation of
This sample code :
public Result hello() {
DynamicForm requestData = formFactory.form().bindFromRequest();
String firstname = requestData.get("firstname");
String lastname = requestData.get("lastname");
return ok("Hello " + firstname + " " + lastname);
}
The ''formFactory'' doesn't exist.
http://i.imgur.com/W941Bgz.png
Why I don't have this field ?
And when i want to create a model, i don't have the model class
http://i.imgur.com/9FW7wp1.png
Thanks you so much if you resolve my problem ! :)
回答1:
From the documentation:
To wrap a class you have to inject a play.data.FormFactory into your Controller
Play already knows about FormFactory
, so just add a constructor parameter for it:
public class FooController {
private final FormFactory formFactory;
@Inject
public FooController(final FormFactory formFactory) {
this.formFactory = formFactory;
}
public Result hello() {
DynamicForm requestData = formFactory.form().bindFromRequest();
String firstname = requestData.get("firstname");
String lastname = requestData.get("lastname");
return ok("Hello " + firstname + " " + lastname);
}
}
I'm guessing the Model
you mention is that of EBean. You need to enable EBean for your project, and then you'll have the necessary classes on your classpath.
In project/plugins.sbt
:
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.0.0")
build.sbt
:
lazy val myProject = (project in file(".")).enablePlugins(PlayJava, PlayEbean)
More information is available in the relevant docs.
回答2:
First make sure to import these 2 libaries in your Play Controller:
import javax.inject.Inject;
import play.data.FormFactory;
After that before using the Form Builder, inject it into your code:
@Inject FormFactory formFactory;
Your code should work fine after this.
回答3:
You will have to inject your formFactory like this:
@Inject FormFactory formFactory;
来源:https://stackoverflow.com/questions/40339240/formfactory-form-doesnt-exist-playframework