来自 Form 类的方法 form(Class<T>) 在 Play 中已弃用!框架 [英] The method form(Class<T>) from Form class is deprecated in Play! Framework
问题描述
我花了很多时间试图解决这个问题,并寻找替代方法,但没有成功.
I spent a lot of time trying so solve this problem, and searching the method to substitute this one, without success.
首先,玩!我有义务使用和注入 FormFactory(在 https://www.playframework.com/documentation/2.5.0/JavaForms).
First, Play! obligates me to use and inject the FormFactory (that is explained in https://www.playframework.com/documentation/2.5.0/JavaForms).
但只是为了实例化这个 FormFactory,我必须为它的构造函数传递 3 个参数,即 MessagesApi、Formatters 和 Validator.包括,我必须实例化 Validator 接口,但我不确定这样做是否正确.
But just for instantiate this FormFactory I had to pass 3 parameters for it's constructor, that is MessagesApi, Formatters and Validator. Including, I had to instantiate the Validator interface, and I'm no sure that is the right way to do it.
为了更简单,我把它放在另一个类中:
To make it more easy, I separated it in another class:
package controllers;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;
import javax.validation.metadata.BeanDescriptor;
import play.data.FormFactory;
import play.data.format.Formatters;
import play.i18n.MessagesApi;
class Validador implements Validator {
@Override
public ExecutableValidator forExecutables() {
// TODO Auto-generated method stub
return null;
}
@Override
public BeanDescriptor getConstraintsForClass(Class<?> arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T unwrap(Class<T> arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> Set<ConstraintViolation<T>> validate(T arg0, Class<?>... arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> Set<ConstraintViolation<T>> validateProperty(T arg0, String arg1, Class<?>... arg2) {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> Set<ConstraintViolation<T>> validateValue(Class<T> arg0, String arg1, Object arg2, Class<?>... arg3) {
// TODO Auto-generated method stub
return null;
}
}
public class FormCreator {
MessagesApi msgAPI;
Formatters formatador;
Validador validador;
FormFactory factory;
public FormCreator() {
msgAPI = new MessagesApi(null);
formatador = new Formatters(msgAPI);
validador = new Validador();
factory = new FormFactory(msgAPI, formatador, validador);
}
// getters e setters
public MessagesApi getmsgAPI() {
return msgAPI;
}
public void setmsgAPI(MessagesApi msgAPI) {
this.msgAPI = msgAPI;
}
public Formatters getFormatador() {
return formatador;
}
public void setFormatador(Formatters formatador) {
this.formatador = formatador;
}
public Validador getValidador() {
return validador;
}
public void setValidador(Validador validador) {
this.validador = validador;
}
public FormFactory getFactory() {
return factory;
}
public void setFactory(FormFactory factory) {
this.factory = factory;
}
}
在我的控制器中,我必须输入如下内容:
And in my controller I have to put something like this:
@Inject
FormCreator formCreator = new FormCreator();
虽然我花了几个小时来发现这一点,但这个问题已经解决了.
althrough that I spent some hours to discover this, this question was solved.
另一个是,无论我做什么,bindFromRequest() 方法总是返回 null,并且没有,因为 eclipse,以及其他任何事情,这是因为我从这个工厂创建的表单中调用它.
Another one is, whatever I do, the method bindFromRequest() always returns null, and is no because the eclipse, and anything else, it's because I call it from a form created by this factory.
示例:
// never works
formCreator.getFactory().form(Diretor.class).bindFromRequest();
// works fine, but is deprecated
Form.form(Diretor.class).bindFromRequest();
使用这种已弃用方法的最新方法是什么?
What is the newest method to use instead this deprecated method?
提前致谢.
推荐答案
Play 2.5.x 文档说明:
The Play 2.5.x documentation states it:
要包装一个类,您必须将 play.data.FormFactory 注入您的控制器,然后允许您创建表单:
To wrap a class you have to inject a play.data.FormFactory into your Controller which then allows you to create the form:
Form userForm = formFactory.form(User.class);
Form userForm = formFactory.form(User.class);
所以第一步是将 FormFactory
注入您的控制器.你这样做:
So the first step is to inject the FormFactory
into your controller. You do it like this:
package controllers;
import play.*;
import play.mvc.*;
public class Application extends Controller {
@Inject FormFactory formFactory;
...
}
然后在处理表单提交请求的操作中,您可以像这样使用 FormFactory
:
Then in the action where you handle the form-submission request you can use the FormFactory
like this:
public Result handleFormSubmission() {
formFactory.form(Director.class).bindFromRequest();
}
这篇关于来自 Form 类的方法 form(Class<T>) 在 Play 中已弃用!框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!