SpringMVC
SpringMVC 概述
springMVC 是 spring 框架的一个模块,springMVC 和 spring 无需通过中间 整合层进行整合。 springmvc 是一个基于 mvc 的 web 框架,方便前后端数据的传输. Spring MVC 拥有控制器,接收外部请求,解析参数传给服务层。
SpringMVC运行流程
- 浏览器发起请求—>地址(login/toLogin)—>被前端请求分发器(DispatcherServlet)拦截;
- DispatcherServlet调用,HandleMapping(映射控制器)将解析结果返回给DispatcherServlet,找到处理器,控制器,如果配置了拦截器还会调用拦截器。
- DispatcherServlet调用对应的控制器,在到达控制器之前,会先进入HandleAdapter(处理适配器),对参数进行封装,数据类型的转换等。
- 最终到我们自己写的控制器中,接受数据库处理返回的ModelAndView(包含视图名,数据等)返回给。DispatcherServlet。
- 把Model(数据)交给view(jsp)。
- 由jsp给出最终影响。
搭建SpringMVC
导包
<dependencies>
<!--springMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
配置DispatcherServlet
在 web.xml 文件中配置 DispatcherServlet
<!--配置spring中核心请求转发Servlet,由此Servlet对请求进行拦截处理-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--在init方法中读取参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 找 web-inf/classes/配置文件 -->
<param-value>classpath:spring.xml</param-value>
</init-param>
<!--服务器启动时创建servler对象-->
<load-on-startup>0</load-on-startup>
</servlet>
<!-- 请求映射 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
开启SpringMVC注解
<mvc:annotation-driven></mvc:annotation-driven>
控制类搭建
@Controller 用于标记在一个类上,使用它标记的类就是一个 SpringMVC
Controller 对象
@RequestMapping 注解是一个用来处理请求地址映射的注解,可用于类或方 法上。
用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
@org.springframework.stereotype.Controller
@RequestMapping(path = "/login")
public class Controller {
@RequestMapping(path = "/toLogin")
public ModelAndView toLogin() {
/*viewname为视图名,通过视图解析器查找jsp*/
ModelAndView mv = new ModelAndView("login");
return mv;
}
@RequestMapping(path = "login")
public ModelAndView login(String name, String password) {
ModelAndView mv = new ModelAndView("success");
mv.addObject("account:" + name);
mv.addObject("password" + password);
return mv;
}
}
视图解析
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/login/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
接受请求
@Controller
Spring 配置中指定了自动扫描的 basepackage 后,Spring 会扫描这些包以及 子包中的使用了@Controller 标识的类,然后将类加入到 Spring IOC 容器中, 注入依赖。需要注意的是这个 bean 在 spring IOC 容器中是单例的,每次请求 到来的时候,使用的是同一个 bean 对象。
@RequestMapping
它的作用就是与请求相匹配,如果匹配上了,所修饰的方法才会被执行。这里我们只需要关注两个属性: value :请求的路径,这个路径相对于应用的上下文,它是 path 的别名。类型是 一个 String[] ,也就是说它可以匹配多个请求路径 。method: 请求的方法。我们知道 HTTP 协议的请求方式有 GET 和 POST. 或者使用@GetMapping, @PostMapping 当前的请求只有与@RequestMapping 上指定的属性都匹配的时候,才会执行 它 标 注 的 方 法 。
获取请求数据
我们编写处理器方法的目的,就是为了处理客户端提交的数据,而客户端的提交 是按照 HTTP 协议报文格式来提交的,如 果 请 求 参 数 的 名 称 与 处 理 器 方 法 中 的 参 数 名 称 相 同 , 那 么 在 使 用 @RequestParam 绑定的时候,可以省略参数,甚至连@RequestParam 都可 以省略。
eg:使用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.example</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ssm</name>
<packaging>war</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.6.2</junit.version>
</properties>
<dependencies>
<!--springMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
</project>
spring.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--spring开启注解扫描-->
<context:component-scan base-package="com.ff.ssm"></context:component-scan>
<!--开启注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<import resource="springMVC.xml"></import>
</beans>
web.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">
<!--配置spring中核心请求转发Servlet,由此Servlet对请求进行拦截处理-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--在init方法中读取参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 找 web-inf/classes/配置文件 -->
<param-value>classpath:spring.xml</param-value>
</init-param>
<!--服务器启动时创建servler对象-->
<load-on-startup>0</load-on-startup>
</servlet>
<!-- 请求映射 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springMVC.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启SpringMVC 注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器-->
<!--prefix前缀,匹配页面的前缀-->
<!--suffix,匹配页面的后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/login/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
控制类搭建
package com.ff.ssm.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@org.springframework.stereotype.Controller
@RequestMapping(path = "/login")
public class Controller {
@RequestMapping(path = "/toLogin")
public ModelAndView toLogin() {
/*viewname为视图名,通过视图解析器查找jsp*/
ModelAndView mv = new ModelAndView("login");
return mv;
}
@RequestMapping(path = "login")
public ModelAndView login(String name, String password) {
ModelAndView mv = new ModelAndView("success");
mv.addObject("account:" + name);
mv.addObject("password" + password);
return mv;
}
}
新建login.jsp
<%--
Created by IntelliJ IDEA.
User: 云
Date: 2021/5/20
Time: 20:41
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>
<form method="post" action="/ssm/login/login">
账号:<input type="text" name="account">
<br>
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
新建success.jsp
<%--
Created by IntelliJ IDEA.
User: 云
Date: 2021/5/20
Time: 21:35
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>欢迎您登录成功!!!</h3>
</body>
</html>
在springMVC.xml中配置视图解析器
运行结果
填入账号为:“admin” 密码为:“111”,点击提交。