SpringMVC课堂笔记
1、SpringMVC概述
1.1 SpringMVC概念
SpringMVC 也叫 Spring web mvc。是 Spring内置的一个MVC框架,在 Spring3.0 后发布。SpringMVC
框架解决了WEB开发中常见的问题(参数接收、文件上传、表单验证等等),而且使用简单,与Spring无
缝集成。支持 RESTful风格的URL请求。采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性
和灵活性。
1.2 SpringMVC原理
在没有使用SpringMVC之前我们都是使用Servlet在做Web开发。但是使用Servlet开发在接收请求参数,
数据共享,页面跳转等操作相对比较复杂。servlet是java进行web开发的标准,既然springMVC是对
servlet的封装,那么很显然SpringMVC底层就是Servlet,SpringMVC就是对Servlet进行深层次的封
装。
1.3 SpringMVC优势
1、基于 MVC 架构,功能分工明确。解决页面代码和后台代码的分离。
2、简单易用。SpringMVC 也是轻量级的,jar 很小。不依赖的特定的接口和类就可以开发一个注解的
SpringMVC 项目。
3、作 为 Spring 框 架 一 部 分 , 能 够 使 用Spring的IoC和AOP 。 方 便 整 合MyBatis,Hiberate,JPA等
其他框架。
4、springMVC的注解强大易用。
2、MVC模式回顾
模型1:jsp+javabean模型---在jsp页面中嵌入大量的java代码
模型2:jsp+servlet+javabean模型---jsp页面将请求发送给servlet,由servlet调用javabean,再
由servlet将制定jsp页面响应给用户。
模型2一般就是现在的MVC模式,也是我们一直使用的。
Model-View-Controller:模型--视图--控制器
Model: 模型层 javaBean 负责数据访问和业务处理 dao service pojo
View: 视图 JSP技术 负责收集和展示数据
Controller: 控制器 servlet技术 中间调度
控制器的工作:
1、接受客户端的请求(包括请求中携带的数据)
2、处理请求:调用后台的模型层中的业务逻辑
3、页面导航:处理完毕给出响应:JSP页面
3、入门程序
3.1 创建maven项目
创建项目并补齐目录结构
3.2 pom.xml文件添加依赖和插件
<!--web项目-->
<packaging>war</packaging>
<dependencies>
<!--spring-webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!--springmvc底层还是servlet,所以必须添加serlvet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope><!--插件运行的时候没有scope,插件启动可能会失败-->
</dependency>
</dependencies>
<build>
<plugins>
<!-- 编码和编译和JDK版本 -->
3.3 创建Spring和SpringMVC的配置文件
我们一般将除了 Controller 之外的所有 Bean 注册到 Spring 容器中,而将 Controller 注册到
SpringMVC 容器中。所以我们在 resources 目录下添加 applicationContext.xml 作为 spring 的配置,
添加springmvc.xml作为springmvc的配置文件。
3.3.1 创建Spring配置文件applicationContext.xml文件
3.3.2 创建SpringMVC的配置文件springmvc.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
<?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
">
<!--spring的配置文件:除了控制器之外的bean对象都在这里扫描-->
<context:component-scan base-package="com.lina.dao,com.lina.service"/>
</beans>
3.4 在web.xml中进行Spring和SpringMVC配置
<?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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--springmvc的配置文件:控制器的bean对象都在这里扫描-->
<context:component-scan base-package="com.lina.controller"/>
</beans>
<!--spring的配置-->
<context-param>
<!--contextConfigLocation:表示用于加载 Bean的配置文件-->
<param-name>contextConfigLocation</param-name>
<!--指定spring配置文件的位置
这个配置文件也有一些默认规则,它的配置文件名默认就叫 applicationContext.xml ,
如果将这个配置文件放在 WEB-INF 目录下,那么这里就可以不用 指定配置文件位置,
只需要指定监听器就可以。
这段配置是 Spring 集成 Web 环境的通用配置;一般用于加载除控制器层的 Bean(如
dao、service 等),以便于与其他任何Web框架集成。
-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--SpringMVC的配置-->
<!--
前端控制器:所有的请求都会经过此控制器,然后通过此控制器分发到各个分控制器.
前端控制器本质上还是一个Servlet,因为SpringMVC底层就是使用Servlet编写的
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 创建前端控制器的时候读取springmvc配置文件启动ioc容器 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml</param-value>
</init-param>
<!-- Tomcat启动完毕就创建此对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置拦截路径url,所有以.do结尾的请求都会被前端控制器拦截处理 -->
<servlet-mapping>
3.5 创建控制器
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--
SpringMVC的配置解析:
1、servlet-class:前端控制器的完全限定名,在spring-webmvc-5.2.13.RELEASE.jar包中的
org.springframework.web.servlet下
2、load-on-startup:标记是否在Web服务器(这里是Tomcat)启动时会创建这个 Servlet 实
例,即是否在 Web 服务器启动时调用执行该 Servlet 的 init()方
法,而不是在真正访问时才创建。 要求取值是整数。
值大于0:表示容器在启动时就加载并初始化这个 servlet,数值越小,该 Servlet的优先级就
越高,其被创建的也就越早
值小于0或者省略:表示该 Servlet 在真正被使用时才会去创建。
值相同:容器会自己选择创建顺序
3、url-pattern:可以写为 / ,可以写为*.do 、*.action、*.mvc等形式,此处先写*.do,以后
介绍不同写法的区别。
4、init-param:表示了springmvc配置文件的名称和位置。如果没有配置,默认在项目的WEB-INF
目录下找名称为 Servlet 名称-servlet.xml 的配置文件。
如果没有配置,启用默认的规则:即如果配置文件放在 webapp/WEB-INF/ 目录下,并且配置文
件的名字等于 DispatcherServlet 的名字+ -servlet(即这里的配置文件路径是 webapp/WEB-
INF/dispatcherServlet-servlet.xml),如果是这样的话,可以不用添加 init-param 参数,即不
用手动配置 springmvc 的配置文件,框架会自动加载。
而一般情况下,配置文件是放在类路径下,即 resources 目录下。所以,在注册前端控制器时,
还需要设置查找 SpringMVC 配置文件路径。
其中contextConfigLocation属性:来自DispatcherServlet的父类FrameworkServlet,
该类中的contextConfigLocation属性用来配置springmvc的路径和名称。
-->
package com.lina.controller;
import com.lina.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* ClassName: TeamController
*
* @author wanglina
* @version 1.0
*/
@Controller
public class TeamController {
@Autowired
private TeamService teamService;
@RequestMapping("hello.do")
public ModelAndView add(){
System.out.println("TeamController----add---");
teamService.add();
ModelAndView mv=new ModelAndView();