栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

spring3.0自定义通用转换器虽然已注册但未找到。错误:找不到匹配的编辑器或转换策略

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring3.0自定义通用转换器虽然已注册但未找到。错误:找不到匹配的编辑器或转换策略

我已经解决了上面发布的问题,并在此处发布了我的解决方案以供参考。

在发布导致错误的原因之前,我想显示 由我的应用程序的Dispatcher Servlet加载的 < servlet-name>
-servlet.xml
上下文文件。

< servlet名称> -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"       xmlns:tx="http://www.springframework.org/schema/tx"       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-3.0.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">        <context:annotation-config />        <context:component-scan base-package="<MY-PACKAGES>" />        <!-- Handles GET requests for /resources/** by efficiently serving static content in the ${webappRoot}/resources dir -->        <mvc:resources location="/resources/" mapping="/resources/**"/>        <!-- This tag registers the HandlerMapping and HandlerAdapter required to dispatch requests to your @Controllers.  In addition, it applies sensible defaults based on what is present in your classpath. Such defaults include: 1) Using the Spring 3 Type ConversionService as a simpler and more robust alternative to JavaBeans PropertyEditors 2) Support for formatting Number fields with @NumberFormat 3) Support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath  4) Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath  5) Support for reading and writing XML, if JAXB is on the classpath  6) Support for reading and writing JSON, if Jackson is on the classpath -->        <mvc:annotation-driven/>        <mvc:interceptors>          <bean >   <property name="sessionFactory">       <ref bean="sessionFactory"/>   </property>   <property name="singleSession" value="false" />          </bean>        </mvc:interceptors>        <!--   Without the following adapter, we'll get a "Does your handler implement a supported interface like Controller?"  This is because mvc:annotation-driven element doesn't declare a SimpleControllerHandlerAdapter  For more info  See http://stackoverflow.com/questions/3896013/no-adapter-for-handler-exception  See http://forum.springsource.org/showthread.php?t=48372&highlight=UrlFilenameViewController -->        <bean  />        <!--    org.springframework.web.servlet.view.ResourceBundleViewResolver   The bundle is typically defined in a properties file,    located in the class path. The default bundle basename is "views".         -->        <bean id="viewResolver" >  <property name="basename" value="views" />        </bean></beans>

从上面的文件中可以看出,我使用了 < mvc:annotation-driven />*,默认情况下会激活
org.springframework.format.support.FormattingConversionService 。我需要一个
org.springframework.core.convert.support.GenericConversionService 实例,使用
ConverterConfig (在第一篇文章中显示) 向其注册自定义转换器 StringToMapConverter
(在第一篇文章中显示)。
*

在Spring容器初始化时,属性值
org.springframework.web.bind.support.ConfigurableWebBindingInitializer.setConversionService(ConversionService)
被设置为 org.springframework.format.support.FormattingConversionService 的实例,而不是
org.springframework.core我 在其中注册了我的自定义转换器的
.convert.support.GenericConversionService ,因此引发了错误, 找不到匹配的编辑器或转换策略

但是到目前为止,我的应用程序中不需要格式转换服务,只想注册我的自定义转换器,因此为了实现我的目标,我使用了如下所示的更新代码:

更新了 -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"       xmlns:tx="http://www.springframework.org/schema/tx"       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-3.0.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">        <context:annotation-config />        <context:component-scan base-package="<MY-PACKAGES>" />        <!-- Handles GET requests for /resources/** by efficiently serving static content in the ${webappRoot}/resources dir -->        <mvc:resources location="/resources/" mapping="/resources/**"/>        <!-- This tag registers the HandlerMapping and HandlerAdapter required to dispatch requests to your @Controllers.  In addition, it applies sensible defaults based on what is present in your classpath. Such defaults include: 1) Using the Spring 3 Type ConversionService as a simpler and more robust alternative to JavaBeans PropertyEditors 2) Support for formatting Number fields with @NumberFormat 3) Support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath  4) Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath  5) Support for reading and writing XML, if JAXB is on the classpath  6) Support for reading and writing JSON, if Jackson is on the classpath -->         <!--    We just require the converter feature not the formatting feature.Thus   registering only the org.springframework.core.convert.support.GenericConversionService and    not org.springframework.format.support.FormattingConversionService which   <mvc:annotation-driven> activates by default.Also we are registering   a custom converter using the com.flightnetwork.cars.controller.util.ConverterConfig   class          -->        <!-- Added this to resolve my issue -->        <mvc:annotation-driven conversion-service="conversionService"/>        <mvc:interceptors>          <bean >   <property name="sessionFactory">       <ref bean="sessionFactory"/>   </property>   <property name="singleSession" value="false" />          </bean>        </mvc:interceptors>        <!--   Without the following adapter, we'll get a "Does your handler implement a supported interface like Controller?"  This is because mvc:annotation-driven element doesn't declare a SimpleControllerHandlerAdapter  For more info  See http://stackoverflow.com/questions/3896013/no-adapter-for-handler-exception  See http://forum.springsource.org/showthread.php?t=48372&highlight=UrlFilenameViewController -->        <bean  />        <!--    org.springframework.web.servlet.view.ResourceBundleViewResolver   The bundle is typically defined in a properties file,    located in the class path. The default bundle basename is "views".         -->        <bean id="viewResolver" >  <property name="basename" value="views" />        </bean>        <!-- Added this to resolve my issue -->        <bean id="conversionService" /></beans>

更新了ConverterConfig.java

    import java.util.Collections;    import org.springframework.beans.factory.annotation.Autowired;    import org.springframework.context.annotation.Bean;    import org.springframework.context.annotation.Configuration;    import org.springframework.context.annotation.DependsOn;    import org.springframework.core.convert.ConversionService;    import org.springframework.core.convert.converter.ConverterRegistry;    import org.springframework.core.convert.support.ConversionServiceFactory;    @Configuration    public class ConverterConfig {        @Autowired        private ConversionService conversionService;        @Autowired        private ConverterRegistry converterRegistry;        @Bean        @DependsOn(value="conversionService")        public StringToMapConverter stringToMapConverter() { StringToMapConverter stringToMapConverter = new StringToMapConverter(); stringToMapConverter.setConversionService(this.conversionService); ConversionServiceFactory.registerConverters(         Collections.singleton(stringToMapConverter), this.converterRegistry); return stringToMapConverter;        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/468862.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号