2013-02-19 66 views
7

我试了很多东西,不明白为什么我越来越多?而不是重音字符。获取问号而不是重音信使用春天MVC 3

我使用我的html:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

和我的控制器具有下面的代码

@RequestParam ("name") String name 
name = name.trim(); 
system.out.println(name); 
//response t?ata 
//expected tábata 

我该如何解决呢?

感谢

+0

你可能会得到正确的字符,除了它只是当你打印到控制台时,你的控制台不支持utf-8?试试这个:http://stackoverflow.com/questions/2038733/how-do-i-change-a-shell-scripts-character-encoding – gerrytan 2013-02-19 03:38:00

+0

这是一篇文章或获取请求?你可以添加实际的方法与实际的注释? – 2013-02-19 07:37:12

+0

它是Post请求,代码是: RequestMapping(value =“/ userSignup”,method = RequestMethod。POST) \t public String userSignup(\t RequestParam(“name”)String name,RequestParam(“email”)String email,RequestParam(“birthdayDay”)String birthdayDay,RequestParam(“birthdayMonth”)String birthdayMonth,RequestParam(“birthdayYear” )字符串birthdayYear,RequestParam(“性别”)字符串性,RequestParam(“密码”)字符串密码,RequestParam(“passwordConfirmation”)字符串passwordConfirmation,HttpServletResponse响应,ModelMap模型){ – 2013-02-20 01:38:53

回答

8

我可以添加以下代码,在我的主模板解决这个问题:

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

重音字母和其他语言,如汉语和阿拉伯语是棘手。
我想你还没有看到这个问题的最后一个。
您应该确保您的文本在链中的任何链接上保持正确编码。

E.g.

  • 数据库 - >的java - >响应 - >浏览器
  • 属性文件 - > jav->响应 - >浏览器
  • 请求(PARAM /表格) - >响应 - >浏览器
  • 的java - > logger->控制台

我建议您按照这个伟大的答案 How to get UTF-8 working in Java webapps?

要调整它SPRI ng使用spring的CharacterEncodingFilter。

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

如果您使用的国际化确保例如定义的默认编码为您ResourceBundleMessageSource会

<bean id="messageSource" 
class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
    > 
     <property name="basename" value="classpath:messages"/> 
     <property name="defaultEncoding" value="UTF-8"/> 
     <property name="useCodeAsDefaultMessage" value="false"/> 
</bean> 

确保您的属性文件使用Java的native2ascii正确编码。
或者,如果你使用Eclipse,我建议Property Editor plugin.
有时第三方框架打破东西,你需要做的“神奇”

new String(yourstring, "UTF8") 

欲了解更多信息,请参阅here

注意更好一些视图解析器还需要定义编码。

+0

我会检查了这一点。谢谢Haim! – 2013-02-21 20:44:56

+0

另一个潜在的问题是log4j appender的编码。 将以下内容添加到您的log4j.properties文件log4j.appender.emsSpringLogFile.encoding = UTF-8 – 2013-02-24 06:33:22