Spring MVC

Spring MVC 框架介绍

核心

@RestController  //类直接可以被浏览器访问

@RequestMapping("abc")//方法可以被浏览器访问 注解必须添加参数,参数是用来服务器访问的路径 例如   http://localhost:8080/adc

System.out.println("lbwdsb"); //返回IDEA控制台,浏览器中看不见

return "hello spring boot";//返回到浏览器

概述

Spring MVC输入Spring Franme work(Spring 框架) 的后续产品,.SpringMVC就是基于MVC设计模式来实现的.MVC设计模式保证了程序的松耦合,提高了程序的复用性

SSM框架中,第一个S就是指Sping MVC 框架,框架提供了很多类,由框架控制每个类的调用过程流程

M  就是Model层(数据处理模型,)  V就是View 视图层(HTML 展示数据) C就是Controller 控制层

SpingMVC 主要的作用:1接受请求(解析请求参数) 2,做出相应

MVC模型

用来进行分层的结构,这样代码分离结构清晰,各层代码,各司其职,易于开发大型项目

MVC(Model模型 , View 视图,control控制层),将软件进行分层达成松耦合的效果

通用的软件编程思想,在MVC设计模式中认为,任何软件都可以分三层:控制层(Controller),数据层(Model),负责展示的视图(View)

在MVC设计思想中要求一个符合MVC设计思想的软件应该保证上面这三部分相互独立,互不干扰,每一个部分只负责自己擅长的部分。如果某一个模块发生变化,应该尽量做到不影响其他两个模块。提高代码的可读性,实现程序间的松耦合、提高代码复用性。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mvoYWfxs-1623044111682)(RackMultipart20210607-4-13lyxac_html_baaade7ec47b0169.jpg)]

 工作原理

 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XH0fEJst-1623044111711)(RackMultipart20210607-4-13lyxac_html_e58ee630c0df1991.png)]

 过程简单描述

客户端发出请求,前端控制器就是客户端发出的请求,找到处理器映射器解析请求,处理器适配器会根据处理器映射器返回的结果来调用真正的处理器来处理请求,并处理相应的业务逻辑,处理器返回一个模型视图,视图解析器进行解析,返回一个视图对象,前端控制器渲染数据,将得到视图对象返回给用户

过程的具体描述

1.用户发出请求,发送到前端控制器(HadnleMapping)

2 前端控制器(HadnlMapping)收到请求发出URL请求调用处理映射器(HandlerMapping)出来映射器

3 处理映射器找到具体的处理器(注解) 例如@Restcontroller 与@RequestMapping,生成处理器对象以及处理器拦截对象(如果有则生成)一并返回给DispatcherServlet

4 DispatcherServle 调用HandlerAdapre处理配置器

5、HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)。

6、Controller执行完成返回ModelAndView。

7、andlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。

8、DispatcherServlet将ModelAndView传给ViewReslover视图解析器。

9、ViewReslover解析后返回具体View。

10、DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。

11、DispatcherServlet响应用户。

职责

前端控制器 只获得请求 负责调度
处理器映射器 找到请求要获得的类方法
处理器适配器  开始干活
视图解析器  找到展示视图的网页
视图渲染  结合数据,在视图解析器找到的页面渲染数据

案例

启动类

package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//标记着这是spring boot 的启动类
@SpringBootApplication
public class RunApp {
    //RunApp 必须与MVC 同包,同级
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class);
    }
}

准备Car类

package cn.tedu.mvc;

/**
 * 创建一个Car对象  充当了MVC设计模式中的Model 封装数据
 */
public class Car {
    private int id;
    private String name;
    private String type;
    private String color;
    private double price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Car() {
    }

    public Car(String name) {
        this.name = name;
    }

    public Car(int id, String name, String type, String color, double price) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.color = color;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }
}

通过Get方式提交,获得用户信息实例

package cn.tedu.mvc;
/**
 * 错误400 找到了页面 但参数类型不匹配
 * 错误404 找不到页面
 * 错误500 idea 内部出现了错误
 */

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Spring MVC 解析get请求参数
 */
@RestController//接受请求,做出相应
@RequestMapping("getc")//规定了浏览器的访问方式
public class GetController {
    //http://localhost:8080//getc/param?id=1  form表单get提交方法

    //http://localhost:8080//getc/param?id=1&name="张三"
    //http://localhost:8080//getc/param?id=1   //您的请求参数里id =1您的请求参数里name =null
    //http://localhost:8080//getc/param?id=1&name="张三"&pass="撒"  只能id,name 的情况下,也能解析到id,name

    @RequestMapping("param1")
    public Car param(Car car){//将对象所有属性都获得
        return car;
    }



/*    @RequestMapping("param")
    public String param(int id,String name){//参数列表必须一致 不能乱写
        return "您的请求参数里id =" + id + "您的请求参数里name =" + name;
    }*/



   /* @Test
    public void jq(){
        //http://localhost:8080/car/insert?id=1&name=张三&age=18 解析数据
        String a = "http://localhost:8080/car/insert?id=1&name=张三&age=18";
        String[] a1 = a.split("\\?");
        System.out.println(Arrays.toString(a1));
        String[] a2 = a1[1].split("\\&");
        System.out.println(Arrays.toString(a2));

        for(int i = 0;i < a2.length;i++){
            String[] a3 = a2[i].split("\\=");// \\转义字符
            System.out.println(a3[1]);
        }


    }*/

    //数组方式
/* @RequestMapping("getj")
    public String[] jq(){
        //http://localhost:8080/car/insert?id=1&name=张三&age=18 解析数据
        String a = "http://localhost:8080/car/insert?id=1&name=张三&age=18";
        String[] a1 = a.split("\\?");
        System.out.println(Arrays.toString(a1));
        String[] a2 = a1[1].split("\\&");
        System.out.println(Arrays.toString(a2));
        //string方式
        String[] a4 = new String[a2.length];
        for(int i = 0;i < a2.length;i++){
            String[] a3 = a2[i].split("\\=");// \\转义字符
            System.out.println(a3[1]);
            a4[i] = a3[1];
        }
return a4;

    }*/

//集合方式
 /*   @RequestMapping("getj")
    public ArrayList<String> jq(){
        //http://localhost:8080/car/insert?id=1&name=张三&age=18 解析数据
        String a = "http://localhost:8080/car/insert?id=1&name=张三&age=18";
        String[] a1 = a.split("\\?");
        System.out.println(Arrays.toString(a1));
        String[] a2 = a1[1].split("\\&");
        System.out.println(Arrays.toString(a2));
        //集合
        ArrayList<String> a4 = new ArrayList<>();
        for(int i = 0;i < a2.length;i++){
            String[] a3 = a2[i].split("\\=");// \\转义字符
            System.out.println(a3[1]);
            a4.add(a3[1]);
        }
        return a4;

    }
*/

}

浏览器传递数据,对象,数组等

package cn.tedu.mvc;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

//完成Spring MVC 的角色,接受请求,给出相应
//是MVC 中的 C 控制器 接受请求,给出相应
@RestController
@RequestMapping("car") //规定了url怎么访问这个类
//RequesMapping 可以用在类与方法上
/*
localhost:8080//car 不能直接访问类
localhost:8080//car/get  可以访问
*/
public class HelloController {
    @RequestMapping("get")//请求的映射 严格要求完全一致,区分大小写
    public String show(){
        return "123";
    }
    @RequestMapping("set")
    public int show1(){
        return 52;
    }
    @RequestMapping("Car")
    public Object car1(){
        Car a = new Car();

       Car b =  new Car(718,"保时捷","cayman t","红色",624521);
        return b;
    }
    @RequestMapping("arr")//返回数组
    public String[] arrays(){
        String[] a = new String[]{"lbw","lwb","wyf"};
        return a;
    }

}

注意:

@RestController  //类直接可以被浏览器访问
@RequestMapping("abc")//方法可以被浏览器访问 注解必须添加参数,参数是用来服务器访问的路径 例如   http://localhost:8080/adc


System.out.println("lbwdsb"); //返回IDEA控制台,浏览器中看不见
 return "hello spring boot";//返回到浏览器

框架,是一个价格,框架提供了很多类,由框架控制每个类的调用过程流程
SSM框架中,第一个S就是指SpingMVC,是一个框架
MVC设计模式保证的松耦合

M 就是Model层(数据处理模型)   V就是视图层 View(HTML 展示数据)   C就是Controller 控制层
SpingMVC 主要的作用:1接受请求(解析请求参数) 2,做出相应

报错

404 找不页面,输入了一个不存在的页面

400 找到了页面,但参数列表类型不对应

500 IDEA中报错,在IDEA中排错


版权声明:本文为qq_45377275原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。