我有一個Spring MVC Web應(yīng)用程序,并使用JSP創(chuàng)建了我的網(wǎng)頁。問題是,當(dāng)我直接提交表單時,編碼為“ application / x-www-form-urlencoded”。當(dāng)我通過AJAX請求從相同的表單提交相同的數(shù)據(jù)時,編碼為“ application / x-www-form-urlencoded; charset = UTF-8”。我需要輸入utf8編碼的字符,該字符是我的控制器中的用戶。例如:一個用戶鍵入“??ü?”,我的控制器將得到“?¤?????”。當(dāng)我通過AJAX請求發(fā)送數(shù)據(jù)時,我得到正確的“??ü?”。我究竟做錯了什么?這是通過http-post提交的簡單表格。進(jìn)行utf8編碼并非不可能。我的應(yīng)用程序在Spring 5.0.1的tomcat 8.5.11上運行。網(wǎng)頁都是HTML5,我在Servlet 3.1環(huán)境中使用JSTL 1.2.5。JSON映射和序列化由fastxml 2.9.2完成該配置完全基于Java。我的WebAppInitializer(aka web.xml)...@Overrideprotected Filter[] getServletFilters() { return new Filter[] { new HiddenHttpMethodFilter(), new CharacterEncodingFilter("UTF-8", true, true) };}在我的servlet配置中,我為StringHttpMessageConverter顯式設(shè)置了一個字符集。...@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8"))); converters.add(new ResourceHttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter());}該網(wǎng)頁看起來像<%@page contentType="text/html;charset=UTF-8"%><%@page pageEncoding="UTF-8"%><%@page session="true" %><%@page trimDirectiveWhitespaces="true" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %><%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %><!DOCTYPE html><html>...<meta charset="UTF-8"/>...<form id="createArticleForm" action="<c:url value='/article/save' />" method="post"> <input type="hidden" name="utf8" value="✓" />... <input type="text" name="name" required="required" />...</form>如您所見,我還嘗試了使用隱藏字段進(jìn)行utf8黑客攻擊。但是什么都行不通。即使我設(shè)置了表單屬性accept-charset =“ UTF-8”和/或encoding =“ application / x-www-form-urlencoded; charset = UTF-8”,也沒有任何改變。
添加回答
舉報
0/150
提交
取消