如何摆脱< mvc:annotation-driven />? [英] Howto get rid of <mvc:annotation-driven />?

查看:192
本文介绍了如何摆脱< mvc:annotation-driven />?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,< mvc:annotation-driven /> 给我带来了很多麻烦,所以我想摆脱它。虽然 spring框架文档清楚地说明它应该做什么,标签列表实际上是summar < mvc:annotation-driven /> 缺乏。

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.

所以我不得不删除< mvc:annotation-driven /> ,现在收到错误

So I'm stuck with removing <mvc:annotation-driven /> and now getting the error


WARN osweb.servlet.PageNotFound -
找不到带有
URI [/ webapp / trainees]的HTTP请求的映射b $ b DispatcherServlet,名称为
'worksesensor'

WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/webapp/trainees] in DispatcherServlet with name 'workoutsensor'

所有应该由控制器类解析的Urls(在此案例: ./受训者)。我可以阅读更多关于< mvc:annotation-driven /> 的任何建议?我非常想知道< mvc:annotation-driven /> 所代表的标签究竟是什么。

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 />.

推荐答案

您可以使用 BeanPostProcessor 来自定义由< mvc:annotation-driven />定义的每个bean。 。 javadoc现在详细说明了标签注册的所有bean。

You can use BeanPostProcessor to customize each bean defined by <mvc:annotation-driven />. The javadocs now details all beans the tag registers.

如果你真的想摆脱它,你可以查看 org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

If you really want to get rid of it, you can look at the source code of org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

你可以看到它定义了哪些bean 。我做过这个'练习'(不是针对所有人,而是针对我需要的人),所以这里是:

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">

现在,您可以看到 CommonWebBindingInitializer 。您必须创建此类,才能使用转换和验证:

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;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆