0

I'm developing a Spring MVC app and I'm having some issues with enconding.

Here's the problem: on the browser I type "João" and after I submit the form, on the Controller I've got "João". My Firefox is configured to UTF-8 and so my Eclispe. If I check the sent paramenters using Firebug the value still correct.

Here's my encoding config:

On the web.xml, the first thing I have:

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

<locale-encoding-mapping-list>
    <locale-encoding-mapping>
        <locale>pt-BR</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
        <locale>pt</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
        <locale>en</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
</locale-encoding-mapping-list>

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

On the JSP:

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

On the form:

<form:form method="post" ... acceptCharset="UTF-8">

On the servelt-context.xml:

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
    <beans:property name="contentType" value="text/html;charset=UTF-8" />
</beans:bean>

Inside my pom.xml (Maven):

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java-version>1.6</java-version>
    <org.springframework-version>3.1.0.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.1</org.slf4j-version>
</properties>

To call the server I'm using JQuery load() function like this:

var params = form.serialize();
$.ajaxSetup({
    contentType: 'text/html; charset=UTF-8'
});
$("#content").load(url, params);

I included this $.ajaxSetup as I desperated attempt, but it has no effect.

I noticed that on the params I have this "João" as "Jo%C3%83%C2%A3o". I changed back to "João" using Firebug but still getting the same issue.

EDIT: also included URIEncoding="UTF-8" on my apache-tomcat-7.0.27\conf\server.xml:

<Connector port="8080" protocol="HTTP/1.1"
   connectionTimeout="20000"
   redirectPort="8443" 
   URIEncoding="UTF-8" />

Any magic ideas?

Thanks in advance.

André Schuster
  • 165
  • 6
  • 18

2 Answers2

0

I'm running the application via maven tomcan plugin (mvn clean tomcat:run), and it ignores Tomcat configuration file. Therefore, I included the uriEncoding on the pom.xml, just like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <version>1.1</version>
    <configuration>
        <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
    </configuration>
</plugin>

Then my properties completed the trick setting the encode to UTF-8:

<properties>
...
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java-version>1.6</java-version>
...
</properties>
André Schuster
  • 165
  • 6
  • 18
0

Does this help (had the same issue):

https://stackoverflow.com/a/6644019/169277

Additionally ex: of the method which I use and I have no issue with encoding :

@RequestMapping(value = "/test")
    public ResponseEntity<String> test(Locale loc, String code, HttpServletResponse response) {
        //enforce right encoding
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");

        return new ResponseEntity<String>("João"), responseHeaders, HttpStatus.CREATED);
    }

Try to print out the variable or the value to stdout so you know it's not spring issue. Also check the file encoding(java file in the eclipse), right click -> properties -> text file encoding (at the bottom). Set it to UTF-8. Hope that helps

Community
  • 1
  • 1
ant
  • 22,634
  • 36
  • 132
  • 182