1、SpringMVC介绍
1、SpringMVC概述:
2、MVC模式回顾
1、jsp+servlet+javabean模型---jsp页面将请求发送给servlet,由servlet调用javabean,再
3、入门程序
1、第一步:创建web项目,创建完成之后,在pom.xml配置文件中修改他的打包方式。然后完成
补全他的项目骨架部分。
2、第二步:因为是SpringMVC项目,所有要在pom.xml中导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zj</groupId>
<artifactId>springMVC01</artifactId>
<version>1.0-SNAPSHOT</version>
<!--javaWeb项目打包的方式-->
<packaging>war</packaging>
<!--导入依赖包-->
<dependencies>
<!--spring-webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!--springMVC底层还是servlet,所以要添加servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<!--由于tomcat插件中还有这个jar包,所以在打包的时候,不因该将该包放入到war包中-->
<scope>provided</scope>
</dependency>
</dependencies>
<!--配置web项目运行时候的插件-->
<build>
<plugins>
<plugin>
<!--编码和编译和jdk版本-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<!--配置jdk版本-->
<configuration>
<source>11.0.6</source>
<target>11.0.6</target>
</configuration>
</plugin>
<!--配置tomcat7的插件-->
<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>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
3、第三步:创建两个配置文件applicationContext.xml 和 springMVC.xml 配置文件
一个作为 Spring 容器,一个作为 springmvc 容器。因为要对包里面的类进行扫描,所
以在Spring容器中进行一下配置
SpringMVC.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
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
">
<context:component-scan base-package="com.zj.controller"/>
</beans>
applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.zj.service,com.zj.dao,com.zj.pojo"/>
</beans>
4、第四步:对web.xml进行配置(当项目一启动的时候,首先扫描的就是web.xml文件),扫描到
配置文件后,就会加载两个容器,一个是appicationContext.xml文件,一个是
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">
<!--当web项目启动时候,首先扫描的就是web.xml配置文件,通过扫描配置文件,就可以找到applicationContext.xml文件和springMVC.xml文件
然后进行相应的操作。-->
<!--spring配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springMVC配置:springMVC前端/核心/中央控制器-->
<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>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
5、第五步:编写service和controller
service:
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
//通过注解的方式将类对象的创建交给spring容器
@Service
public class TeamService {
public void add(){
System.out.println("TeamService------add------");
}
}
controller:
import com.zj.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;
//通过注解的方式将创建对象的权力交给spring容器
@Controller
public class TeamController {
//通过注解的方式将对象注入,这样就可以不用自己 new 对象了
@Autowired
private TeamService teamService;
//该注解是方法的映射(spring要怎么知道当请求hello的时候要到这里来找该方法,这个时候就需要我们配置springMVC.xml文件了。
@RequestMapping("hello.do")
public ModelAndView add(){
System.out.println("TeamController------add------");
//1、调用service层方法,去处理业务逻辑
teamService.add();
//2、携带数据
ModelAndView mv = new ModelAndView();
mv.addObject("teamName","湖人队");//相当于以前的request.setAttribute("key","value");
//3、返回值
mv.setViewName("index");//相当于以前的跳转页面,未来经过Spring容器的试图解析器处理,转换为物理路径
//经过internalResourceViewResolver对象处理之后,加上前后缀就成了 "/index.jsp"
return mv;
}
}
第六步:测试
4、解析
1、当 Spring 和 SpringMVC 同时出现,我们的项目中将存在两个容器,一个是 Spring 容器,另 一 个是 SpringMVC 容器,Spring 容器通过 ContextLoaderListener 来加载,SpringMVC 容
器则通过 DispatcherServlet 来加载,这两个容器不一样:
5、SpringMVC工作流程
SpringMVC组件