Makis Arvanitis
Makis Arvanitis

Reputation: 1195

Encoding problem using Spring MVC

I have a demo web application that creates users. When I try to insert data in other languages (like french) the characters are not encoded correctly. The code on the controller is:

@SuppressWarnings("unchecked")
    @RequestMapping(value = "/user/create.htm", params={"id"}, method = RequestMethod.GET)
    public String edit(@RequestParam("id") Long id, ModelMap model) {
        System.out.println("id is " + id);
        User user = userService.get(id);

        model.put("user", user);
        return "user/create";
    }

    @RequestMapping(value = "/user/create.htm", method = RequestMethod.POST)
    public String save(@ModelAttribute("user") User user, BindingResult result) {

        System.out.println(user.getFirstName());
        System.out.println(user.getLastName());


        validator.validate(user, result);
        if(result.hasErrors()) {
            return "user/create"; 
        }

        userService.save(user);
        return "redirect:list.htm";
    }

my web.xml is:

...

    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>

    </filter>

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

...

and the page is:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

...

<form:form method="post" commandName="user">

...

<form:input path="firstName" cssErrorClass="form-error-field"/>

...

when I enter some french characters in the first name then the output from the system.out.println is ????+????? or something similar.

I saw other people fixing this with the CharacterEncodingFilter but this doesn't seem to work.

Thanks a lot.

Edited the filter value.

Upvotes: 14

Views: 17312

Answers (6)

Jacob Mattison
Jacob Mattison

Reputation: 51052

Perhaps I'm missing something, but if the page-encoding in your JSP is "UTF-8", shouldn't the encoding in your CharacterEncodingFilter be UTF-8 rather than ISO-8859-7?

Upvotes: 2

Brett Ryan
Brett Ryan

Reputation: 28255

There are two things to help:

  1. In your setenv.sh file add JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=utf-8"
  2. In the script that starts your tomcat process, set the LANG environment: export LANG='utf-8'

This can then be tested by examining the default character set: Charset.defaultCharset().

Upvotes: 0

Dmitry Avdeev
Dmitry Avdeev

Reputation: 121

You need to add accept-charset="UTF-8" to your form.

Upvotes: 1

Luke
Luke

Reputation: 764

Try making CharacterEncodingFilter the first filter in web.xml.

I realize this question is a little old, but I just ran into the same problem, and moving CharacterEncodingFilter fixed it for me.

Upvotes: 11

Koraktor
Koraktor

Reputation: 42913

If this still does not work and you're using Tomcat as your application server try to set the following option on every <Connector> element in the server.xml:

<Connector URIEncoding="UTF-8" ...>
    ...
</Connector>

This did the trick for me. There might be similar options for other application servers, so you might want to check the server documentation.

Upvotes: 7

axtavt
axtavt

Reputation: 242686

Output of System.out.println() depends on console encoding, so it's not a good way to debug encoding problems.

To check that your values are decoded properly, you should show it at another page. Actually, it's already done in the case of form validation failure, so your system works fine if values in the fields remains the same after validation error.

Upvotes: 0

Related Questions