【问题标题】:Spring Encoding with CharacterEncodingFilter in web.xml在 web.xml 中使用 CharacterEncodingFilter 进行 Spring 编码
【发布时间】:2011-07-23 14:45:42
【问题描述】:

stackoverflow.com 上的编码

{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþŸ▲►▼◄■□▣▤▥▦▧▨▩▪▫◊○●☺☻☼€¢£¥¤♀♂♂♠♤♣♧♥""♡♦★☆⌂№☎☏♨☜☞♩♪♫♬♭†‡←↑→↓↔↕↖↗↘↙×÷+-Ω√¼½¾⅓⅔⅛⅜⅜⅝%‰¹²³

在我的网站上结束编码:

{|}~???????????????????????????¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþ?????¦???????????????¤?¢£¥¤????????""????¦???????????????????????×÷+-Ov¼½¾??????%?¹²³

解决方案:

<#ftl attributes={"content_type":"text/html"} encoding="UTF-8"/>

并将其放入我的 HttpsCoookieFilter:

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

显然我(ab)使用 HttpServlet 而不是 Freemarker 来使用 out.write() 生成 HTML 内容,所以我添加了上述内容。

这里是 servlet 源代码。任何有关如何更改它的提示都非常受欢迎:

public class HttpsCookieFilter implements Filter {
private static Logger log = Logger.getLogger(HttpsCookieFilter.class);

@Override
public void destroy() {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    final HttpServletRequest req = (HttpServletRequest) request;
    final HttpServletResponse res = (HttpServletResponse) response;

        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html; charset=UTF-8");

    final HttpSession session = req.getSession(false);

    if (session != null) {
        setCookie(req, res);
    }
    try {
        chain.doFilter(req, res);
    } catch (IllegalStateException e) {
        log.warn("HttpsCookieFilter redirect problem! ", e);
    }
}

@Override
public void init(FilterConfig arg0) throws ServletException {
}

private void setCookie(HttpServletRequest request, HttpServletResponse response) {
    Cookie cookie = new Cookie("JSESSIONID", request.getSession(false).getId());
    cookie.setMaxAge(-1);
    cookie.setPath(getCookiePath(request));
    cookie.setSecure(false);
    response.addCookie(cookie);
}

private String getCookiePath(HttpServletRequest request) {
    String contextPath = request.getContextPath();
    return contextPath.length() > 0 ? contextPath : "/";
}
}

现在 UTF-8 无处不在 ;) 谢谢 BalusC !!!

【问题讨论】:

  • 显然你并没有一直使用 UTF-8,否则一切都会好起来的。 :)

标签: java spring utf-8 character-encoding


【解决方案1】:

问号通常在字符到字节转换器/写入器本身知道字符集时使用字符必须被解码为。如果解码字符集不支持原始编码中的特定字符,则将其转换为问号。

在具有数据库后端的普通 Web 应用程序中,有两个地方会发生这种情况:

  1. 当用户提交的数据即将在数据库中插入/更新时。
  2. 当 HTTP 响应正文即将被写入并发送到客户端时。

在这两种情况下,都使用了只理解字节的 TCP/IP 网络,并且服务器和客户端通常都知道双方使用的字符集。在所有其他情况下,您会看到 Mojibake

要涵盖第一种情况,您需要确保将数据库和表配置为使用 UTF-8。您通常在CREATE 期间指定。这是一个 MySQL 方言的例子。

CREATE DATABASE db_name CHARACTER SET utf8;
CREATE TABLE tbl_name (...) CHARACTER SET utf8;

对于一些 JDBC 驱动程序,例如 MySQL 驱动程序,您需要指示驱动程序本身使用 UTF-8。

jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8

要涵盖第二种情况,您需要确保指示响应编写器使用 UTF-8 将字符解码为字节。当使用 JSP 作为视图时,只需将以下内容添加到 每个 JSP 页面(包括包含)的顶部就足够了(它不仅设置响应编码,还隐式设置正确的响应标头) .

<%@ page pageEncoding="UTF-8" %>

另见:


对于您当前使用的 Spring 字符编码过滤器,它只设置请求编码,以便您可以确保提交的数据被解释为 UTF-8。它基本上所做的一切如下:

request.setCharacterEncoding("UTF-8");

仅此而已。请注意,这仅涵盖 POST 请求,对于 GET 请求,您仍需要配置网络服务器以将 URL 解释为 UTF-8。

【讨论】:

    【解决方案2】:

    在您的调度程序 servlet 上下文 xml 中,您必须添加一个属性 "&lt;property name="contentType" value="text/html;charset=UTF-8" /&gt;" 在您的 viewResolver bean 上。 我们正在使用 freemarker 进行视图。

    看起来像这样:

    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
           ...
           <property name="contentType" value="text/html;charset=UTF-8" />
           ...
    </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2015-07-27
      • 2023-03-26
      • 2011-08-07
      • 2014-01-18
      • 2012-03-07
      • 2016-05-16
      相关资源
      最近更新 更多