iNikkz iNikkz - 6 months ago 328
Java Question

Play Framework 2.5 Form submit using POST method Error

I have new to Play Framework 2.5. I am using Play in Java. Created a normal HTML form and want to submit the form using POST method but it throws error. I googled a lot but not succeed.

routes:

POST /output controllers.HomeController.result


HTML Code:

<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>


Controller:

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();
}

}


Error:

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


I want to use normal HTML form to submit request using POST method. Any help please ?

Answer

I have the solution of my answer.

We can get the form post data in Play framework 2.5 by

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

and then get the form name parameter value by:

form_values.get("<key_name>")[0];