我正在尝试在Play框架中实现Java表单。我正在做文档中的所有事情,但我有一个错误。 https://www.playframework.com/documentation/2.5.x/JavaForms
我的代码:
import play.api.data.Form;
import play.data.FormFactory;
public class Products extends Controller {
@Inject FormFactory formFactory;
Form<Product> productForm = formFactory.form(Product.class);
...
错误:
no instance(s) of type variable(s) T exist so that play.data.Form<T> conforms to play.api.data.Form<models.Product>
将导入库play.api.data更改为play.data后出现此错误:
1) Error injecting constructor, java.lang.NullPointerException
at controllers.Products.<init>(Products.java:18)
while locating controllers.Products
for parameter 2 at router.Routes.<init>(Routes.scala:32)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler <init>(HttpRequestHandler.scala:200)
while locating play.api.http.JavaCompatibleHttpRequestHandler
while locating play.api.http.HttpRequestHandler
for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
at play.api.DefaultApplication.class(Application.scala:221)
while locating play.api.DefaultApplication
while locating play.api.Application
答案 0 :(得分:0)
您导入来自Play jar的play.api.data.Form
,但FormFactory
会返回play.data.Form
,而不是之前的那些,所以您只需要更改导入。
答案 1 :(得分:-3)
正确的方法是使用构造函数编写代码,然后注入FormFactory formFactory
。
private Form<Product> productForm;
@Inject
public ProductsController(FormFactory formFactory) {
this.productForm = formFactory.form(Product.class);
}
按照下面的链接执行此策略。