今天学习了SpringMVC的基本项目搭建,现在总结出来与大家分享
1、导入所需要的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
2、在Web.xml中配置SpringMVC核心控制器DispatcherServlet
<!--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>
3、创建Controller和视图Jsp页面并使用注解配置 Controller类中业务方法的映射地址
@Controller
public class TestController {
@RequestMapping("/quick")
public String TestShow(){
return "succes.jsp";
}
}
4、配置 SpringMVC核心文件 spring-mvc.xml
<?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 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.itcast.controller"/>
</beans>
5、启动项目并进行测试
至此SpringMVC简单快速入门搭建就完成了.