环境说明
- JDK 17
- SpringMVC 6.0.6
- Tomcat 10.0.12
环境准备
首先创建一个Jakar EE 项目,选择Web Application 模板,指定Tomcat版本,然后添加 SpringMVC 依赖(导入该依赖时会自动导入Spring等相关依赖):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.6</version>
</dependency>
XML 配置形式
配置 web.xml 文件,用 DispacherServlet 替换掉 Tomcat 自带的 Servlcet,url-pattern 设置为 “/” :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
接着为 Web 应用配置 Spring 上下文环境(SpringMVC是基于Spring开发的,它直接利用Spring提供的容器来实现各种功能):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
</beans>
然后为DispatcherServlet配置初始化参数来指定刚刚创建的配置文件:
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
删除项目自带的Servlet类,创建一个 MVC 中使用的Controller类:
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/")
public String hello(){
return "HelloWorld!";
}
}
在 Spring 配置文件中注册这个 Bean,这里直接使用包扫描的方式:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example.controller"/>
<!--<bean class="com.example.controller.HelloController"/>-->
</beans>
最后启动项目访问即可。
注解配置形式
创建 Initializer 配置类用以替代 web.xml 配置文件。该配置类继承自 AbstractAnnotationConfigDispatcherServletInitializer 类,后者主要作用是负责应用程序中 DispatcherServlet 和 Spring 上下文的配置。创建 MainConfig 配置类作为主容器的配置类,创建 WebConfig 配置类作为 web 子容器的配置类:
@Configuration
public class MainConfig {
}
@EnableWebMvc
@ComponentScan("com.example.controller")
@Configuration
public class WebConfig {
}
然后在 Initializer 配置类中引入主容器和web容器的配置类 相关 :
public class MainInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
// 父容器的配置类,一般用于业务层配置
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{MainConfig.class};
}
// 子容器的配置类(SpringMVC相关的配置类),主要用于Controller等配置
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
// 配置DispatcherServlet的url-pattern
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
启动项目即可访问。