formFactory.form() doesn't exist ! PlayFramewo

2019-06-28 00:12发布

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 ! :)

3条回答
孤傲高冷的网名
2楼-- · 2019-06-28 00:22

You will have to inject your formFactory like this:

@Inject FormFactory formFactory;
查看更多
萌系小妹纸
3楼-- · 2019-06-28 00:26

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.

查看更多
我命由我不由天
4楼-- · 2019-06-28 00:36

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.

查看更多
登录 后发表回答