1. AJAX를 사용하여 전경 페이지에서 백그라운드 컨트롤러로 데이터를 전송할 때 중국어가 달린 코드 (질문 마크 ???)가 나타납니다.
나는 이전에 온라인으로 다양한 솔루션을 찾았지만 작동하지 않았습니다. 마지막으로 Tomcat 서버가 데이터를 수신하는 데 문제가 있음을 알았습니다.
해결책:
방법 1 : 컨트롤러가 매개 변수를 수신 할 때 매개 변수를 트랜스 코드
@responsebody@requestmapping (value = "/getJsondatabycityName", produces = "application/json") public String getJsonByName (httpservletRequest request, httpservletrepsonge 응답,@requestParam String city_name) servletexception, ioexception { //response.setContentType("text/html; charset=utf-8 "); //request.setcharacterencoding("utf-8") ;////SOLDE POST GRADBLED PROSKE SYSTEM.out.println (request.getCharacterEncoding ()); city_name = new String (city_name.getBytes ( "ISO-8859-1"), "UTF-8"); System.out.println ( "city_name :"+city_name);}방법 2 : 방법 2.
Tomcat 디렉토리에서 service.xml 파일을 구성하십시오
tomcat7/conf/server.xml
이 코드 라인에 uriencoding = "utf-8"의 인코딩 속성을 추가하십시오.
<커넥터 포트 = "8080"프로토콜 = "http /1.1"ConnectionTimeout = "20000"redirectport = "8443"uriencoding = "utf-8" />
원인 분석 :
실제로, 문제의 이유는 기본 Tomcat 구성, 수신 된 요청이 ISO-8859-1로 트랜스 코딩되어 중국어의 문제가 발생하기 때문입니다. Barbled 코드가 UTF-8으로 올바르게 변환 될 수있는 한, Breadbled 문제를 해결할 수 있습니다.
2. JSP 페이지에서 배경 컨트롤러에 이르기까지 일반 데이터 전송, 중국어로 된 문제에 대한 해결책
(1) 먼저 JSP 페이지 인코딩 형식이 UTF-8인지 확인
(2) 중국 필터링을 설정합니다
<!-중국 인코딩-> <filter> <filter-name> 캐릭터 encodingfilter </filter-name> <filter-class> org.springframework.web.filter.caracterencodingfilter </filter-class> <init-param> <param-name> 인코딩 </param--value> utf-8 </param-val-value> <init-param> <init-param> <param-name> forceencoding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> 캐릭터 에코딩 필터 </filter-name> <Url-pattern>/*</url-pattern>
(3) SpringMVC 구성 파일에서 JSON 데이터 변환을 설정합니다
<MVC : 주석 구동> <MVC : Message-Converters Register-defaults = "true"> <!-스프링 MVC 주석 기능 시작, 요청 및 주석 매핑을 완료하면 Pojo 매핑 주석 요청 매핑은 기본적으로 ISO-88859-1입니다. 이것은 UTF-8-> <ean> <속성 이름 = "supportedMediAtypes"value = "text/html; charset = utf-8"/> </bean> <!-JSON 형식 구성, JSON 형식으로 자동 변환 할 필요가 없으며, 다른 클래스가 필요하지 않습니다-> <bean id = "jacksonMessageConverter"> <property name = "supportedMediAteGeconverter는 필요하지 않습니다. value = "application/json; charset = utf-8"/> </bean> </mvc : message-converters> </mvc : Annotation-Driven>
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.