0

Problem with encoding. Spring MVC controller receives from ajax char '?' instead 'ã' etc... Encoding on all page is utf-8, alert in javascript file show right chars, set up ajax, and adding filters do not work

*javascript* 

$.ajax({
    url : urlID,        
    contentType: "text/html; charset=utf-8",
    data : parameters,
    success : function(data, textStatus) {
        $(contentID).empty().html(data);
    },
    error : function(jqXHR, textStatus, errorThrown) {
        $("#errorMessage").empty().html(
                "<h3>Exception: " + errorThrown + "</h3>");
    }
});

Controller

@RequestMapping(value = "/marketing-changepage", method = RequestMethod.GET)
    public String marketingChangepage(HttpServletResponse response, @RequestParam String startDate,
            @RequestParam String endDate, @RequestParam String device,
            @RequestParam String source, Model model, Principal principal) {        
        AjaxRequestParams ajaxRequestParams = new AjaxRequestParams();
        ajaxRequestParams.setDateRange(new DateRange(startDate, endDate));
        ajaxRequestParams.setSource(source);
        ajaxRequestParams.setDevice(device.toLowerCase());
        ajaxRequestParams.setClientInfo(userService
                .fetchClientInfoByUserName(principal.getName()));

        preparePage(ajaxRequestParams, model, principal);
        return "marketing/content_marketing";
    }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>logbackConfigLocation</param-name>
        <param-value>/WEB-INF/logback.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>charsetFilter</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>charsetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>

    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/WEB-INF/views/generalerror.jsp</location>
    </error-page>
</web-app>
1
  • tomcat ? or glassfish ? Commented Oct 6, 2014 at 9:05

1 Answer 1

0

just configure your server.xml file and add attribute URIEncoding="UTF-8" like the following :

  <Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true"
               URIEncoding="UTF-8"
   />

and please refer to this wiki here , about Character Encoding in Tomcat .

and also refer to this answer here .

Hope that Helps .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.