Play Framework - 在没有模型的情况下创建表单

时间:2016-11-23 05:29:04

标签: java playframework

是否可以在Play Framework 2.5中创建没有模型的表单?

我尝试在控制器中添加代码:

package forms;

import play.data.validation.Constraints;
import play.data.Form;


public class UserForm extends Form{
    @Constraints.Required
    @Constraints.Email
    public String email;
}

然后在Controller:

public Result cabinet() {
        Form<UserForm> userform = formFactory.form(UserForm.class);
        return ok(payment.render(userform)); }

最后,在视图中:

@import forms.UserForm
@(form: Form[UserForm])

@import b3.vertical.fieldConstructor  // Declares a vertical field constructor as default

@cabinet() {
        @b3.form(routes.HomeController.landing) {
        @b3.email( form("email"), '_label -> "Email", 'placeholder -> "example@mail.com" )
        @b3.submit('class -> "btn btn-default"){ Sign in }
    }
}

当我移动到测试网址时,我遇到了一个空的异常错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.NullPointerException]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
    at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
    at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
    at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: java.lang.NullPointerException
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
    at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
    at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
    at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
    at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
    at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: java.lang.NullPointerException: null
    at controllers.HomeController.cabinet(HomeController.java:38)
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$5$$anonfun$apply$5.apply(Routes.scala:199)
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$5$$anonfun$apply$5.apply(Routes.scala:199)
    at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:157)
    at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:156)
    at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:136)
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:73)
    at play.http.HttpRequestHandler$1.call(HttpRequestHandler.java:54)
    at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
    at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)

如何正确创作?

更新 问题出在FormFactory中 - 这应该明确定义:

@Inject
public HomeController(FormFactory formFactory) {
    this.formFactory = formFactory;
}

这件事解决了NullException问题

1 个答案:

答案 0 :(得分:1)

实际上取决于形式,形式是什么类型?

  1. 您想验证数据库中的内容吗?
  2. 如果要检查数据库中的值等,则需要模型
  3. 模型是业务逻辑被验证或执行的地方,并返回给MVC中的控制器。
  4. 对于一个简单的表单,只是为了测试一些您不需要模型的示例用户名和密码,您可以使用框架中的控制器和视图来完成它们!

    请关注此video,在此视频中,作者在模型中使用Users.java。这是最好的方式,当你想建立一个大型网站时,它是如何工作的。

    但是,在您的情况下,您可以使用DynamicForm在控制器中实现此功能,请查看此post以获取更多详细信息。