【问题标题】:Passing UTF-8 string to f:viewParam in JSF将 UTF-8 字符串传递给 JSF 中的 f:viewParam
【发布时间】:2012-03-06 08:39:31
【问题描述】:

我尝试将 UTF-8 字符串作为 f:viewParam 值传递,但值显示为垃圾字符串,我在 web.xml 中添加了 EncodingFilter 以将 UTF-8 设置为请求和响应,如下所示

HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request = (HttpServletRequest) servletRequest;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

我用这种风格定义了facelet页面,但问题没有解决

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
<f:metadata>
    <f:viewParam name="q" value="#{searchBean.query}"/>
</f:metadata>
.....
</html>

我用 Myfaces 2.0.5 和 Mojarra 2.0.5 对此进行了测试

【问题讨论】:

    标签: jsf-2 character-encoding mojibake viewparams


    【解决方案1】:

    显然,您使用的服务器配置默认使用不同的字符编码来解释 GET 查询字符串。例如,Tomcat 默认将它们解释为 ISO-8859-1。需要打开Tomcat的/conf/server.xml,在&lt;Connector&gt;元素中添加URIEncoding属性,值为UTF-8。

    <Connector ... URIEncoding="UTF-8">
    

    顺便说一句,那个过滤器是完全没有必要的。去掉它。 Facelets 上的 JSF 2.x 已经在所有级别默认为 UTF-8。此外,HttpServletRequest#setCharacterEncoding() 不影响 GET 请求,它只影响 POST 请求。

    另见:

    【讨论】:

      猜你喜欢
      • 2011-05-10
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      相关资源
      最近更新 更多