SpringMVC消息转换器失效?

新手上路,请多包涵

之前有乱码,
springmvc-servlet.xml加入

<mvc:annotation-driven >  
 <mvc:message-converters register-defaults\="true"\>  
  <!-- 消息转换器 -->  
  <bean class\="org.springframework.http.converter.StringHttpMessageConverter"\>  
 <property name\="supportedMediaTypes" value\="text/html;charset=UTF-8"/>  
 </bean>  <!-- 阿里 fastjson -->  
  <bean class\="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4"/>  
 </mvc:message-converters>  
</mvc:annotation-driven>

问题解决。
2.png
然后又添加了自定义转换器,

<bean id\="conversionService" class\="org.springframework.context.support.ConversionServiceFactoryBean"\>  
 <property name\="converters"\>  
 <list>  <!--日期转换器-->  
  <bean class\="com.bob.DateConverter"\>  
  <!-- 调用有参构造函数创建 bean -->  
  <constructor-arg type\="java.lang.String" value\="yyyy-MM-dd"/>  
 </bean>  <!--学生信息转换器-->  
  <bean class\="com.bob.StudentConverter"\></bean>  
 </list> </property></bean>  
<mvc:annotation-driven conversion-service\="conversionService"/>

问题又出现了:
1.png

springmvc-servlet.xml 全部

<?xml version\="1.0" encoding\="UTF-8"?>  
<beans xmlns\="http://www.springframework.org/schema/beans"  
  xmlns:xsi\="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns:context\="http://www.springframework.org/schema/context"  
  xmlns:mvc\="http://www.springframework.org/schema/mvc"  
  xsi:schemaLocation\="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"\>  
  <!--启用spring的一些annotation -->  
  <mvc:annotation-driven />  
  
  <!-- 配置自动扫描 -->  
  <context:component-scan base-package\="com.bob"\></context:component-scan>  
  <!-- 配置视图解析器 -->  
  <bean class\="org.springframework.web.servlet.view.InternalResourceViewResolver"\>  
  <!-- 前缀 -->  
  <property name\="prefix" value\="/WEB-INF/views/"\></property>  
  <!-- 后缀 -->  
  <property name\="suffix" value\=".jsp"\></property>  
 </bean>  
  <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->  
  <mvc:annotation-driven >  
 <mvc:message-converters register-defaults\="true"\>  
  <!-- 消息转换器 -->  
  <bean class\="org.springframework.http.converter.StringHttpMessageConverter"\>  
 <property name\="supportedMediaTypes" value\="text/html;charset=UTF-8"/>  
 </bean>  <!-- 阿里 fastjson -->  
  <bean class\="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4"/>  
 </mvc:message-converters>  
 </mvc:annotation-driven>  
  
 <bean id\="conversionService" class\="org.springframework.context.support.ConversionServiceFactoryBean"\>  
 <property name\="converters"\>  
 <list>  <!--日期转换器-->  
  <bean class\="com.bob.DateConverter"\>  
  <!-- 调用有参构造函数创建 bean -->  
  <constructor-arg type\="java.lang.String" value\="yyyy-MM-dd"/>  
 </bean>  <!--学生信息转换器-->  
  <bean class\="com.bob.StudentConverter"\></bean>  
 </list> </property> </bean> <mvc:annotation-driven conversion-service\="conversionService"/>  
  
 <mvc:default-servlet-handler /> <!--这句要加上,要不然可能会访问不到静态资源,具体作用自行百度-->  
</beans>

刚学一周,这到底是什么原理啊?!
这两个配置不能同时使用吗?谢谢!!

阅读 1.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题