之前有乱码,
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>
问题解决。
然后又添加了自定义转换器,
<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"/>
问题又出现了:
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>
刚学一周,这到底是什么原理啊?!
这两个配置不能同时使用吗?谢谢!!