mvc:annotation-driven context:annotation-config context:component-scan

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。

<context:annotation-config> declares support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.

<mvc:annotation-driven /> is actually rather pointless. It declares explicit support for annotation-driven MVC controllers (i.e.@RequestMapping, @Controller, etc), even though support for those is the default behaviour.

My advice is to always declare <context:annotation-config>, but don't bother with <mvc:annotation-driven /> unless you want JSON support via Jackson.

当我们需要controller返回一个map的json对象时,可以设定<mvc:annotation-driven />,

同时设定<mvc:message-converters> 标签,设定字符集和json处理类


context:component-scan在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean

注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用<mvc:annotation-driven>标签时,可能会出现以下错误: 1. 找不到org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping类 这个错误通常是由于Spring版本不兼容导致的。如果您使用的是Spring 3.1或更高版本,则应该使用<mvc:annotation-driven>标签。如果您使用的是Spring 3.或更低版本,则应该使用<context:component-scan>标签。 2. 找不到org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter类 这个错误通常是由于Spring版本不兼容导致的。如果您使用的是Spring 3.1或更高版本,则应该使用<mvc:annotation-driven>标签。如果您使用的是Spring 3.或更低版本,则应该使用<context:component-scan>标签。 3. 找不到org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter类 这个错误通常是由于Spring版本不兼容导致的。如果您使用的是Spring 5.或更高版本,则应该使用@EnableWebMvc注释。如果您使用的是Spring 4.或更低版本,则应该使用WebMvcConfigurerAdapter类。 4. 找不到org.springframework.web.servlet.DispatcherServlet类 这个错误通常是由于您没有正确配置DispatcherServlet导致的。请确保您已经正确配置了DispatcherServlet,并且在web.xml文件中正确地映射了它。 5. 找不到org.springframework.web.servlet.view.InternalResourceViewResolver类 这个错误通常是由于您没有正确配置InternalResourceViewResolver导致的。请确保您已经正确配置了InternalResourceViewResolver,并且在Spring配置文件中正确地定义了它。 总之,当使用<mvc:annotation-driven>标签时,如果出现错误,请检查您的Spring版本和配置是否正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值