【问题标题】:Play Framework: Processing POST params?播放框架:处理 POST 参数?
【发布时间】:2014-09-11 16:00:57
【问题描述】:

PlayFramwork 新手,遇到了障碍。 @https://www.playframework.com/documentation/2.3.x/JavaForms 上的教程显示了通过将表单输入绑定到类来处理表单。但是,我想处理 HTML 表单而不将我的字段绑定到类。

我的表单示例:

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

我希望能够像这样在我的操作中处理这个:

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

我试过了

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

这样做,我只能访问这样的值

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

但我明白了

[Ljava.lang.String;@4d36e231

求助!!!!!!

【问题讨论】:

    标签: playframework playframework-2.0


    【解决方案1】:

    变量 values 包含一个映射,其中值是一个字符串数组。所以使用 get 方法会返回一个数组。要访问参数的值,请查看它的第一个元素。

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

    我还注意到您要上传文件。不要忘记将表单的 enctype 更改为:

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

    您使用以下方式访问 POST 数据:

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

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 1970-01-01
      • 2012-06-02
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多