2012-01-23 57 views
2

我正在使用Spring框架與Tomcat 7 & MySQL。我正在使用UTF-8編碼爲本地語言提交表單數據。這可以正常使用通過JSP發佈。但是,當使用基於Ajax的表單提交時,它將垃圾字符顯示爲服務器中的請求參數。使用Ajax本地化表單提交

請讓我知道如果我錯過什麼..

** My Ajax Code** 

    xmlHttp.open("POST", "viewTeluguScript.htm", true); 
    //xmlHttp.setRequestHeader("Content-type", "multipart/form-data;charset=UTF-8"); 
    xmlHttp.setRequestHeader("Content-length", parameters.length); 
    // xmlHttp.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); 
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8"); 
    //xmlHttp.setRequestHeader("pageEncoding","UTF-8"); 
    //xmlHttp.setRequestHeader("accept-charset","UTF-8"); 
    xmlHttp.setRequestHeader("content-language","tel"); 
    xmlHttp.setRequestHeader("Connection", "close"); 
    xmlHttp.send(parameters); 


    **My Controller code** 

    public ModelAndView viewTeluguScript(HttpServletRequest request, HttpServletResponse response) throws SQLException, CannotGetJdbcConnectionException, Exception { 

    //textInfo = java.net.URLDecoder.decode(request.getParameter("teluguText"), "UTF-8"); 
    textInfo = request.getParameter("teluguText"); 
    /* StringBuffer jb = new StringBuffer(); 
    String line = null; 
    try { 
    BufferedReader reader = request.getReader(); 
    while ((line = reader.readLine()) != null) 
    jb.append(line); 
    } catch (Exception e) { 


    } 

    System.out.println("request is :::: "+jb.toString());*/ 


    ModelAndView mv = new ModelAndView("viewQuestions"); 
    //request.getSession().setAttribute("workingModule", "questions"); 
    return mv; 
    } 


     enter code here 

    **My web.xml** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 


     <context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 

       /WEB-INF/applicationContext-security.xml 
       classpath:applicationContext-common-business.xml 
       classpath:applicationContext-common-authorization.xml 
      </param-value> 
     </context-param> 

     <filter> 
      <filter-name>encodingFilter</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> 

     <locale-encoding-mapping-list> 
      <locale-encoding-mapping> 
       <locale>en</locale> 
       <encoding>UTF-8</encoding> 
      </locale-encoding-mapping> 
      <locale-encoding-mapping> 
       <locale>tel</locale> 
       <encoding>UTF-8</encoding> 
      </locale-encoding-mapping> 

     </locale-encoding-mapping-list> 


     <filter-mapping> 
      <filter-name>encodingFilter</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> 
     <listener> 
      <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
     </listener> 
     <listener> 
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
     </listener> 
     <servlet> 
      <servlet-name>g2l</servlet-name> 
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <!--<load-on-startup>2</load-on-startup>--> 
     </servlet> 
     <servlet-mapping> 
      <servlet-name>g2l</servlet-name> 
      <url-pattern>*.htm</url-pattern> 
     </servlet-mapping> 
     <!-- <session-config> 
      <session-timeout> 
       30 
      </session-timeout> 
     </session-config> 
     <welcome-file-list> 
      <welcome-file>redirect.jsp</welcome-file> 
     </welcome-file-list>--> 
    </web-app> 

回答

0

嘗試添加內容編碼HTTP頭 您的瀏覽器使用Unicode來表示要顯示的數據。來自Web服務器的資源可以選擇在http頭中附加編碼信息。 如果存在內容編碼標頭,瀏覽器會使用響應標頭中的編碼將資源轉換爲內部unicode表示。如果沒有內容編碼頭,瀏覽器會假定文件編碼與請求資源的頁面相同。

+0

已嘗試xmlHttp.setRequestHeader(「Content-encoding」,「UTF-8」);然而它不工作。謝謝你的建議 – shravan