目录
一、创建项目
二、环境搭建
(1)引入相关依赖
(2)在web.xml中配置前端控制器DispatcherServlet
(3)编写SpringMVC核心配置文件springmvc.xml
三、测试是否成功
(1)编写控制器
(2)运行项目
(2)运行测试
一、创建项目
我们创建的是一个maven项目,并且它是从原型创建的,如下:
二、环境搭建
(1)引入相关依赖
<dependencies> <!-- Spring核心模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.12.RELEASE</version> </dependency> <!-- SpringWeb模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.12.RELEASE</version> </dependency> <!-- SpringMVC模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.12.RELEASE</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- JSP --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>8080</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> <systemProperties> <java.util.logging.SimpleFormatter.format>%1$tH:%1$tM:%1$tS %2$s%n%4$s: %5$s%6$s%n </java.util.logging.SimpleFormatter.format> </systemProperties> </configuration> </plugin> </plugins> </build>
(2)在web.xml中配置前端控制器DispatcherServlet
<web-app> <display-name>Archetype Created Web Application</display-name> <!--SpringMVC前端控制器,本质是一个Servlet,接收所有请求,在容器启动时就会加载--> <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:springmvc.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> </web-app>
(3)编写SpringMVC核心配置文件springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描包 --> <context:component-scan base-package="com.itbaizhan"/> <!-- 开启SpringMVC注解的支持 --> <mvc:annotation-driven/> </beans>
三、测试是否成功
(1)编写控制器
@Controller public class MyController1 { // 该方法的访问路径是/c1/hello1 @RequestMapping("/c1/hello1") public void helloMVC(){ System.out.println("hello SpringMVC!"); } }
(2)运行项目
(1)点击配置,如图的maven标志,在没有配置时上面写的是添加配置
(2)点击左上角的加号
(3)找到maven标志,并且点击
(4)命令行中如下命令即可
(2)运行测试
使用tomcat插件启动项目,访问 http://localhost:8080/c1/hello1
出现如上两种即为成功!