Encoding problem using Spring MVC

Posted by Makis Arvanitis on Stack Overflow See other posts from Stack Overflow or by Makis Arvanitis
Published on 2010-06-02T14:10:34Z Indexed on 2010/06/02 14:23 UTC
Read the original article Hit count: 476

Filed under:
|

Hi all,

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.

© Stack Overflow or respective owner

Related posts about mvc

Related posts about spring