1.SpringMVC 基础 1.掌握基于 SpringMVC 获取请求参数与响应 json数据操作2.熟练应用基于 REST 风格的请求路径设置与参数传递。3.能够根据实际业务建立前后端开发通信协议并进行实现。4.基于 SSM 整合技术开发任意业务模块功能Spring MVC 简介spring mvc 概述入门案例入门案例工作流程分析Controlller 加载控制PostManSpring MVC概述传统:三层架构WEB --页面数据收集|Service:业务处理|Dao:数据持久化传统一个 servlet 只能处理一个请求。(2)现在进行了改造:ControllerviewModel|service|dao页面:view模型:Model 反馈给客户端。这样一个servlet可以处理多个请求。(3)当前异步调用:html Controller浏览器-----cssVUE ----- Service ----ModelElementUIdaoModel无法直接返回客户端页面。所有需要将其 改为 json格式。然后返回给前端。前端和后端交互使用 json 格式的数据进行交互。后端开发主要是负责controller部分的开发将返回的json数据给到前端展示。Spring MVC 是一种基于 Java实现 MVC模型的轻量级WEB框架。优点:使用简单开发便捷(相比 Servlet),灵活性强。使用 Servlet 技术开发WEB程序流程 。1.创建 web 工程(maven结构)2.设置 tomcat 服务器加载web工程(tomcat插件)3.导入坐标(servlet)4.定义处理请求的功能类(UserServlet)5.设置请求映射(配置映射关系)使用SpringMVC 技术开发web程序流程。1.创建web工程(Maven结构)2.设置tomcat服务器加载web功能(tomcat插件)3.导入坐标(Spring MVCServlet)4.定义处理请求的功能类(UserController)5.设置请求映射(配置映射关系)sout:快捷键:System.out.println(); altEnter :红色时输出抽象类。 pom.xml project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion groupIdorg.example/groupId artifactIdspringmvc_01_quiickstart/artifactId packagingwar/packaging version1.0-SNAPSHOT/version namespringmvc_01_quiickstart Maven Webapp/name urlhttp://maven.apache.org/url dependencies dependency groupIdjavax.servlet/groupId artifactIdjavax.servlet-api/artifactId version3.1.0/version scopeprovided/scope /dependency dependency groupIdorg.springframework/groupId artifactIdspring-webmvc/artifactId version5.2.10.RELEASE/version /dependency /dependencies build finalNamespringmvc_01_quiickstart/finalName plugins plugin groupIdorg.apache.tomcat.maven/groupId artifactIdtomcat7-maven-plugin/artifactId version2.1/version configuration port80/port path//path /configuration /plugin /plugins /build /project ##################################ServletContainerInitConfig.java package com.ycl.config; import org.springframework.lang.Nullable; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer { Override //加载springmvc对应的容器对象。 protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext(); ctx.register(SpringMvcConfig.class); return ctx; } Override //哪些请求在springmvc处理。 protected String[] getServletMappings() { //所有请求都归mvc处理。 return new String[]{/}; } Nullable Override //加载spring的容器的配置的容器对象。 protected WebApplicationContext createRootApplicationContext() { return null; } } ##########################SpringMvcConfig.java package com.ycl.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; Configuration ComponentScan(com.ycl.controller) public class SpringMvcConfig { } ########################## package com.ycl.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; Controller public class UserController { RequestMapping(/save) ResponseBody public String save(){ System.out.println(user save ...); return {info:springmvc}; } }RequestMapping(/save)#控制请求映射路径。ResponseBody#SpringMVC 控制器方法定义上方。作用:设置当前控制器方法响应内容为当前返回值无需解析。AbstractDispatcherServletInitializer 类是Spring MVC 提供了快速初始化 web3.0容器的抽象类。AbstractDispatcherServletInitializer 提供三个接口方法供用户实现。createServletApplicationContext() 方法创建Servlet容器时加载 SpringMVC对应的bean 并放入WebApplicationContext 对象范围中而 WebApplicationContext 的作用范围为 ServletContext 范围即整个web容器范围。protected String[] getServletMappings() {//所有请求都归mvc处理。return new String[]{/};}getServletMappings() 方法设定 SpringMVC 对应的请求映射路径设置为 / 表示拦截所有请求任意请求都将转入到 SpringMVC进行处理。protected WebApplicationContext createRootApplicationContext() {return null;}createRootApplicationContext():方法如果创建 Servlet 容器时需要加载非 SpringMVC 对应的bean,使用当前方法进行。使用方式同 createRootApplicationContext()总结:SpringMVC 入门程序开发总结一次性工作:创建工程设置服务器加载工程导入坐标创建web容器启动类加载 SpringMVC 配置并设置 Spring MVC 请求拦截路径。Spring MVC 核心配置类(设置配置类扫描 controller 包加载Controller控制器bean)多次工作:定义处理请求的控制器类。定义处理请求的控制器方法并配置映射路径(RequestMapping) 与返回 json数据(ResponseBody)#例如:RequestMapping(/save)ResponseBodypublic String save(){System.out.println(user save ...);return {info:springmvc};}