对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

启用MVC配置

在Java配置中,你可以使用@注解来启用MVC配置,如下例所示:spring-doc.cadn.net.cn

@Configuration
@EnableWebMvc
public class WebConfig {
}
@Configuration
@EnableWebMvc
class WebConfig

在XML配置中,你可以使用<mvc:annotation-driven>元素来启用MVC配置,如下例所示:spring-doc.cadn.net.cn

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/mvc
		https://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<mvc:annotation-driven/>

</beans>

上一个示例注册了多个Spring MVC 基础结构bean,并适应类路径上的依赖项(例如,用于JSON、XML和其他格式的有效载荷转换器)。spring-doc.cadn.net.cn