Play Framework 2.5表单使用POST方法提交错误

时间:2016-05-26 09:14:03

标签: java forms post playframework-2.5

我是 Play Framework 2.5 的新手。我在 Java 中使用Play。创建了一个普通的 HTML 表单,并希望使用POST 方法提交表单,但它会引发错误。我google了很多但没有成功。

路线:

POST     /output     controllers.HomeController.result

HTML代码:

    <form name="form1" action="/output" method="post">
        <div id="content">
            <label>Enter Sentence/Paragraph.</label></br>
            <textarea id="para" name="para" rows="15" cols="100"></textarea></br>
            <input id="submit_btn" name="submit_btn" type="submit" value="Submit" />
        </div>
    </form>

控制器:

package controllers;

import com.google.inject.Inject;
import play.data.DynamicForm;
import play.data.FormFactory;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.index;
import views.html.output;.

public class HomeController extends Controller {

    @Inject
    FormFactory formFactory;

    public Result index() {
        return ok(index.render("NLP Pipeline."));
    }

    public Result result() {
        DynamicForm requestData = formFactory.form().bindFromRequest();
        System.out.println("form: " + requestData.get("para"));
        return ok();
    }

}

错误:

Unexpected exception
ProvisionException: Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.getMethod(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;
  at play.data.format.Formatters.<init>(Formatters.java:31)
  at play.data.format.FormattersModule.bindings(FormattersModule.java:18):
Binding(class play.data.format.Formatters to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating play.data.format.Formatters
    for parameter 1 at play.data.FormFactory.<init>(FormFactory.java:25)
  at play.data.FormFactoryModule.bindings(FormFactoryModule.java:17):
Binding(class play.data.FormFactory to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating play.data.FormFactory
    for field at controllers.HomeController.formFactory(HomeController.java:16)
  while locating controllers.HomeController
    for parameter 1 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

1 error

我想使用普通的 HTML表单来使用 POST 方法提交请求。有什么帮助吗?

2 个答案:

答案 0 :(得分:3)

为什么不在html代码中使用帮助器?使用

@import helper._

@form(my.controller.method.handling.post(param)) {
    <div id="content">
        <label>Enter Sentence/Paragraph.</label></br>
        <textarea id="para" name="para" rows="15" cols="100"></textarea></br>
        <input id="submit_btn" name="submit_btn" type="submit" value="Submit" />
    </div>
}

然后,在控制器中:

final Form<MyObject> form = formFactory.form(MyObject.class).bindFromRequest();

您只需创建一个表单的简单bean类(由Play提交数据填写):

public class MyObject {
    private String para;
    public String getPara() { return para; }
    public void setPara(String para) { this.para = para; }
}

更轻松地处理表单对象而不处理类型转换等等(请参阅https://www.playframework.com/documentation/2.5.x/JavaForms#Handling-form-submission上的播放文档)

答案 1 :(得分:2)

我有解答。

我们可以在 Play framework 2.5 中获取发布数据表单

if(isset($_POST['Edit']))

然后通过以下方式获取表单名称参数值

final Map<String, String[]> form_values = request().body().asFormUrlEncoded();