如何摆脱警告:PWC4011:无法将请求字符编码设置为UTF-8 [英] How to get rid of WARNING: PWC4011: Unable to set request character encoding to UTF-8

查看:429
本文介绍了如何摆脱警告:PWC4011:无法将请求字符编码设置为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在GlassFish 3.1上使用的,它在Mojarra上使用PrimeFaces,并与MyFaces CODI一起腌制.几乎在每个请求上都会显示以下消息:

This is on GlassFish 3.1, using PrimeFaces over Mojarra and salted with MyFaces CODI. On just about every request the following message appears:

警告:PWC4011:无法从上下文/com.myapp_war_0.1将请求字符编码设置为UTF-8,因为已经读取了请求参数,或者已经调用了ServletRequest.getReader()

WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /com.myapp_war_0.1, because request parameters have already been read, or ServletRequest.getReader() has already been called

自从我开始这个项目以来,这种情况就发生了-到目前为止,我一直无视它,但是现在我意识到我在浪费大量时间阅读它.我在此处找到了一个有趣但不完整的解决方法,但我不理解.

This has happened ever since I started the project -- so far I have been ignoring it but now I have realized I am wasting a lot of time reading around it. I found an interesting but incomplete work-around here, but I don't understand it.

有人可以建议如何在不抑制其他可能的警告消息的情况下篡改此消息吗?

Can someone suggest how to tamp down this message without suppressing other possible warning messages?

推荐答案

JSF/Facelets默认使用UTF-8解码HTTP请求参数. GlassFish本身默认使用ISO-8859-1来解码HTTP请求参数. HTTP请求参数只能解析和解码一次,并且每次代码第一次请求请求参数时都会发生这种情况,例如request.getParameter("name").因此,如果在 之前第一次请求请求参数,则JSF将请求参数编码设置为UTF-8,那么它将(错误地)使用ISO-8859-1进行解析.

JSF/Facelets uses by default UTF-8 to decode HTTP request parameters. GlassFish itself uses by default ISO-8859-1 do decode HTTP request parameters. HTTP request parameters can be parsed and decoded only once and this happens whenever a request parameter is requested by the code for the first time like so request.getParameter("name"). So, if a request parameter is requested for the first time before JSF has set the request parameter encoding to UTF-8, then it will (incorrectly) be parsed using ISO-8859-1.

当JSF需要在还原视图阶段按如下方式设置请求参数编码时,

When JSF needs to set the request parameter encoding during restore view phase as follows,

request.setCharacterEncoding("UTF-8");

当请求参数已被解析时,GlassFish将准确显示此警告.

while the request parameters are already parsed, then GlassFish will show exactly this warning.

不想要的结果是,所有这些HTTP请求参数都可能以 Mojibake 结尾.表单数据最初是使用UTF-8提交和编码的.如果使用其他字符集(如ISO-8859-1)对UTF-8数据进行解码,则8位及更高范围内的字符(通常是éàö等那些特殊字符"等将会损坏,并以éàö等结尾.

The unwanted consequence is, all those HTTP request parameters may possibly end up in Mojibake. The form data is originally submitted and encoded using UTF-8. If you decode UTF-8 data using a different charset like ISO-8859-1, then the characters in 8-bit range and beyond (usually, it are those "special characters" like é, à, ö, etc. will be corrupted and end up in é, à, ö, etc.

从技术上讲,正确的解决方案是在JSF设置正确的编码之前请求HTTP请求参数.基本上,您需要检查在JSF的还原视图阶段之前运行的所有代码,例如servlet过滤器,阶段侦听器等(如果没有这样做的话).

Technically, the right solution is to not request a HTTP request parameter before JSF has set the right encoding. You basically need to check all the code which runs before JSF's restore view phase, such as servlet filters, phase listeners, etc if they aren't doing that.

如果您似乎找不到它,或者代码超出了您的控制范围,那么您可以告诉GlassFish改用UTF-8解码HTTP请求参数,这样在JSF时就不需要更改它想得到他们.您可以通过在/WEB-INF/glassfish-web.xml文件的<glassfish-web-app>中添加以下条目来做到这一点:

If you can't seem to find it, or the code is beyond your control, then you can tell GlassFish to use UTF-8 instead to decode HTTP request parameters, so that it doesn't need to be changed when JSF want to get them. You can do that by adding the following entry to the <glassfish-web-app> of your /WEB-INF/glassfish-web.xml file:

<parameter-encoding default-charset="UTF-8"/>

(注意:文件和根条目以前分别称为sun-web.xml<sun-web-app>)

请注意,这是特定于GlassFish的,并且在将Web应用程序部署到其他服务器时,所有这些都将无法使用.与服务器无关的规范方法是创建一个 servlet过滤器,该过滤器基本上在doFilter()方法中完成以下工作:

Noted should be that this is specific to GlassFish and this all won't work when you deploy the webapp to a different server. The canonical server-independent approach is to create a servlet filter which does basically the following job in doFilter() method:

request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);

,并确保已将其映射到需要收集任何HTTP请求参数的任何其他过滤器之前.

and make sure that it's been mapped before any other filter which needs to collect any HTTP request parameters.

更新:关于GlassFish为何预先设置它的原因,可能是PrimeFaces引起的.另请参阅以下相关问题:通过PrimeFaces输入组件检索的Unicode输入已损坏.

Update: as to why GlassFish has set it beforehand, it's possibly caused by PrimeFaces. See also this related question: Unicode input retrieved via PrimeFaces input components become corrupted.

这篇关于如何摆脱警告:PWC4011:无法将请求字符编码设置为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆