1.SpringMVC概述
1.1.MVC介绍
MVC是一种设计模式,将软件按照模型、视图、控制器来划分:
-
M:Model,模型层,指工程中的JavaBean,作用是处理数据
JavaBean分为两类:
-
一类称为数据承载Bean:专门存储业务数据的,如 Student、User 等
-
一类称为业务处理Bean:指 Service 或 Dao 对象,专门用于处理业务逻辑和数据访问。
-
-
V:View,视图层,指工程中的html或jsp等页面,作用是与用户进行交互,展示数据
- C:Controller,控制层,指工程中的servlet,作用是接收请求和响应浏览器
MVC的工作流程
用户通过视图层发送请求到服务器,在服务器中请求被Controller接收,Controller调用相应的Model层处理 请求,处理完毕将结果返回到Controller,Controller再根据请求处理的结果找到相应的View视图,渲染数据 后最终响应给浏览器
MVC与三层架构的关系:
三层架构的分层模式是典型的上下关系,上层依赖于下层。但MVC作为表现模式是不存在上下关系的,而是相互协作关系。
三层是基于业务逻辑来分的,而mvc是基于页面来分的。
MVC与三层架构的关系:
三层架构的分层模式是典型的上下关系,上层依赖于下层。但MVC作为表现模式是不存在上下关系的,而是相互协作关系。
三层是基于业务逻辑来分的,而mvc是基于页面来分的。
1.2.Spring MVC介绍
-
Spring MVC 是Spring框架的一个模块,是一个基于 MVC 设计模式的轻量级 Web 开发框架,本质上相当于 Servlet。
-
SpringMVC 是 Spring 为表示层开发提供的一整套完备的解决方案。在表述层框架历经 Strust、WebWork、Strust2 等诸多产品的历代更迭之后,目前业界普遍选择了 SpringMVC 作为 Java EE 项目表述层开发的首选方案。
2.SpringMVC 的入门
2.1.环境搭建
2.1.1.创建工程
2.1.2.添加web支持
- 右键项目选择
Add framework support...
2.添加web支
3.效果
-
注意:
-
不要先添加打包方式
-
将web目录要拖拽到main目录下,并改名为webapp
-
2.1.3.pom.xml
md-end-block
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555"><?xml</span> <span style="color:#555555">version="1.0" encoding="UTF-8"?></span>
<span style="color:#117700"><</span><span style="color:#117700">project</span> <span style="color:#0000cc">xmlns</span>=<span style="color:#aa1111">"http://maven.apache.org/POM/4.0.0"</span>
<span style="color:#0000cc">xmlns:xsi</span>=<span style="color:#aa1111">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color:#0000cc">xsi:schemaLocation</span>=<span style="color:#aa1111">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">modelVersion</span><span style="color:#117700">></span>4.0.0<span style="color:#117700"></</span><span style="color:#117700">modelVersion</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>com.by<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>SpringMVC_day01<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>1.0-SNAPSHOT<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!--打包方式--></span>
<span style="color:#117700"><</span><span style="color:#117700">packaging</span><span style="color:#117700">></span>war<span style="color:#117700"></</span><span style="color:#117700">packaging</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">dependencies</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-context<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>5.1.8.RELEASE<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-web<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>5.1.8.RELEASE<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-webmvc<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>5.1.8.RELEASE<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>javax.servlet<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>servlet-api<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>2.5<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">scope</span><span style="color:#117700">></span>provided<span style="color:#117700"></</span><span style="color:#117700">scope</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>javax.servlet.jsp<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>jsp-api<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>2.0<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">scope</span><span style="color:#117700">></span>provided<span style="color:#117700"></</span><span style="color:#117700">scope</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">dependencies</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">build</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">plugins</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!-- 配置Tomcat插件 --></span>
<span style="color:#117700"><</span><span style="color:#117700">plugin</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.apache.tomcat.maven<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>tomcat7-maven-plugin<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>2.2<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">configuration</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!--端口号--></span>
<span style="color:#117700"><</span><span style="color:#117700">port</span><span style="color:#117700">></span>8080<span style="color:#117700"></</span><span style="color:#117700">port</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!--项目名--></span>
<span style="color:#117700"><</span><span style="color:#117700">path</span><span style="color:#117700">></span>/<span style="color:#117700"></</span><span style="color:#117700">path</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">configuration</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">plugin</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">plugins</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">build</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">project</span><span style="color:#117700">></span></span></span>
2.2.入门案例
2.2.1.index.jsp
md-end-block
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#117700"><</span><span style="color:#117700">%@</span> <span style="color:#0000cc">page</span> <span style="color:#0000cc">contentType</span>=<span style="color:#aa1111">"text/html;charset=UTF-8"</span> <span style="color:#0000cc">language</span>=<span style="color:#aa1111">"java"</span> <span style="color:#0000cc">%</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">head</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></span>$Title$<span style="color:#117700"></</span><span style="color:#117700">title</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">a</span> <span style="color:#0000cc">href</span>=<span style="color:#aa1111">"/hello"</span><span style="color:#117700">></span>hello<span style="color:#117700"></</span><span style="color:#117700">a</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span></span>
2.2.2.controller
md-end-block
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555">@Controller</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">HelloController</span> {
<span style="color:#555555">@RequestMapping</span>(<span style="color:#aa1111">"/hello"</span>)
<span style="color:#770088">public</span> <span style="color:#000000">ModelAndView</span> <span style="color:#000000">hello</span>() {
<span style="color:#aa5500">//ModelAndView对象封装了模型数据和视图名称</span>
<span style="color:#000000">ModelAndView</span> <span style="color:#000000">mv</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ModelAndView</span>();
<span style="color:#aa5500">//添加数据,request.setAttribute("hello","hello springmvc!!")</span>
<span style="color:#000000">mv</span>.<span style="color:#000000">addObject</span>(<span style="color:#aa1111">"hello"</span>, <span style="color:#aa1111">"欢迎你 springmvc"</span>);
<span style="color:#aa5500">//设置逻辑视图路径</span>
<span style="color:#000000">mv</span>.<span style="color:#000000">setViewName</span>(<span style="color:#aa1111">"success"</span>);
<span style="color:#aa5500">//返回数据和视图</span>
<span style="color:#770088">return</span> <span style="color:#000000">mv</span>;
}
}</span></span>
2.2.3.springmvc.xml
md-end-block
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555"><?xml</span> <span style="color:#555555">version="1.0" encoding="UTF-8"?></span>
<span style="color:#117700"><</span><span style="color:#117700">beans</span> <span style="color:#0000cc">xmlns</span>=<span style="color:#aa1111">"http://www.springframework.org/schema/beans"</span>
<span style="color:#0000cc">xmlns:xsi</span>=<span style="color:#aa1111">"http://www.w3.org/2001/XMLSchema-instance"</span> <span style="color:#0000cc">xmlns:p</span>=<span style="color:#aa1111">"http://www.springframework.org/schema/p"</span>
<span style="color:#0000cc">xmlns:context</span>=<span style="color:#aa1111">"http://www.springframework.org/schema/context"</span>
<span style="color:#0000cc">xmlns:mvc</span>=<span style="color:#aa1111">"http://www.springframework.org/schema/mvc"</span>
<span style="color:#0000cc">xsi:schemaLocation</span>=<span style="color:#aa1111">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span>
<span style="color:#aa1111">http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd</span>
<span style="color:#aa1111">http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!-- 配置创建 spring 容器要扫描的包 --></span>
<span style="color:#117700"><</span><span style="color:#117700">context:component-scan</span> <span style="color:#0000cc">base-package</span>=<span style="color:#aa1111">"com.by"</span><span style="color:#117700">></</span><span style="color:#117700">context:component-scan</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!-- 配置视图解析器 --></span>
<span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"prefix"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"/WEB-INF/pages/"</span><span style="color:#117700">></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"suffix"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">".jsp"</span><span style="color:#117700">></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!--开启springmvc注解支持:配置HandlerMapping和HandlerAdapter--></span>
<span style="color:#117700"><</span><span style="color:#117700">mvc:annotation-driven</span><span style="color:#117700">></</span><span style="color:#117700">mvc:annotation-driven</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">beans</span><span style="color:#117700">></span></span></span>
2.2.4.success.jsp
md-end-block
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#117700"><</span><span style="color:#117700">%@</span> <span style="color:#0000cc">page</span> <span style="color:#0000cc">contentType</span>=<span style="color:#aa1111">"text/html;charset=UTF-8"</span> <span style="color:#0000cc">language</span>=<span style="color:#aa1111">"java"</span> <span style="color:#0000cc">%</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">head</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></span>Title<span style="color:#117700"></</span><span style="color:#117700">title</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">h2</span><span style="color:#117700">></span>${msg}<span style="color:#117700"></</span><span style="color:#117700">h2</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span></span>
2.2.5.web.xml
md-end-block
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555"><?xml</span> <span style="color:#555555">version="1.0" encoding="UTF-8"?></span>
<span style="color:#117700"><</span><span style="color:#117700">web-app</span> <span style="color:#0000cc">xmlns</span>=<span style="color:#aa1111">"http://xmlns.jcp.org/xml/ns/javaee"</span>
<span style="color:#0000cc">xmlns:xsi</span>=<span style="color:#aa1111">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color:#0000cc">xsi:schemaLocation</span>=<span style="color:#aa1111">"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span>
<span style="color:#0000cc">version</span>=<span style="color:#aa1111">"4.0"</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>springmvc<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>org.springframework.web.servlet.DispatcherServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!-- 配置初始化参数,用于读取 SpringMVC 的配置文件 --></span>
<span style="color:#117700"><</span><span style="color:#117700">init-param</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>contextConfigLocation<span style="color:#117700"></</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>classpath:springmvc.xml<span style="color:#117700"></</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">init-param</span><span style="color:#117700">></span>
<span style="color:#aa5500"><!-- 表示容器在启动时立即创建servlet对象 --></span>
<span style="color:#117700"><</span><span style="color:#117700">load-on-startup</span><span style="color:#117700">></span>1<span style="color:#117700"></</span><span style="color:#117700">load-on-startup</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>springmvc<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
<span style="color:#117700"><</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>/<span style="color:#117700"></</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">web-app</span><span style="color:#117700">></span></span></span>