Form class 中的方法 form(Class<T>) 在 Play 中已弃用!框架
The method form(Class<T>) from Form class is deprecated in Play! Framework
我花了很多时间尝试解决这个问题,并寻找替代方法,但没有成功。
首先,玩!要求我使用和注入 FormFactory(在 https://www.playframework.com/documentation/2.5.0/JavaForms 中有解释)。
但是为了实例化这个 FormFactory,我必须为其构造函数传递 3 个参数,即 MessagesApi、Formatters 和 Validator。包括,我必须实例化 Validator 接口,但我不确定这是正确的方法。
为了方便起见,我把它分开了 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;
}
}
在我的控制器中,我必须放这样的东西:
@Inject
FormCreator formCreator = new FormCreator();
虽然我花了几个小时才发现这一点,但这个问题已经解决了。
另一个是,无论我做什么,方法 bindFromRequest() 总是 returns null,并且不是,因为 eclipse 和其他任何东西,这是因为我从这个工厂创建的表单中调用它。
示例:
// never works
formCreator.getFactory().form(Diretor.class).bindFromRequest();
// works fine, but is deprecated
Form.form(Diretor.class).bindFromRequest();
使用什么最新方法来代替这个已弃用的方法?
提前致谢。
您不必初始化 FormFactory
,请尝试以下操作:
@Inject FormFactory formFactory;
然后在您的操作中调用:
formFactory.form(Director.class).bindFromRequest();
Play 2.5.x 文档指出:
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);
所以第一步是将 FormFactory
注入您的控制器。你这样做:
package controllers;
import play.*;
import play.mvc.*;
public class Application extends Controller {
@Inject FormFactory formFactory;
...
}
然后在处理表单提交请求的操作中,您可以像这样使用 FormFactory
:
public Result handleFormSubmission() {
formFactory.form(Director.class).bindFromRequest();
}
不幸的是,上面的答案对我不起作用。这是我想出的,它适用于我的项目。
import play.mvc.*;
import play.*;
import views.html.logins.*;
import models.User;
import play.data.Form;
import play.data.FormFactory;
import javax.inject.Inject;
public class Login extends Controller{
private static Form<User> loginForm;
@Inject
public Login(FormFactory formFactory) {
this.loginForm = formFactory.form(User.class);
}
public Result list() {
return TODO;
}
}
public class UserController extends Controller {
@Inject
FormFactory formFactory;
public Result loginProcess(){
Form<LoginForm> loginForm = formFactory.form(LoginForm.class).bindFromRequest();
...
}
}
那里不需要登录构造函数..请参阅我的示例
我花了很多时间尝试解决这个问题,并寻找替代方法,但没有成功。
首先,玩!要求我使用和注入 FormFactory(在 https://www.playframework.com/documentation/2.5.0/JavaForms 中有解释)。
但是为了实例化这个 FormFactory,我必须为其构造函数传递 3 个参数,即 MessagesApi、Formatters 和 Validator。包括,我必须实例化 Validator 接口,但我不确定这是正确的方法。
为了方便起见,我把它分开了 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;
}
}
在我的控制器中,我必须放这样的东西:
@Inject
FormCreator formCreator = new FormCreator();
虽然我花了几个小时才发现这一点,但这个问题已经解决了。
另一个是,无论我做什么,方法 bindFromRequest() 总是 returns null,并且不是,因为 eclipse 和其他任何东西,这是因为我从这个工厂创建的表单中调用它。
示例:
// never works
formCreator.getFactory().form(Diretor.class).bindFromRequest();
// works fine, but is deprecated
Form.form(Diretor.class).bindFromRequest();
使用什么最新方法来代替这个已弃用的方法?
提前致谢。
您不必初始化 FormFactory
,请尝试以下操作:
@Inject FormFactory formFactory;
然后在您的操作中调用:
formFactory.form(Director.class).bindFromRequest();
Play 2.5.x 文档指出:
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);
所以第一步是将 FormFactory
注入您的控制器。你这样做:
package controllers;
import play.*;
import play.mvc.*;
public class Application extends Controller {
@Inject FormFactory formFactory;
...
}
然后在处理表单提交请求的操作中,您可以像这样使用 FormFactory
:
public Result handleFormSubmission() {
formFactory.form(Director.class).bindFromRequest();
}
不幸的是,上面的答案对我不起作用。这是我想出的,它适用于我的项目。
import play.mvc.*;
import play.*;
import views.html.logins.*;
import models.User;
import play.data.Form;
import play.data.FormFactory;
import javax.inject.Inject;
public class Login extends Controller{
private static Form<User> loginForm;
@Inject
public Login(FormFactory formFactory) {
this.loginForm = formFactory.form(User.class);
}
public Result list() {
return TODO;
}
}
public class UserController extends Controller {
@Inject
FormFactory formFactory;
public Result loginProcess(){
Form<LoginForm> loginForm = formFactory.form(LoginForm.class).bindFromRequest();
...
}
}
那里不需要登录构造函数..请参阅我的示例