• SpringMVC学习案例2:


    1.请求路径的问题解决:

    当有两个bean类请求入径都后面都有相同的:设置请求路径前缀:
    第一种方式:加前缀位置直接book/save这样加

    public class UserController {
    
       @RequestMapping("book/save")//2.访问路径
       }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public class Userwel {
    
        
        @RequestMapping("we/save")//2.访问路径
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    第二种方式:类外面加前缀

    @RequestMapping("/book")
    public class UserController {
    
       @RequestMapping("/save")//2.访问路径
       }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @RequestMapping("/we")
    public class Userwel {
    
    
        @RequestMapping("/save")//2.访问路径
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.关于设置多个类请求路径设置bean的注解类型:

    都是用这个注解@Controller (springmvc)
    名称一般都是和类名称一样。
    前缀一样的不同类:会报错误!

    @Controller    //实际bean的名称是:Userwel,类型是Controller
    @RequestMapping("/we")
    public class Userwel {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    @Controller  // 实际bean的名称是UserController:,类型是Controller
    @RequestMapping("/book")
    public class UserController {
    }
    
    • 1
    • 2
    • 3
    • 4

    3.关于get和post请求使用(postman软件)

    postman软件:能进行表单操作,get,post请求方式。

    get请求和post请求最大的区别就是:
    get请求:浏览器地址是?name=&&username=&&
    post请求浏览器地址是:不显示的。

    部分代码:

    package com.itheima;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    
    @RequestMapping("/read")
    public class GetController {//测试get请求,post请求
    
        @RequestMapping("/use")
        @ResponseBody
     public String read(String name){
    
        System.out.println("请求的数据"+name);
        return"{请求的数据}"+name;
    
    }
    
    
    
    
    
    
    
    
    
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    结果:
    直接在网址http://localhost:8080/SpringMVC_dom1_war_exploded/read/use后面增加:
    ?name=helloworld
    把get的请求获取到了。

    在这里插入图片描述

    get请求就是在:get的params设置
    post请求的使用:在postman的:post的body下面设置
    get请求和post请求代码一样
    在这里插入图片描述

    4.关于设置中文乱码:

    上面的结果出现了中文乱码现象

    解决方法第一种:

    给RequestMapping增加produces属性,属性值是text/json;charset=utf-8
    @RequestMapping(value = “/read”,produces =“text/json;charset=utf-8”)

    可能是text/html

    package com.itheima;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    //produces=
    @RequestMapping(value = "/read",produces ="text/json;charset=utf-8")
    public class GetController {//测试get请求,post请求
    
        @RequestMapping("/use")
        @ResponseBody
     public String read(String name){
    
        System.out.println("请求的数据"+name);
        return"{请求的数据}"+name;
    
    }
    
    
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    结果:中文不出现乱码
    在这里插入图片描述

    5.数据类型传递

    1.类对象传递:

    package com.itheima;
    
    public class User {
        
        public String name;
        public String password;
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    ShujuController类:

    package com.itheima;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    
    @RequestMapping(value = "/shuju",produces = "text/json;charset=utf-8")
    public class ShujuController {
    
        @RequestMapping("/user")
        @ResponseBody
        //1.传递类型User对象
        public String er(User user ){
    
            System.out.println( user);
            return "{User类型}"+user;
    
        }
    
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    结果:
    在这里插入图片描述

    2.集合传递:

    List 集合
    传递要加@RequestParam,否则报错误:

    No primary or single unique constructor found for interface java.util.List
    @RequestParam注解把传的值丢进传递类型里面去

    package com.itheima;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    
    @RequestMapping(value = "/shuju",produces = "text/json;charset=utf-8")
    public class ShujuController {
    
        @RequestMapping("/user")
        @ResponseBody
        //1.传递类型User对象
        public String er(User user ){
    
            System.out.println( user);
            return "{User类型}"+user;
    
        }
    
    //2.传递类型:集合类型List
        //3.集合传递过程中需要弄@RequestParam,把值丢进集合里
        //不加这个会报错误:No primary or single unique constructor found for interface java.util.List
    //    无法找到list接口
        @RequestMapping("/qwe")
        @ResponseBody
        public String err(@RequestParam List<String>like){
    
            System.out.println(like);
            return "{集合类型}";
        }
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    @RequestParam 还可以用于:
    请求参数名称和形参名称不同,给形参起请求参数名。这样形参就能收到了。
    请求参数是:name
    public String err(@RequestParam (“name”)String wer){}

    3.json数据传递:

    json数据传递:要转换成集合List,所以先导入包json
    json坐标:

      <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.14.0-rc3</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    写入注解:@EnableWebMvc

    package com.config;
    
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    
    //4.创建springmvc配置文件
    @Configuration //==beans
    
    @ComponentScan("com.itheima")
    @EnableWebMvc//加这个注解,进行Jason数据转换
    public class Config {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    具体代码:

    package com.itheima;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    @RequestMapping(value = "/jason",produces = "text/json;charset=utf-8")
    public class Jason {
    
    
    
        @RequestMapping("/der")
        @ResponseBody
    //1.json数据转换成集合数据
    //@RequestParam用这个进行丢会报错误,@RequestBody注解用来进行jason转换
        public String ER( @RequestBody List<String> likes){
    
    
    
            return "{jason数据展示:}"+likes;
        }
    
    
        @RequestMapping("/wers")
        @ResponseBody
    //2.json数据转换成对象类数据
    
        public String app(@RequestBody User user){
    
    
            System.out.println("jason....对象类");
            return "{jason数据展示:}";
    
        }
    
    
    //源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。

    // // // @RequestMapping("/werser") @ResponseBody //2.json数据转换成对象类数据 public String app(@RequestBody List<User> list){ System.out.println("jason....对象类"); return "{jason数据展示:}"+list; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    结果:
    在这里插入图片描述

    在这里插入图片描述

    4.日期格式参数转换:加注解:@DateTimeFormat(pattern = “yyyy-mm-dd”)
    具体代码

    package com.itheima;
    
    import org.springframework.format.annotation.DateTimeFormat;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.Date;
    
    @Controller
    @RequestMapping(value = "/date",produces = "text/json;charset=utf-8")
    public class DateController {
        //1.日期格式转换
    
    
    
        @RequestMapping("/a")
        @ResponseBody
        //如果date1是:2022-11-02,这种是string,要进行转换
        public String a(Date date,@DateTimeFormat(pattern = "yyyy-mm-dd") Date date1){//date输入是这种:2022/11/02
    
            System.out.println(date);//:打印出来是  Wed Nov 02 00:00:00 CST 2022
    
            return "{Date日期格式转换}"+date+"{date日期转换第二种方式}"+date1;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    结果:
    在这里插入图片描述

  • 相关阅读:
    关于java语言中的多态(提醒大家仔细阅读红色字体部分,能帮助你理解“多态”)
    GPU -- 图形处理器(显卡)
    解析标准树状文件为sql语句及代码解析(python版本,txt,mysql)
    四十五、壁面函数理论及y+的确定
    JavaScript 浏览器元素滚动 scrollIntoView()
    Spring Boot 3 集成 Knife4j
    docker安装minio及minio的使用
    html当当书网站 html网上在线书城 html在线小说书籍网页 当当书城网页设计
    【IEEE会议】 第三届智能通信与计算国际学术会议(ICC 2023)
    【顺序表的实现】
  • 原文地址:https://blog.csdn.net/qq_51813155/article/details/127603554