第一步:配置jar包
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
第二步:在web.xml中配置springMVC的前端控制器dispatcherservlet
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置springMVC的前端控制器DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数,配置springMVC配置文件的位置和名称-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
<!--表示容器在启动时,立即加载dispatcherServlet-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--让springMVC前端控制器拦截所有的请求-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第三步:创建springMVC配置文件,文件名与web.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: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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.itwangqi"></context:component-scan>
<!--视图解析器的配置-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀和后缀-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--开启SpringMVC注解的支持,一键式配置法-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
第四步:创建处理请求的控制器类,类名自定义,此处为Hello,重点是两个注释@Controller和@RequestMapping
@Controller:对类进行标注使其成为一个可以处理http请求的控制器
@RequestMapping:为控制器指定可以处理哪些URL请求。标注在类上时,该类所有的方法都将映射为相对于类级别的请求;标注在方法上时,该方法将会成为一个请求处理方法,它会在程序接受到对应的URL请求时被调用。
package com.itwangqi.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 王琦
* @create 2020/3/21 - 19:01
*/
@Controller
public class Hello {
@RequestMapping(path = "/hello")
public String sayHello(){
System.out.println("HELLO SPRINGMVC!");
return "success";
}
}
第五步:创建视图页面
<%--
Created by IntelliJ IDEA.
User: Asus
Date: 2020/3/21
Time: 18:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springMVC的入门程序</title>
</head>
<body>
<h3>入门程序</h3>
<a href="hello">入门</a>
</body>
</html>
/WEB-INF/pages/success.jsp
<%--
Created by IntelliJ IDEA.
User: Asus
Date: 2020/3/21
Time: 19:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门成功</h3>
</body>
</html>