简体   繁体   English

Spring MVC Ajax编码

[英]spring mvc ajax encoding

Problem with encoding. 编码问题。 Spring MVC controller receives from ajax char '?' Spring MVC控制器从Ajax字符'?'接收 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 取而代之的是'ã'等...所有页面上的编码都是utf-8,javascript文件中的警报显示正确的字符,设置了ajax,并添加了过滤器不起作用

*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 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>

just configure your server.xml file and add attribute URIEncoding="UTF-8" like the following : 只需配置您的server.xml文件并添加属性URIEncoding="UTF-8" ,如下所示:

  <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 . 并请参考这里的关于Tomcat中字符编码的Wiki。

and also refer to this answer here . 在这里也参考这个答案。

Hope that Helps . 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM