Spring MVC + Hibernate编码问题

发布于 2024-08-26 10:25:53 字数 1271 浏览 3 评论 0原文

我使用 Spring MVC + Hibernate 应用程序,使用 MySQL(版本 5.0.51a)和 InnoDB 引擎。

当我发送带有西里尔字符的表单时出现问题。 结果,数据库包含未知编码的无意义字符。

所有 JSP 页面、数据库(+ 表和字段)均使用 UTF-8 创建。 Hibernate 配置还包含将编码设置为 UTF-8 的属性。

我通过创建使用 UTF-8 对请求内容进行编码的过滤器解决了这个问题。 示例代码:

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

但它明显减慢了应用程序的速度。

有趣的是,直接从应用程序执行插入查询(即从 Eclipse 作为 Java 应用程序运行)效果非常好。

UPD。

据我了解,使用过滤器是我的案例中唯一可行的解​​决方案。

我不知道标准的CharacterEncodingFilter。现在就使用,效果非常好!

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

I work on Spring MVC + Hibernate application, use MySQL (ver. 5.0.51a) with the InnoDB engine.

The problem appears when I am sending a form with cyrillic characters.
As the result, database contains senseless chars in unknown encoding.

All the JSP pages, database (+ tables and fields) created using UTF-8.
Hibernate config also contains property which sets encoding to UTF-8.

I had solved this by creating filter which encodes request content with UTF-8.
Exemplary code:

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

But it visibly slows down the app.

The interesting thing is that executing insert query directly from the app (i.e. running from Eclipse as Java Application) works perfect.

UPD.

As far as I understood, using filter is the only working solution in my case.

I didn't know about standard CharacterEncodingFilter. Use it now, works very well!

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-09-02 10:25:53

您的 JSP 文件必须是 UTF-8,有两种方式:

  • 标头 <%@page pageEncoding="UTF-8" %>
  • 其内容 - 右键单击 > eclipse中的properties并将其编码设置为UTF-8(它可能会拒绝转换它,因此剪切当前内容,更改编码,然后将其粘贴回来)

然后spring有一个CharacterEncodingFilter对于这种情况,不应该有任何显着的性能影响:

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

实际上,任何过滤器都不应该有显着的性能影响,除非其 doFilter() 方法被声明为 synchronized

Your JSP files must be UTF-8 in two ways:

  • the header <%@page pageEncoding="UTF-8" %>
  • their content - right-click > properties in eclipse and make their encoding UTF-8 there (it may refuse to convert it, so cut the current content, change the encoding, and then paste it back)

Then spring has a CharacterEncodingFilter for this case, which shouldn't have any significant performance hit:

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

Actually, no filter should have a significant performance hit, unless its doFilter() method is declared synchronized.

ま柒月 2024-09-02 10:25:53

这是你进行正确编码转换的代价:UTF-8(请求)-> UTF-16(Java)-> UTF-8 (DB)。

如果它(转换)花费的时间超过整个请求时间的百分之几,我会感到惊讶。如果确实如此,恕我直言,有些事情做错了。

所有 JSP 页面...使用 UTF-8 创建

您的容器是否将 UTF-8 设置为页面编码? (请参阅生成的页面属性)。如果是,则无需设置过滤器,浏览器将以 UTF-8 格式提交表单。

It's your price for doing the correct encoding transformation: UTF-8 (request)-> UTF-16 (Java) -> UTF-8 (DB).

I would be surprised though if it (transformation) took more than a few percent of overall request time. If it does, something is done wrong IMHO.

All the JSP pages ... created using UTF-8

Does your container set UTF-8 as page encoding? (See generated page properties). If it does, you don't need to set filter, the browser would submit the form back in UTF-8.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文