你可以看到它定义了哪些 bean.我已经完成了这项锻炼"(不是针对所有人,而是针对我需要的那些人),所以它们是:<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><属性名称=webBindingInitializer"><bean class="com.yourpackage.web.util.CommonWebBindingInitializer"/></属性><属性名称="messageConverters"><bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/><bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/><bean class="org.springframework.http.converter.StringHttpMessageConverter"/><bean class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter"/><bean class="org.springframework.http.converter.feed.RssChannelHttpMessageConverter"/><bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/><bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/><bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/><!-- bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/--></list></属性></bean><bean id="handlerMapping"class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">现在,您可以在上方看到 CommonWebBindingInitializer.您必须创建此类,才能使用转换和验证:公共类 CommonWebBindingInitializer 实现 WebBindingInitializer {@自动连线私有验证器验证器;@自动连线私有 ConversionService 转换服务;@覆盖公共无效initBinder(WebDataBinder绑定器,WebRequest请求){binder.setValidator(验证器);binder.setConversionService(conversionService);}}到目前为止,这对我来说很好用.如有任何问题,请随时报告.Up to now, <mvc:annotation-driven /> has caused plenty of trouble for me, so I would like to get rid of it. Although the spring framework docs clearly say what it is supposed to be doing, a listing of tags actually summar <mvc:annotation-driven /> is lacking.So I'm stuck with removing <mvc:annotation-driven /> and now getting the errorfor all Urls supposed to be resolved by the controller classes (in this case: ./trainees). Any suggestion where I can read more about <mvc:annotation-driven />? I desperately would like to know what tags exactly are represented by <mvc:annotation-driven />. 解决方案 You can use BeanPostProcessor to customize each bean defined by <mvc:annotation-driven />. The javadocs now details all beans the tag registers.If you really want to get rid of it, you can look at the source code of org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParserAnd you can see which beans it is defining. I've done this 'exercise' (not for all of them, but for those I need), so here are they:<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="com.yourpackage.web.util.CommonWebBindingInitializer" /> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter" /> <bean class="org.springframework.http.converter.feed.RssChannelHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /> <!-- bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /--> </list> </property> </bean><bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">Now, above you see the CommonWebBindingInitializer. You have to create this class, in order to use conversion and validation:public class CommonWebBindingInitializer implements WebBindingInitializer { @Autowired private Validator validator; @Autowired private ConversionService conversionService; @Override public void initBinder(WebDataBinder binder, WebRequest request) { binder.setValidator(validator); binder.setConversionService(conversionService); }}And this works fine for me so far. Feel free to report any problems with it. 这篇关于如何摆脱&lt;mvc:annotation-driven/&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 07:05
查看更多