首先你要确保自己有Maven项目
第一步:创建maven工程,导入SpringMVC坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.37</version>
</dependency>
第二步:在web.xml配置前端控制器DispatcherServlet
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
第三步:创建核心配置文件 ,在WEB-INF文件夹下创建SpringMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myController" name="/MyController" class="com.hwq.controller.MyController"></bean>
</beans>
第四步:创建类,实现控制器接口,重写方法
public class MyController implements Controller {
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
System.out.println("弼马温到此一游~~~~~~~~");
return null;
}
}
第五步:在核心配置文件中配置控制器路径
<bean id="myController" name="/MyController" class="com.hwq.controller.MyController"></bean>
第六步:浏览器测试
http://localhost:8080/MyController