JSF中GET参数的UTF-8编码 [英] UTF-8 encoding of GET parameters in JSF

查看:161
本文介绍了JSF中GET参数的UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSF中有一个搜索表单,它使用RichFaces 4自动完成组件和以下的JSF 2页面和Java bean实现。我使用Tomcat 6& 7运行应用程序。

I have a search form in JSF that is implemented using a RichFaces 4 autocomplete component and the following JSF 2 page and Java bean. I use Tomcat 6 & 7 to run the application.

...
<h:commandButton value="#{msg.search}" styleClass="search-btn" action="#{autoCompletBean.doSearch}" />
...

在AutoCompleteBean

In the AutoCompleteBean

public String doSearch() {

   //some logic here
   return "/path/to/page/with/multiple_results?query=" + searchQuery + "&amp;faces-redirect=true";

}

只要使用searchQuery字符串是拉丁语-1,如果在拉丁语-1以外不工作。

This works well as long as everything withing the "searchQuery" String is in Latin-1, it does not work if is outside of Latin-1.

例如,搜索bodø将被自动编码为bod% F8。但是,搜索KraÐong将无法工作,因为它无法编码Ð。

For instance a search for "bodø" will be automatically encoded as "bod%F8". However a search for "Kra Ðong" will not work since it is unable to encode "Ð".

我现在尝试了几种不同的方法来解决这个问题,但没有一个

I have now tried several different approaches to solve this, but none of them works.


  • 我尝试使用URLEncode编码searchQuery我自己,但这只导致双重编码,因为%编码为%25

  • 我已经尝试使用java.net.URI来获取编码,但是给出与URLEncode相同的结果。

  • UTF-8在Tomcat中使用URIEncoding =UTF-8在连接器,但这只会恶化这个问题,因为那时非ascii字符不工作。

我的问题:


  1. 我可以更改JSF 2对GET参数进行编码的方式吗?

  2. 如果我不能改变JSF 2对GET参数进行编码的方式,我可以转换编码并手动进行吗?

  3. 我在这里奇怪吗?


推荐答案

我认为你在JSF中遇到了一个错误。查询字符串由 ExternalContext#encodeRedirectURL() ,它使用由 ExternalContext#getResponseCharacterEncoding() 。然而,虽然JSF默认情况下使用UTF-8作为响应字符编码,但这只是在视图实际被渲染时设置,而不是当响应被重定向时,所以响应字符编码仍然返回平台默认值 ISO-8859-1 这会导致您的字符使用此错误的编码进行网址编码。

I think you've hit a corner case bug in JSF. The query string is URL-encoded by ExternalContext#encodeRedirectURL() which uses the response character encoding as obtained by ExternalContext#getResponseCharacterEncoding(). However, while JSF by default uses UTF-8 as response character encoding, this is only been set if the view is actually to be rendered, not when the response is to be redirected, so the response character encoding still returns the platform default of ISO-8859-1 which causes your characters to be URL-encoded using this wrong encoding.

这作为问题2440 。同时,你最好的办法是明确地设置响应字符编码自己之前。

I've reported this as issue 2440. In the meanwhile your best bet is to explicitly set the response character encoding yourself beforehand.

FacesContext.getCurrentInstance().getExternalContext().setResponseCharacterEncoding("UTF-8");

请注意,这仍然需要容器本身使用相同的字符编码来解码请求网址,因此你肯定需要在Tomcat的配置中设置 URIEncoding =UTF-8。这不会弄乱字符,因为他们现在真的是UTF-8。

Note that this still requires that the container itself uses the same character encoding to decode the request URL, so you certainly need to set URIEncoding="UTF-8" in Tomcat's configuration. This won't mess up the characters anymore as they will be really UTF-8 now.

这篇关于JSF中GET参数的UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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