10

I tried lots of thing and could not understand why i am getting ? instead accented character.

I'm using on my html:

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

and my controller has the following code

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

how do I fix that?

Thanks

Luis Felipe Perez
  • 339
  • 1
  • 4
  • 18
  • Could you possibly get the right characters, except it's just that when you print it to the console, your console doesn't support utf-8 ? Try this: http://stackoverflow.com/questions/2038733/how-do-i-change-a-shell-scripts-character-encoding – gerrytan Feb 19 '13 at 03:38
  • It this a post or a get request? Can you add the actual method with the actual annotations? – Haim Raman Feb 19 '13 at 07:37
  • It is Post request and that is the code: RequestMapping(value = "/userSignup", method = RequestMethod.POST) public String userSignup( RequestParam ("name") String name, RequestParam ("email") String email, RequestParam ("birthdayDay") String birthdayDay, RequestParam ("birthdayMonth") String birthdayMonth, RequestParam ("birthdayYear") String birthdayYear, RequestParam ("sex") String sex, RequestParam ("password") String password, RequestParam ("passwordConfirmation") String passwordConfirmation,HttpServletResponse response,ModelMap model) { – Luis Felipe Perez Feb 20 '13 at 01:38

2 Answers2

10

I could fix this issue by adding the following code on my master template:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
Luis Felipe Perez
  • 339
  • 1
  • 4
  • 18
0

Accented letter and other languages like Mandarin and Arabic are tricky.
I guess you have not seen the last of this issue.
You should make sure that you keep your text encoded correctly in any link in the chain.

E.g.

  • database ->java ->response->browser
  • properties file ->jav-> response->browser
  • request (param/form) ->response->browser
  • java -> logger-> console

I suggest following this great answer How to get UTF-8 working in Java webapps?

To adjust it to spring use spring’s 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>

If you are using i18n make sure to define the default Encoding for your ResourceBundleMessageSource e.g.

<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>

Make sure your properties files are encoded correctly using Java’s native2ascii.
Or better if you use eclipse I recommend Property Editor plugin.
Sometime third-party framework break something and you need to do the “magical”

new String(yourstring, "UTF8")

For more information see here

Note that some view resolvers need to defined encoding as well.

Community
  • 1
  • 1
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • One more potential issue is the encoding of the log4j appender. Add the following to you log4j.properties file log4j.appender.emsSpringLogFile.encoding=UTF-8 – Haim Raman Feb 24 '13 at 06:33