zoukankan      html  css  js  c++  java
  • spring

     新建了一个class,添加注解设定一级路径为 annotation

    @RequestMapping(path = "/annotation")

    里面添加了方法用来测试:

    1.

    RequestParam (加在controller方法的参数前)

    用来规定参数名字

        @RequestMapping(path = "/RequestParam")
        public String requestParamTest(String name, String password){
            System.out.println("name = " + name);
            System.out.println("password = " + password);
            return "success";
        }

    不插入注解时,前台需要传递两个参数,name 和 password

        @RequestMapping(path = "/RequestParam")
        public String requestParamTest(String name, @RequestParam(name = "p") String password){
            //插入注解后,前台需要传递的参数变为name 和 p
            System.out.println("name = " + name);
            System.out.println("password = " + password);
            return "success";
        }

    插入注解后,前台需要传递的参数变为name 和 p

    插入注解后的jsp部分:

    <a href="annotation/RequestParam?name='yzha'&p='123456'">RequestParam test</a>

    2.

    RequestBody

    拿到post请求的请求体(key=value的格式,处理json格式数据时会用到)

    get请求:一般是超链

    post请求:提交表单

    RequestHeader: 获得请求头,用得比较少

    CookieValue:将cookie值传回,可以用来生成浏览记录

    java的controller方法:

        @RequestMapping(path = "/RequestBody")
        public String requestBodyTest(@RequestBody String body, @RequestHeader(value = "ACCEPT") String accept, @CookieValue(value = "JSESSIONID", required = false) String cookie){
            //RequestBody:将所有参数变成一个字符串传回接收
            //RequestHeader:将请求头传回,用字符串接收。
            //CookieValue:将cookie传回,用字符串接收。
            System.out.println(body);
            System.out.println("---------------------");
            System.out.println(accept);
            System.out.println("---------------------");
            System.out.println(cookie);
            return "success";
        }

    jsp:

    <form action="/annotation/RequestBody" method = post>
                账号信息:<br/>
                用户名:<input type="text" name = 'name'><br/>
                密码:<input type="text" name = 'password'><br/>
                用户信息:<br/>
                用户姓名:<input type="text" name = 'user.name'><br/>
                用户年龄:<input type="text" name = 'user.age'><br/>
                用户生日(yyyy-mm-dd):<input type="text" name = 'user.birthday'><br/>
                <input type ="submit" value = "提交">
            </form>

    输入:

     输出:

     RequestBody的整体格式类似get的传参方法,空格变成了+,汉字变成了编码

    3.

    PathVariable

    用来添加url的占位符,一般用来实现restful的编程风格

    关于restful:

    https://www.cnblogs.com/clamp7724/p/11714650.html

    java的controller方法

        @RequestMapping(path = "/PathVariable", method = RequestMethod.GET)
        public String pathVariableTest1(String name){
            System.out.println("根据name = " + name + "查询");
            return "success";
        }
        @RequestMapping(path = "/PathVariable/{userid}", method = RequestMethod.GET) //{}中的参数名字需要与下面@PathVariable中name一致
        public String pathVariableTest2(@PathVariable(name = "userid") String id){ //用了PathVariable后,需要在路径格式为  /PathVariable/100 这种
            System.out.println("根据id = " + id + "查询");
            return "success";
        }

    jsp部分

        <div>
            <a href="annotation/PathVariable?name='yzha'">RequestParam test1</a><br/>
            <a href="annotation/PathVariable/20?name='yzha'">RequestParam test2</a>
        </div>

    后台输出:

    4. ModelAttribute

    修饰方法时,方法会在当前class中的controller运行前提前运行,可以接收前台传过来的参数,没有对应参数的时候为默认值(null或者0)。

    因为Spring是利用反射调用对象的set方法对对象进行赋值,所以预处理的对象被之后的controller接收到后,会利用set方法进行赋值,预处理的值可以保留(返回值和controller参数的名字和类型一致),不会影响controller的正常运行。(我知道可能看不懂。。。总是这里很抽象- -)

    修饰参数时,把这个注解的value的值作为key,获取map对应的value。 (map需要在@ModelAttribute修饰的方法中预先定义)

    public @interface ModelAttribute {
        @AliasFor("name")
        String value() default "";
    
        @AliasFor("value")
        String name() default "";
    
        boolean binding() default true;
    }

    这是源码。。。别被绕进去了- -虽然是ModelAttribute的value属性,但是值是作为map的key。。。

    他的key才是value

    场景

    比如一个网站用户注册网站旗下游戏账号时,自己的信息可以直接从网站获得,不需要填写。所以可以用modelAttribute预处理数据

    利用返回值预处理:

    java部分:

        //ModelAttribute
        @RequestMapping(path = "/ModelAttribute")
        public String modelAttributeTest(Account account){
            System.out.println(account);
            return "success";
        }
    //预处理部分
        @ModelAttribute
        public Account dealWithNullAttribute(String name){
            System.out.println("新增用户:" + name);
            //模拟查询数据库后获得了user信息
            User user = new User();
            user.setName("张三");
            user.setAge(20);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date d = new Date();
            try {
                d = simpleDateFormat.parse("1990-05-26");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setBirthday(d);
            Account account = new Account();
            account.setUser(user);
            return account;
        }

    jsp部分:

        <div>
            <form action="/annotation/ModelAttribute" method = post>
                账号信息:<br/>
                用户名:<input type="text" name = 'name'><br/>
                密码:<input type="text" name = 'password'><br/>
                <input type ="submit" value = "提交">
            </form>
        </div>

    输入:

     输出:

    利用map (我把上面那个ModelAttribute的java部分注释掉了)

    java:

        //ModelAttribute
        @RequestMapping(path = "/ModelAttribute")
        public String modelAttributeTest( @ModelAttribute("aaa") Account account){ //把map中value = aaa的 key作为预处理对象
            System.out.println(account);
    
            return "success";
        }
    
        //比如一个网站用户注册网站旗下游戏账号时,自己的信息可以直接从网站获得,不需要填写。所以可以用modelAttribute预处理数据
        //预处理部分 (不需要返回值)
        @ModelAttribute
        public void dealWithNullAttribute(String name, Map<String, Account> map){  //一个字符串 和 Account对象作为键值对的Map
            System.out.println("新增用户:" + name);
            //模拟查询数据库后获得了user信息
            User user = new User();
            user.setName("李四");
            user.setAge(20);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date d = new Date();
            try {
                d = simpleDateFormat.parse("1990-05-26");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setBirthday(d);
            Account account = new Account();
            account.setUser(user);
    
            map.put("aaa", account);
        }

    jsp:

        <div>
            <form action="/annotation/ModelAttribute" method = post>
                账号信息:<br/>
                用户名:<input type="text" name = 'name'><br/>
                密码:<input type="text" name = 'password'><br/>
                <input type ="submit" value = "提交">
            </form>
        </div>

    输入:

    输出:

    另外,如果controller写成

    public String modelAttributeTest( @ModelAttribute("aaa") Account account, Map<String, Account> map)

    可以直接用

     Map<String, Account> map

    接收整个预处理创建的map

  • 相关阅读:
    linux下svn命令使用大全(share)
    vi 编辑器命令 (share)
    如何成为一名优秀的前端工程师 (share)
    正则表达式入门教程&&经典Javascript正则表达式(share)
    动手学深度学习11- 多层感知机pytorch简洁实现
    动手学深度学习10- pytorch多层感知机从零实现
    动手学深度学习9-多层感知机pytorch
    动手学深度学习8-softmax分类pytorch简洁实现
    动手学深度学习7-从零开始完成softmax分类
    动手学深度学习6-认识Fashion_MNIST图像数据集
  • 原文地址:https://www.cnblogs.com/clamp7724/p/11714776.html
Copyright © 2011-2022 走看看