我正在使用Spring框架与Tomcat 7& MySQL的。我使用UTF-8编码提交本地语言的表单数据。这通过JSP使用post工作正常。但是,在使用基于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 :(得分:0)
尝试添加内容编码的http标头 您的浏览器使用Unicode来表示要显示的数据。来自Web服务器的资源可以选择在http头中附加编码信息。 如果存在内容编码头,则浏览器使用响应头中的编码将资源转换为内部unicode表示。如果没有内容编码标头,则浏览器假定文件编码与请求资源的页面相同。