blee908
blee908

Reputation: 12555

Play Framework: Processing POST params?

New to PlayFramwork and running into a roadblock. The tutorial over @ https://www.playframework.com/documentation/2.3.x/JavaForms shows processing forms by binding the form inputs to a class. However, I want to process the HTML form without binding my fields to a class.

Example of my form:

<form method="POST" action="form/submit">
<input type="file" name="slider[1][file]" class="thumbnailUpload">
<input type="text" value="http://www.tapiture.com/shop/collection1" name="slider[1][url]" class="form-control">
<input type="file" name="slider[2][file]" class="thumbnailUpload">
<input type="text" value="http://www.tapiture.com/shop/collection1" name="slider[3][url]" class="form-control">
<input type="file" name="slider[2][file]" class="thumbnailUpload">
<input type="text" value="http://www.tapiture.com/shop/collection1" name="slider[3][url]" class="form-control">
</form>

I want to be able to process this in my action like so:

String url = form.get("slider")[0]["url"]

I've tried

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

And doing this, I can only access the values like so

value.get("slider[1][url]")

But I get

[Ljava.lang.String;@4d36e231

HELP!!!!!!

Upvotes: 0

Views: 329

Answers (1)

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

The variable values contains a map where values are an array of strings. So using a get method returns an array. To access a value of a parameter look into a first element of it.

String value = values.get("slider[1][url]")[0];

I've also noticed that you're going to upload files. Don't forget that after changing form's enctype to:

<form method="POST" action="form/submit" enctype="multipart/form-data">

you access POST data with:

final Map<String, String[]> values = body.asMultipartFormData().asFormUrlEncoded();

Upvotes: 1

Related Questions