如何摆脱<mvc:annotation-driven/>? [英] Howto get rid of <mvc:annotation-driven />?
问题描述
到目前为止,
给我带来了很多麻烦,所以我想摆脱它.虽然 spring 框架文档清楚地说明它应该做什么,实际上缺少总结 <mvc:annotation-driven/>
的标签列表.
所以我坚持删除 <mvc:annotation-driven/>
现在得到错误
警告 o.s.web.servlet.PageNotFound -未找到 HTTP 请求的映射URI [/webapp/trainees] 在带名称的 DispatcherServlet'锻炼传感器'
对于所有应该由控制器类解析的 URL(在本例中:./trainees
).有什么建议可以让我阅读更多关于 <mvc:annotation-driven/>
的信息?我非常想知道 <mvc:annotation-driven/>
到底代表了什么标签.
您可以使用 BeanPostProcessor
自定义每个由 <mvc:annotation-driven/>
.javadocs 现在详细说明了标记注册的所有 bean.
如果你真的想摆脱它,你可以看一下org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
>
你可以看到它定义了哪些 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 error
WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/webapp/trainees] in DispatcherServlet with name 'workoutsensor'
for 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.AnnotationDrivenBeanDefinitionParser
And 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.
这篇关于如何摆脱<mvc:annotation-driven/>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!