1. 程式人生 > >後臺傳值前臺,頁面中文亂碼

後臺傳值前臺,頁面中文亂碼

解決方法一:**.java

@RequestMapping(value="/disasterPicBrowse.do", produces = {"text/html;charset=UTF-8;"})//解決:後臺傳值前臺,頁面中文亂碼

@RequestMapping(value="/getDisasterPicBrowseData.data", produces = {"text/html;charset=UTF-8;"})//解決:後臺傳值前臺,頁面中文亂碼
@ResponseBody

解決方法二:配置springmvc.xml

<!-- 3—方式1.解決 ResponseBody 返回中文亂碼,如果用<mvc:annotation-driven/>,則要放在其前--> 
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">     
	 <property name="messageConverters">         
		 <list>             
			 <bean class="org.springframework.http.converter.StringHttpMessageConverter">                 
				 <property name="supportedMediaTypes">                     
					 <list>  
						 <!-- // **.js中$.ajax({對應此處的ajax屬性配置}) -->
						 <bean class="org.springframework.http.MediaType">                             
							 <constructor-arg index="0" value="text" />                             							     
							 <constructor-arg index="1" value="plain" />                            
							 <constructor-arg index="2" value="UTF-8" />
							 <!-- <constructor-arg index="3" value="json" />   //**.js中$.ajax({dataType:'json',//已配置}),則此行不能重複配置  -->					     
						 </bean>                     
					 </list>                 
				 </property>             
			 </bean>         
		 </list>     
	 </property> 
 </bean>   

<!-- 3—方式2.解決 ResponseBody 返回中文亂碼,如果用<mvc:annotation-driven/>,則要放在其前-->
<bean id="StringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
	<property name="supportedMediaTypes">
		<list>
			<value>text/plain;charset=utf-8</value>
			<value>text/html;charset=utf-8</value>
		</list>
	</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.mappingJackson2HttpMessageConverter">
	<property name="supportedMediaTypes">
		<list>
			<value>application/json;charset=utf-8</value>
		</list>
	</property>
</bean>  

注意:需要重新發布專案、重啟伺服器Tomcat7,才能生效。

其他參考連結

SQL Server中文亂碼問題的解決http://database.51cto.com/art/201011/233041.htm

ajax post data 獲取不到資料,注意 content-type的設定 、post/gethttps://www.cnblogs.com/dayou123123/p/3443939.html

springmvc 怎麼設定content-type為 application/json。https://wenda.so.com/q/1478577266725668