gpt4 book ai didi

Java Spring Hibernate - 没有来自 HTML 表单的波兰语字母

转载 作者:行者123 更新时间:2023-12-04 16:06:39 25 4
gpt4 key购买 nike

我正在学习 Java,在编写我的第一个网络应用程序时,我遇到了一些波兰语字母(例如 ą、ć、ł、ś 等)的问题。问题是,当我在 GET 中将对象绑定(bind)到表单时,它在浏览器中显示正常,所有波兰语字母都很好(数据库配置正确),但是在我的 Controller 页面上点击“发送”按钮后,在 POST 中,我得到缺少波兰语字母的乱码文本。当我在 Controller 中将 View (JSP 文件)中的编码设置为 utf-8 而不是“±”时,我得到“Ä”(两个字节)并且编码设置为 iso8895-2 我得到“±”(1 个字节)。使用 servlet 解决方案是添加

request.setCharacterEncoding("8859_2");

作为 POST 的第一行,但 Hibernate 不使用 HttpServletRequest,所以即使我添加它,我仍然得到垃圾。 STS(我的 IDE)设置为 UTF-8。

他的有什么解决办法吗?

要求的浏览器日志:

Request URL:http://localhost:8080/Project/register
Request Method:POST
Status Code:200
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Content-Language:pl-PL
Content-Length:3338
Content-Type:text/html;charset=UTF-8
Date:Mon, 29 Jan 2018 11:30:04 GMT
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:58
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=88145A5FCBBD13FDBE3C288110B38187
DNT:1
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/Project/register
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.102 Safari/537.36 Vivaldi/1.94.971.8
Form Data
view source
view URL encoded
username:ąąą
email:
age:0
phone:0
password:

点击查看 URL 编码后:

%C4%85%C4%85%C4%85

最佳答案

您应该为您的页面设置一个内容类型 header ,然后它用于发送 POST 请求。

参见 Setting the HTTP charset parameter :

Documents transmitted with HTTP that are of type text, such as text/html, text/plain, etc., can send a charset parameter in the HTTP header to specify the character encoding of the document.

It is very important to always label Web documents explicitly. HTTP 1.1 says that the default charset is ISO-8859-1. But there are too many unlabeled documents in other encodings, so browsers use the reader's preferred encoding when there is no explicit charset parameter.

The line in the HTTP header typically looks like this:

Content-Type: text/html; charset=utf-8

In theory, any character encoding that has been registered with IANA can be used, but there is no browser that understands all of them. The more widely a character encoding is used, the better the chance that a browser will understand it. A Unicode encoding such as UTF-8 is a good choice for a number of reasons.


我不确定特定的内容类型代码是否可以解决页面通过 HTTP 传输时的编码问题。但应为请求设置正确的代码。

Header set Content-Type "text/html; charset=iso-8859-2" 

UTF-8是ASCII字符集之外的字符通用字符编码。服务器默认配置为使用 UTF-8。

关于Java Spring Hibernate - 没有来自 HTML 表单的波兰语字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com