本章主要是完成Spring MVC项目的初始搭建以及hello world的展示,涉及到的知识点有:
1. 前端控制器DispatcherServlet : 解析前端请求,根据处理映射器HandlerMapping分发到相应的控制器。
2. 处理映射器HandlerMapping : 在服务启动时,spring会根据xml和注解加载bean,形成URI与控制器的映射关系。
2. 上下文加载监听器ContextLoaderListener + 上下文配置文件位置contextConfigLocation:组合起来,获取spring的配置文件
3. context:component-scan:开启@Component注解,自动定义bean
4. context:annotation-config:开启注解
5. InternalResourceViewResolver:视图解析器,讲控制器返回的逻辑视图解析成真正的页面。
1.关于开发环境
- IDE : IntelliJ IDEA Ultimate 2017.3
- Maven : Apache Maven 3.5.2
- JDK : 1.8.0_131
- Database : Mysql 5.7.17
- DB IDE : Mysql workbench 6.3.8
- archetype : org.apache.maven.archetype:maven-archetype-webapp
- run configuration : clean tomcat7:run -e
- package configuration : clean package -e
2.目录结构
project
\---src
| \---main
| \---java
| | \---pers
| | \---hanchao
| | \---hespringmvc
| | \---hello
| | \---HelloController.java
| \---resources
| | \---applicationcontext.xml
| \---webapp
| \---helloworld
| | \---helloworld.jsp
| \---WEB-INF
| | \---spring-mvc-servlet.xml
| | \---web.xml
| \---index.jsp
\---pom.xml
3.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pers.hanchao</groupId>
<artifactId>hispringmvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>hispringmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<final-name>hispringmvc</final-name>
<springframework.version>5.0.0.RELEASE</springframework.version>
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
<servlet-api.version>3.1.0</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<javax-mail.version>1.5.0-b01</javax-mail.version>
<commons-logging.version>1.2</commons-logging.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<commons-io.version>2.4</commons-io.version>
<tomcat-plugin.version>2.2</tomcat-plugin.version>
</properties>
<dependencies>
<!--基本包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>
<!--javax.mail-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javax-mail.version}</version>
</dependency>
<!--Cannot find the class file for javax.servlet.ServletContext.-->
<!-- servlet的jar -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- 验证的jar -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${springframework.version}</version>
</dependency>
<!--mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>hispringmvc</finalName>
<plugins>
<!--Maven Tomcat 组件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat-plugin.version}</version>
<configuration>
<path>/</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Hi Spring MVC</display-name>
<!--spring-mvc前端分发器DispatcherServlet-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--spring上下文配置加载器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--spring配置文件总入口-->
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>
<!--上下文加载监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--欢迎页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.spring-mvc-servlet.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">
<!--开启component注解自动扫描-->
<context:component-scan base-package="pers.hanchao.*"/>
<!--开启注解-->
<context:annotation-config/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6.applicationcontext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
7.HelloController.java
package pers.hanchao.hespringmvc.hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* <p>Hi My Spring MVC</p>
* @author hanchao 2018/1/13 16:50
**/
@Controller
public class HelloController {
@GetMapping("/helloworld")
public String helloWorld(Model model){
model.addAttribute("helloworld","Hi Spring MVC");
return "helloworld/helloworld";
}
}
8.index.jsp
<html>
<body>
<h2>Hello World!</h2>
<a href="/helloworld">Hello World!</a>
</body>
</html>
9.helloworld.jsp
<%--
Created by IntelliJ IDEA.
User: hanchao
Date: 2018/1/13
Time: 15:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>${helloworld}</h3>
</body>
</html>
10.result
11.本实例中Spring MVC的工作原理
流程说明:
- index.jsp页面向服务发送
GET
请求http://localhost:8080/helloworld
,被前端控制器DispatcherServlet
捕获。 - 前端控制器DispatcherServlet截取出URI为
/helloworld
,然后查询HandlerMapping
。 HandlerMapping
根据/helloworld
,获取对应的控制器HelloController
以及方法helloworld()
,返回给前端控制器DispatcherServlet
。- 前端控制器
DispatcherServlet
提取HttpServletRequest
,调用HelloController
的helloworld()
方法。 HelloController
的helloworld()
进行业务处理- 处理完成后,以
ModelAndView
对象的形式返回给前端控制器DispatcherServlet
。 - 前端控制器
DispatcherServlet
接收ModelAndView
,从中提取出View视图"helloworld/helloworld"
,然后查询视图解析器InternalResourceViewResolver
。 - 视图解析器
InternalResourceViewResolver
根据View视图"helloworld/helloworld"
查询出真正的页面:/helloworld/helloworld.jsp
。 - 视图解析器
InternalResourceViewResolver
将真正的页面:/helloworld/helloworld.jsp
返回给前端控制器DispatcherServlet
。 - 前端控制器
DispatcherServlet
将从ModelAndView
提取出的Model模型"helloword"-"Hi Spring MVC"
与View视图"helloworld/helloworld"
结合。 - 前端控制器
DispatcherServlet
调用页面/helloworld/helloworld.jsp
,并将Model模型中的信息"helloword"-"Hi Spring MVC"
填写到页面的占位字段${helloworld}
中。 - 最终,我们在页面上看到了
Hi Spring MVC
的效果。
备注:
1. 图中关于HandlerMapping
关联的假想映射表只是为了方便理解而画的,并不是HandlerMapping
的真正样子。
2. 这张图是我自己的理解,如果各位觉得有哪里不对,请多多指教,不胜感激。
3. 图片可能在文章中看不清,可以考虑通过右键->在新标签页中打开图片
的方式解决。