Getting a question mark instead of an accented letter using spring MVC 3

I tried many things and couldn’t understand why I get? instead an accented character.

I am using 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 can i fix this?

thanks

+7
source share
2 answers

I could fix this problem by adding the following code to my main template:

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

Accented writing and other languages ​​such as Mandarin and Arabic are complex.
I think you have not seen the last of these questions.
You must make sure that you correctly encode the text in any link in the chain.

eg.

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

I suggest following this wonderful answer. How to get UTF-8 to work in Java Webapps?

To configure it for 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, be sure to define the default encoding for your ResourceBundleMessageSource, for example.

 <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 property files are encoded correctly using Javas native2ascii.
Or better if you use eclipse, I recommend the property editor plugin.
Once upon a time, a third-party structure breaks something and you need to do the “magic”

 new String(yourstring, "UTF8") 

For more information see here .

Please note that some viewers also need to define an encoding.

0
source

All Articles