用Spring-MVC / Java保存中文字符 [英] Save Chinese characters with Spring-MVC / Java

查看:110
本文介绍了用Spring-MVC / Java保存中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在jsp上通过

设置contentType

 <%@ page contentType =text / html; charset = UTF-8%> 

我也在jsp中设置了这个标签:

 < META HTTP-EQUIV =content-typeCONTENT =text / html; charset = UTF-8/> 

然而,当我提交表单时,控制器会看到与我输入的字符不同的字符。



我正在输入角色我并看到æ??在控制器中。当数据重新显示在页面上时,它会显示相同的错误字符(<?>)。



为什么控制器没有得到正确的字符?

解决方案

在您的web.xml文件中声明一个CharacterEncodingFilter之前,任何其他过滤器

 <滤光器> 
< filter-name> charsetFilter< / filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter< / filter-class>
< init-param>
< / param-name>编码< / param-name>
< param-value> UTF-8< /参数值>
< / init-param>
< / filter>

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

在您的jsp文件中,尝试在文件的开头添加它:

 <%@ page language =javacontentType =text / html; charset = UTF-8pageEncoding =UTF-8%> ; 


I'm trying to save Chinese characters from a form submit into the database.

I've set the contentType on the jsp via

<%@ page contentType="text/html;charset=UTF-8" %>

I've also set this tag inside the of the jsp:

<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8" />

However, when I submit the form, my controller sees a different character than what I entered.

I am entering the character 我 and seeing æ?? in the controller. When the data redisplays on the page, it shows the same wrong character (æ??).

Why isn't the controller getting the correct character?

解决方案

Declare a CharacterEncodingFilter in your web.xml file before any other filter

<filter>
    <filter-name>charsetFilter</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>
</filter>

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

In your jsp file try adding this at the very start of the file:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

这篇关于用Spring-MVC / Java保存中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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