gpt4 book ai didi

spring-mvc - Spring 框架 : what is the purpose of

转载 作者:行者123 更新时间:2023-12-03 09:37:34 25 4
gpt4 key购买 nike

我对 Spring 比较陌生,我对标签有点困惑。

在浏览了文档并查看了不同的帖子之后,它的主要用途似乎是 Spring MVC 要求将请求分派(dispatch)到 @Controllers。

我创建了一个带有两个 requestMappings 的 Controller :

@RequestMapping(method=RequestMethod.GET, value="/health")
@RequestMapping(method=RequestMethod.GET, value="/test")

我在 servlet.xml 中测试了带有和不带有的 Web 应用程序,似乎省略与不省略似乎没有任何区别。这些请求似乎仍然可以很好地到达我的 Controller 。

谁能向我解释该标签的确切用途?

提前致谢!

最佳答案

支持@Controller@RequestMapping默认情况下由 Spring 提供。但是,通过启用 mvc:annotation-driven您可以支持处理映射到带注释的 Controller 方法的请求,例如声明性验证、格式化和转换服务。摘自 spring's blog介绍了新的配置功能

It applies sensible defaults based on what is present in your classpath. Such defaults include:

  • Using the Spring 3 Type ConversionService as a simpler and more robust alternative to JavaBeans PropertyEditors

  • Support for formatting Number fields with @NumberFormat

  • Support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
  • Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
  • Support for reading and writing XML, if JAXB is on the classpath
  • Support for reading and writing JSON, if Jackson is on the classpath


另一个相关的有用博客 post

If this tag is not added to the XML, then you will have to manually define the beans for components like HandlerAdapter, HandlerMapping, Binding Initializer, Request Message converters, etc. This tag helps registering the following components.

  • DefaultAnnotationHandlerMapping - This is a HandlerMapping implementation which maps the HTTP requests to the handler methods defined using the @RequestMapping annotation.
  • AnnotationMethodHandlerAdapter - It is responsible for scanning the controllers to identify methods (and parameters) annotated with @MVC annotations. It scans and caches handler methods annotated with @RequestMapping. Also handles the @RequestParam, @ModelAttribute, @SessionAttributes and @InitBinder annotations.
  • ConfigurableWebBindingInitializer - The initializer for the Web Data Binder. Helps in declaratively configuring the Web Binder with validators, conversion services, property editors, etc.
  • LocalValidatorFactoryBean - Implements the validator interface and enables JSR303 validation. This is injected into ConfigurableWebBindingInitializer.
  • FormattingConversionServiceFactoryBean - A conversion factory that returns conversion services for basic objects like date and numbers. This factory is again injected into ConfigurableWebBindingInitializer.
  • Support for Message Converters


终于在官方 docs 中给出了更正式的定义

关于spring-mvc - Spring 框架 : what is the purpose of <mvc:annotation-driven/>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28851306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com