Spring MVC + Hibernate编码问题
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 JSP 文件必须是 UTF-8,有两种方式:
<%@page pageEncoding="UTF-8" %>
右键单击 > eclipse中的properties
并将其编码设置为UTF-8(它可能会拒绝转换它,因此剪切当前内容,更改编码,然后将其粘贴回来)然后spring有一个
CharacterEncodingFilter
对于这种情况,不应该有任何显着的性能影响:
实际上,任何过滤器都不应该有显着的性能影响,除非其
doFilter()
方法被声明为synchronized
。Your JSP files must be UTF-8 in two ways:
<%@page pageEncoding="UTF-8" %>
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:Actually, no filter should have a significant performance hit, unless its
doFilter()
method is declaredsynchronized
.这是你进行正确编码转换的代价:UTF-8(请求)-> UTF-16(Java)-> UTF-8 (DB)。
如果它(转换)花费的时间超过整个请求时间的百分之几,我会感到惊讶。如果确实如此,恕我直言,有些事情做错了。
您的容器是否将 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.
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.