从Play升级后! 2.4玩! 2.5 form-enconding 似乎存在问题。例如德国的变形金刚真的搞砸了。例如,德语单词“Bär”变为“Bär”。 我已经改变了从
访问表单数据的方式Form.form(Form.class).bindFromRequest();
注入方式,如迁移指南中所述:
formFactory.form(Form.class).bindFromRequest();
是否有修复 Play 2.5 中的字符编码?
答案 0 :(得分:2)
这是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
答案 1 :(得分:0)