1.导入SpringMVC坐标
2.配置Servlet,即配置SpringMVC的前端控制器DispathcerServlet
3.编写POJO(控制器Controller),即创建Controller类和视图
4.通过注解将控制器配置到Spring容器中(@Controller),配置Controller类中业务方法的映射地址
5.配置组件扫描(spring-mvc.xml),即配置SpringMVC核心配置文件
6.客户端发起请求测试
首先,导入SpringMVC的坐标
<!--导入SpringMVC的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
</dependency>
然后,配置SpringMVC的前端控制器
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
接着创建Controller和业务方法,并配置注解
//创建Controller和业务方法,并配置注解
@Controller
public class UserController {
@RequestMapping("/save")
public String save(){
System.out.println("Save running");
return "success.jsp";
}
}
最后,创建SpringMVC的配置文件
<?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">
<!--配置Controller的组件扫描-->
<context:component-scan base-package="com.test.controller"></context:component-scan>
</beans>
评论 (0)