2014-10-06 67 views
0

编码问题。 Spring MVC控制器从ajax char'?'收到而不是“A”等等......所有网页上的编码是UTF-8,在JavaScript文件显示正确的字符警惕,设立Ajax和添加过滤器不工作spring mvc 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>"); 
    } 
}); 

控制器

@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> 
+0

tomcat?或玻璃鱼? – 2014-10-06 09:05:58

+0

tomcat 7服务器 – eriksson 2014-10-06 09:10:17

回答

0

只是配置您的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" 
    /> 

,并请参阅本维基here,约在Tomcat的字符编码。

并且还参考这个答案here

希望有所帮助。

+0

谢谢。这行得通。 – eriksson 2014-10-06 11:34:22