参与的一个后台管理系统的开发,涉及到SpringMVC,Maven,Gson,Velocity等相关技术
问题:Ajax请求,转到服务器,进行逻辑判断后,传回结果,显示????
检查:web.xml中配置了包含字符过滤器,velocity头信息包含UTF-8字符集设置,velocity配置文件表明字符集设置UTF-8,spring配置文件中对于velocity页面的部分配置。
<filter>
<filter-name>CharacterEncodingFilter</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>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<meta charset="utf-8">
input.encoding=UTF-8
output.encoding=UTF-8
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm" />
<property name="exposeRequestAttributes" value="true"/>
<property name="exposeSessionAttributes" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
<!-- 页面使用工具类配置 -->
<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>
</bean>
依旧存在该问题,
解决方案如下:在spring中增加配置
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="urlDecode" value="true"/>
<property name="messageConverters">
<list>
<bean class=" org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
</list>
</property>
</bean>
初步判断问题原因在
org.springframework.http.converter.StringHttpMessageConverter
中的默认字符集是ISO-8859-1,造成的字符转换问题,问题虽然解决,但还有很多东西不是很理解,继续加油吧