首页 > 代码库 > (转)spring mvc 中文乱码问题解决

(转)spring mvc 中文乱码问题解决

在eclipse环境里,页面传输数据的时候通常用ISO-8859-1这个字符集
可以用 str = new String(str.getBytes("ISO-8859-1"), "GBK"); 转码
str是你页面得到的字符串,后面的表示从"ISO-8859-1"转到"GBK",或者你转成别的也可以

16:35 浏览 (1550) 评论 (2) 分类: 技术 收藏 相关推荐 评论
chenlb 2007-09-09 回复 
不好意思多写了 
Java代码 
<filter>  
  <filter-name>Set Character Encoding</filter-name>  
  <filter-class>filters.SetCharacterEncodingFilter</filter-class>  
  <init-param>  
  <param-name>encoding</param-name>  
  <param-value>utf8</param-value>  
  </init-param>  
</filter>  
 <filter-mapping>  
  <filter-name>Set Character Encoding</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  

<filter>  
  <filter-name>Set Character Encoding</filter-name>  
  <filter-class>filters.SetCharacterEncodingFilter</filter-class>  
  <init-param>  
  <param-name>encoding</param-name>  
  <param-value>utf8</param-value>  
  </init-param>  
</filter>  
 <filter-mapping>  
  <filter-name>Set Character Encoding</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  
chenlb 2007-09-09 回复 
如果你这样str = new String(str.getBytes("ISO-8859-1"), "GBK"); 转码。 写出来程序通用性不好,而且每次有中文都要这样,用过虑器好。但只对post方式有效。get方式看, http://www.blogjava.net/chenlb/archive/2007/07/17/130922.html 

如果是spring 

用spring提供的过虑器,在web.xml里加 
Java代码 
<filter>  
  <filter-name>Set Character Encoding</filter-name>  
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  <init-param>  
  <param-name>encoding</param-name>  
  <param-value>utf8</param-value>  
  </init-param>  
</filter>  
 <filter-mapping>  
  <filter-name>Set Character Encoding</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  

 <filter>
  <filter-name>Set Character Encoding</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>utf8</param-value>
  </init-param>
 </filter>
  <filter-mapping>
  <filter-name>Set Character Encoding</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>


如没用spring,用tomcat里面的例子(在webapps\servlets-examples\WEB-INF\classes\里把filters目录放到你的classer\下,其实只要SetCharacterEncodingFilter就可以了,同样在web.xml里加 
Java代码 
<filter>  
  <filter-name>Set Character Encoding</filter-name>  
  <filter-class>filters.SetCharacterEncodingFilter</filter-class>  
  <init-param>  
  <param-name>encoding</param-name>  
  <param-value>utf8</param-value>  
  </init-param>  
</filter>  
 <filter-mapping>  
  <filter-name>Set Character Encoding</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  
http://xhy0422.javaeye.com/blog/51084

(转)spring mvc 中文乱码问题解决