一篇文章带你掌握主流服务层框架——SpringMVC

一篇文章带你掌握主流服务层框架——SpringMVC在之前的文章中我们已经学习了Spring的基本内容 , SpringMVC隶属于Spring的一部分内容
但由于SpringMVC完全针对于服务层使用,所以我们在介绍时常常把SpringMVC单独当作一个大章节来学习

温馨提醒:在学习SpringMVC前请确保已学习Spring内容
SpringMVC简介首先我们先来简单了解一下SpringMVC:
  • SpringMVC是一种基于Java实现MVC模型的轻量级Web框架
  • SpringMVC致力于服务层,同Servlet一样应用于服务层 , 用于服务层开发
  • SpringMVC隶属于Spring,同样具有简化代码,使用简单,开发便捷,灵活性强的优点
SpringMVC入门案例在未学习SpringMVC之前,我们的服务端开发通常采用Servlet:
package com.itheima.web.servlet.old;import com.alibaba.fastjson.JSON;import com.itheima.pojo.Brand;import com.itheima.service.BrandService;import com.itheima.service.impl.BrandServiceImpl;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.BufferedReader;import java.io.IOException;import java.util.List;//@WebServlet("/addServlet")public class AddServlet extends HttpServlet {private BrandService brandService = new BrandServiceImpl();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1. 接收品牌数据BufferedReader br = request.getReader();String params = br.readLine();//json字符串//转为Brand对象Brand brand = JSON.parseObject(params, Brand.class);//2. 调用service添加brandService.add(brand);//3. 响应成功的标识response.getWriter().write("success");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}我们可以注意到其过程非常繁琐,因为我们需要获取参数并进行类型转换 , 包括添加至Service等过程
但是SpringMVC秉承着简化代码的原则,将大部分内容转化为Java代码进行封装,大大减少了繁琐的过程
接下来我们来介绍SpringMVC版:
  1. 导入jar包
<?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.itheima</groupId><artifactId>springmvc_01_quickstart</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><dependencies><!--servlet坐标--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!--SpringMVC坐标--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.10.RELEASE</version></dependency></dependencies><!--Tomcat配置--><build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><port>80</port><path>/</path></configuration></plugin></plugins></build></project>
  1. 创建SpringMVC控制类(等同于Servlet类)
package com.itheima.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;//定义表现层控制器bean@Controllerpublic class UserController {//设置映射路径为/save,即外部访问路径@RequestMapping("/save")//设置当前操作返回结果为指定json数据(本质上是一个字符串信息)@ResponseBodypublic String save(){System.out.println("user save ...");return "{'info':'springmvc'}";}//设置映射路径为/delete , 即外部访问路径@RequestMapping("/delete")@ResponseBodypublic String delete(){System.out.println("user save ...");return "{'info':'springmvc'}";}}
  1. 初始化SpringMVC环境(同Spring一样创建Config配置Java类)
package com.itheima.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;//springmvc配置类,本质上还是一个spring配置类@Configuration@ComponentScan("com.itheima.controller")public class SpringMvcConfig {}
  1. 初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理的请求

    推荐阅读