SpringMVC 简介

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring MVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts 2(一般老项目使用)等。

@WebServlet中配置”/“,控制器拦截所有请求,但是不会拦截到.jsp文件

@WebServlet中配置”/*“,控制器拦截所有请求,包括拦截到.jsp文件

image-20191220205557956

image-20191220210149241

SpringMVC 中重要组件

  • DispatcherServlet:前端控制器,接收所有请求(如果配置/不包含jsp)
  • HandlerMapping:解析请求格式的.判断希望要执行哪个具体的方法.
  • HandlerAdapter:负责调用具体的方法.
  • ViewResovler:视图解析器,解析结果,准备跳转到具体的物理视图

SpringMVC运行原理图

image-20191220211848680

1
2
3
4
5
6
7
8
9
10
11
12
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="dispatcher?control=demo1">跳转1</a>
<a href="dispatcher?control=demo2">跳转2</a>
<a href="dispatcher?control=demo3">跳转3</a>
<a href="dispatcher?control=demo4">跳转4</a>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.kylin.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* /不拦截jsp
* /* 拦截所有,包括jsp
*/
@WebServlet("/")
public class Dispatcher extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行控制器");
String result = req.getParameter("control");
System.out.println(result);
if ("demo1".equals(result)){
demo1();
}
if ("demo2".equals(result)){
demo2();
}
if ("demo3".equals(result)){
dem03();
}
if ("demo4".equals(result)){
dem04();
}
}
public void demo1() {
System.out.println("控制器执行demo1");
}

public void demo2() {
System.out.println("控制器执行demo2");
}
public void dem03() {
System.out.println("控制器执行demo3");
}
public void dem04() {
System.out.println("控制器执行demo4");
}
}

SpringMVC环境搭建-纯配置文件

导入jar

image-20191221152428789

配置web.xml

首先在web.xml文件中配置SpringMVC的前端控制器,通过配置初始化参数contextConfigLocation修改服务器加载SpringMVC的配置文件路径。默认加载路径是在WEB-INF下寻找配置文件加载。

image-20191221131736663

配置springmvc.xml

在SpringMVC的配置文件中,配置HandlerMapping,HandlerAdapter,viewResolver

image-20191221133446175

编写controller进行测试

image-20191221133816436

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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">
<!--配置前端控制器-->
<servlet>
<servlet-name>springmvc</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>
</init-param>
<!--自启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--拦截除了jsp外的所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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">
<bean id="demo123" class="com.kylin.controller.DemoController"></bean>
<!--HandlerMapping:解析请求格式,判断希望要执行哪个具体的方法。-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap" >
<map>
<!--key表示解析出来控制器的逻辑名,servlet的别名-->
<entry key="demo" value-ref="demo123"></entry>
</map>
</property>
</bean>
<!--HandlerAdapter:负责调用具体的方法-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--视图解析器:解析结果。准备跳转到具体的物理视图-->
<!--不配id可能不好使-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--<property name="prefix" value="/"></property>-->
<!--<property name="suffix" value=".jsp"></property>-->
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.kylin.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DemoController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
System.out.println("执行了springmvc的控制器");
ModelAndView mav = new ModelAndView("/main.jsp");
return mav;
}
}