2 spring-mvc.xml 配置
3 web.xml配置
4 自定义一个核心控制类
5 页面配置
详细步骤以及代码如下:
1 导入依赖
<dependencies>
<!--spring的核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--springMVC的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--servlet-api的包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<!--maven项目执行分为三个阶段 编译 测试 运行-->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
2 spring-mvc.xml 配置
<!--开启注解 扫描包-->
<context:component-scan base-package="cn.lijun"></context:component-scan>
3 web.xml配置
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置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:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4 自定义一个核心控制类
/**
* @author lijun
* @date 2019/7/9 9:48
*/
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/test")
public String test(){
System.out.println("我的第一个springMVC 测试成功");
return "/WEB-INF/show.jsp";
}
}
5 页面配置
User: lijun
Date: 2019/7/9
Time: 9:46
To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/test/test">请求</a>
<a href="${pageContext.request.contextPath}/test/test">请求1</a>
</body>
</html>