简体   繁体   English

Spring MVC标签配置

[英]spring mvc tag configuration

1, In spring 3.0 documentation, about mvc:annotation-driven is: 1,在spring 3.0文档中,关于mvc:annotation-driven是:

"this tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers." “这个标签注册了Spring MVC分发请求到@Controllers所需的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter bean。”

but i haven't injected this tag into any spring configuration file, i am sure of that. 但我没有将此标签注入任何spring配置文件中,对此我确信。
so why my app can dispatch requests to @Controllers without that? 那么为什么我的应用程序可以在没有该请求的情况下将请求调度到@Controllers?

2, After mvc:default-servlet-handler be injected into my app to handle static resource, all controllers don't work just 404 not found error on web page but static resource is fine. 2,将mvc:default-servlet-handler注入我的应用程序以处理静态资源后,所有控制器都无法正常工作,只是在网页上找不到404错误,但静态资源没问题。

i goolged it found that maybe a mvc:annotation-driven Lost. 我发现它可能是由mvc:annotation驱动的

unfortunately a customized interceptor stop working after a mvc:annotation-driven added. 不幸的是,在添加了mvc:annotation-driven之后,自定义的拦截器停止工作。 here is interceptor definition: 这是拦截器的定义:

<bean id="currentMemberInterceptor"  class="com.skill.common.CurrentMemberInjectionInterceptor" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
    <property name="interceptors">  
        <list>
            <ref bean="currentMemberInterceptor" />
        </list>  
    </property>  
</bean>

Interceptor can works if interceptor definition change to mvc tag such as: 如果拦截器定义更改为mvc标签,则拦截器可以工作,例如:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/" />
        <bean class="com.skill.common.CurrentMemberInjectionInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

I cannot make sense of those tags and relationship after read spring reference. 阅读弹簧参考后,我无法理解这些标签和关系。

plz help, thanks! 请帮助,谢谢!

The thing is that Spring uses default fallback configuration ( default strategies ) in many places when you don't provide any configuration. 事实是,当您不提供任何配置时,Spring会在许多地方使用默认的后备配置( 默认策略 )。

This is also true for handler mappings and adapters . 对于处理程序映射和适配器也是如此。 If you don't have any of these on your application context, Spring just tries to register some default beans. 如果您的应用程序上下文中没有这些,Spring只会尝试注册一些默认bean。 However as soon as you define at least one bean of the matching type, it will not try to register any defaults and you are on your own (and that makes sense if you think about it). 但是,一旦您定义了至少一个匹配类型的bean,它就不会尝试注册任何默认值,并且您是一个人(如果您考虑一下,这是很有意义的)。

See DispatcherServlet dependency initialization code and check the default fallback configuration to see what are the defaults. 请参阅DispatcherServlet依赖项初始化代码,并检查默认的后备配置以查看默认值。


Configuration namespaces can be sometimes a little bit cryptic about their inner workings. 配置名称空间有时可能对其内部运作有些神秘。 When I don't understand what is some tag doing I usually check source code of the tag handler (naming convention for these classes is "NameOfTheTag"BeanDefinitionParser ). 当我不明白标记在做什么时,我通常会检查标记处理程序的源代码(这些类的命名约定是"NameOfTheTag"BeanDefinitionParser )。 You can try it yourself by checking AnnotationDrivenBeanDefinitionParser (the class behind <mvc:annotation-driven /> ). 您可以通过检查AnnotationDrivenBeanDefinitionParser<mvc:annotation-driven />后面的类)来自己尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM