入门程序目录结构
1、引入依赖
- spring-context
- spring-webmvc
- servlet-api
- jsp-api
pom.xml
<!--spring的核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--springMVC的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--
maven 项目执行分为三个阶段
编译(需要) 测试(需要) 运行(不需要)
provided : 编译器生效,测试生效,运行不生效
servlet-api ,jsp-api:这两个包,需要配置依赖范围为provided ,其他都不需要
-->
<!-- servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
2、spring-mvc.xml配置
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">
<!--开启注解,扫描包-->
<context:component-scan base-package="com.testfan"></context:component-scan>
</beans>
3、web.xml配置
web.xml
<!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>
<!--配置servlet:前端控制器
springmvc只有一个servlet
-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--servlet中配置局部参数:读取配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--在启动tomcat时创建servlet对象,如果不创建 在第一次访问servlet时创建-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!--
/ 拦截所有的请求: 包括http请求, 静态资源请求:img,js ,css
/* 拦截请求:不包含静态资源
*.do , *.action : 拦截以 .do, .action结尾的请求, 思想来源于struts2:默认拦截.action
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、自定义核心控制类
TestController.java
@Controller
@RequestMapping("/test")
public class TestController {
/**
* RequestMapping: 请求映射
* @return
*/
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
System.out.println("helloWorld--测试成功");
return "show";
}
}
5、页面配置
index.jsp
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--isELIgnored="false" 不忽略el表达式 --%>
<a href="${pageContext.request.contextPath}/test/hello">请求1</a>
</body>
</html>
show.jsp
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
展示页面
</body>
</html>
6、效果展示
控制台打印:helloWorld–测试成功