> 文章列表 > SpringMVC入门

SpringMVC入门

SpringMVC入门

    1.创建web项目,并导入需要的jar

 

2.index.jsp页面中创建一个a标签,执行一个地址请求
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h2>欢迎来到项目首页</h2>
<h3><a href="test">请求测试</a></h3>
</body>
</html>
3.web.xml中配置springMVC的核心控制器
DispatcherServlet,同时设置它创建的时候加载
springmvc的核心配置文件springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"><!--配置springMVC的核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化:DispatcherServlet创建的时候加载springmvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--配置加载的时机:非0表示服务器启动是就进行加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.src目录下创建核心配置文件springmvc.xml,扫描控
制器的包、配置视图解析器、开启注解驱动支持(默认配置
HandlerMapping映射器 和 HandlerAdapter 适配
器)
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.chen.controller"/>
<!--视图解析器-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--视图查找的位置-->
<property name="prefix" value="/" />
<!--视图对应的后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--开启注解支持:默认配置了 HandlerMapping映射器 和 HandlerAdapter 适配器-->
<mvc:annotation-driven />
</beans>
5.书写处理器,即我们的Controller,首先把类使用
@Controller标识,标识该类交给IOC容器管理,其次在方
法上指定映射关系
package com.chen.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/test")
public String test(){
System.out.println("TestController...test");
return "zhuye";
}
}
注意:
在controller的方法中指定返回值为要响应的视图名字,springMVC框架会通过视图解析器在 指定
位置 找到 指定后缀 的页面响应给前端。
6.springMVC的执行流程如下:(非常重要)
详细流程:

SpringMVC 的执行流程如下。
1. 用户点击某个请求路径,发起一个 HTTP request 请求,该请求会被提交到
DispatcherServlet(前端控制器);
2. DispatcherServlet 请求一个或多个 HandlerMapping(处理器映射器),并返回一个执
行链(HandlerExecutionChain)。
3. DispatcherServlet 将执行链返回的 Handler 信息发送给 HandlerAdapter(处理器适配
器);
4. HandlerAdapter 根据 Handler 信息找到并执行相应的 Handler(常称为 Controller);
5. Handler 执行完毕后会返回给 HandlerAdapter 一个 ModelAndView 对象Spring MVC
底层对象,包括 Model 数据模型和 View 视图信息);
6. HandlerAdapter 接收到 ModelAndView 对象后,将其返回给 DispatcherServlet
7. DispatcherServlet 接收到 ModelAndView 对象后,会请求 ViewResolver(视图解析器)对
视图进行解析;
8. ViewResolver 根据 View 信息匹配到相应的视图结果,并返回给 DispatcherServlet
9. DispatcherServlet 接收到具体的 View 视图后,进行视图渲染,将 Model 中的模型数据填充
View 视图中的 request 域,生成最终的 View(视图);
10. 视图负责将结果显示到浏览器(客户端)。

简要流程

1. 服务器启动,应用被加载。读取到 web.xml 中的配置创建 spring 容器并且初始化容器中的
对象。
2. 浏览器发送请求,被 DispatherServlet 捕获,该 Servlet 并不处理请求,而是把请求转发出
去。转发的路径是根据请求 URL,匹配@RequestMapping 中的内容。
3. 匹配到了后,执行对应方法。该方法有一个返回值。
4. 根据方法的返回值,借助 InternalResourceViewResolver 找到对应的结