玩! Framework 2.5 形式编码

Play! Framework 2.5 form encoding

从 Play 升级后! 2.4 玩! 2.5 form-enconding 似乎有问题。例如,德国变音符号真的搞砸了。例如德语单词 "Bär" 变成 "Bär"。 我已经更改了从

访问表单数据的方式
Form.form(Form.class).bindFromRequest();

注入方式,如迁移指南中所述:

formFactory.form(Form.class).bindFromRequest();

是否可以修复 Play 2.5 中的字符编码?

在您的表单中尝试添加 accept-charset="ISO-8859-1"

<form  accept-charset="ISO-8859-1" action="whatever" method="post">

针对单词 Bär 进行了测试,它工作正常

编辑:

对于阿拉伯文字,字符也存储为 ISO-8859-1 编码。一张图显示最后一切顺利。

这是 play 2.5 中的错误,请参阅 github 问题:https://github.com/playframework/playframework/pull/5920

我用自定义正文解析器解决了这个问题:

   package com.kashi.ssff.services;

import akka.util.ByteString;
import play.api.http.HttpConfiguration;
import play.core.parsers.FormUrlEncodedParser;
import play.http.HttpErrorHandler;
import play.libs.F;
import play.libs.streams.Accumulator;
import play.mvc.BodyParser;
import play.mvc.BodyParsers;
import play.mvc.Http;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.Map;

/**
 * Created by mohsen on 3/21/16.
 */
public class FormBodyParser extends BodyParser.BufferingBodyParser<Map<String, String[]>> {
    private final HttpErrorHandler errorHandler;

    public FormBodyParser(long maxLength, HttpErrorHandler errorHandler) {
        super(maxLength, errorHandler, "Error parsing form");
        this.errorHandler = errorHandler;
    }

    @Inject
    public FormBodyParser(HttpConfiguration httpConfiguration, HttpErrorHandler errorHandler) {
        super(httpConfiguration, errorHandler, "Error parsing form");
        this.errorHandler = errorHandler;
    }

    @Override
    public Accumulator<ByteString, F.Either<Result, Map<String, String[]>>> apply(Http.RequestHeader request) {
        return BodyParsers.validateContentType(errorHandler, request, "Expected application/x-www-form-urlencoded",
                ct -> ct.equalsIgnoreCase("application/x-www-form-urlencoded"), super::apply);
    }

    @Override
    protected Map<String, String[]> parse(Http.RequestHeader request, ByteString bytes) throws Exception {
        String charset = request.charset().orElse("UTF-8");
        return FormUrlEncodedParser.parseAsJavaArrayValues(bytes.decodeString(charset), charset);
    }
}

并使用此主体解析器注释控制器:

    @BodyParser.Of(FormBodyParser.class)
    public Result register() {
.
.
.

特别感谢@GregMethvin