一、环境准备
1、POM文件依赖
1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <maven.compiler.source>1.7</maven.compiler.source> 4 <maven.compiler.target>1.7</maven.compiler.target> 5 <spring.version>5.0.4.RELEASE</spring.version> 6 </properties> 7 8 <dependencies> 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-context</artifactId> 12 <version>${spring.version}</version> 13 </dependency> 14 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-web</artifactId> 18 <version>${spring.version}</version> 19 </dependency> 20 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-webmvc</artifactId> 24 <version>${spring.version}</version> 25 </dependency> 26 27 <dependency> 28 <groupId>javax.servlet</groupId> 29 <artifactId>servlet-api</artifactId> 30 <version>2.5</version> 31 <scope>provided</scope> 32 </dependency> 33 34 <dependency> 35 <groupId>javax.servlet.jsp</groupId> 36 <artifactId>jsp-api</artifactId> 37 <version>2.1</version> 38 <scope>provided</scope> 39 </dependency> 40 </dependencies>
2、/webapp/WEB-INF/web.xml文件
<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> <!-- 配置Servlet的初始化参数,读取springmvc的配置文件,创建spring容器 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 配置servlet启动时加载对象 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--配置欢迎页--> <welcome-file-list> <welcome-file>index1.jsp</welcome-file> </welcome-file-list> </web-app>
3、编写resources下的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"> <!-- 配置spring创建容器时要扫描的包 --> <context:component-scan base-package="com.kunming"></context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置spring开启注解mvc的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
4、编写index.jsp和HelloController类
(1)index.jsp
<body> <h3>入门程序</h3> <a href="/hello">点击我</a> </body>
(2)HelloController.java
@Controller public class HellorController { @RequestMapping("/hello") public String Hello(){ return "success"; } }
(3)在WEB-INF目录下创建pages文件夹,编写success.jsp的成功页面
<body> <h3>入门成功!!</h3> </body>
二、执行流程解析
1. 当启动Tomcat服务器的时候,加载web.xml配置,因为配置了load-on-startup标签,所以会创建DispatcherServlet对象, 就会加载springmvc.xml配置文件
2. 开启了注解扫描,那么HelloController对象就会被创建
3. 从index.jsp发送请求,请求会先到达DispatcherServlet核心控制器,根据配置@RequestMapping注解 找到执行的具体方法
4. 根据执行方法的返回值,再根据配置的视图解析器,去指定的目录下查找指定名称的JSP文件
5. Tomcat服务器渲染页面,做出响应
图形化:
完整流程图:
原文:https://www.cnblogs.com/kunming97/p/14413966.html