zoukankan      html  css  js  c++  java
  • SpringMVC接收前端传递的数据 & SpringMVC返回数据给前端

    package com.sun.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.Mapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author sunhongguang
     * @create 2020-11-26-22:33
     */
    @Controller
    @RequestMapping(path = "/5")
    public class ControllerTest5 {
    
        /**
         * 前端传递的参数名称和方法的参数名称一致
         *
         * @param name
         * @param model
         * @return
         */
        @GetMapping(path = "/t1")
        public String test1(String name, Model model) {
            model.addAttribute("msg", name);
            return "test";
        }
    
        /**
         * 前端传递的参数名称和方法的名称不一致,使用@RequestParm参数进行转换
         *
         * @param name
         * @param model
         * @return
         */
        @GetMapping(path = "/t2")
        public String test2(@RequestParam(value = "username") String name, Model model) {
            model.addAttribute("msg", name);
            return "test";
        }
    
    
        /**
         * 这样是不行的
         * @param nameList
         * @param model
         * @return
         */
        @GetMapping(path = "/t3")
        public String test3(List<String> nameList,Model model){
            model.addAttribute("msg", nameList.toString());
            System.out.println(nameList.toString());
            System.out.println(nameList);
            return "test";
        }
    
        @GetMapping(path = "/t4")
        public String test4(@RequestParam(value = "nameList") List<String> names,Model model){
            model.addAttribute("msg", names.toString());
            System.out.println(names.toString());
            System.out.println(names);
            return "test";
        }
    
        @GetMapping(path = "/t5")
        public String test5(@RequestParam(value = "nameList",defaultValue = "zhangSan,LiSi") List<String> names,Model model){
            model.addAttribute("msg", names.toString());
            System.out.println(names.toString());
            System.out.println(names);
            return "test";
        }
    
        @GetMapping(path = "/t6")
        public String test6(@RequestParam Map<String,Object> map,Model model){
            model.addAttribute("msg", map);
            System.out.println(map.toString());
            return "test";
        }
    
    //    @PostMapping(path = "/t7")
    //    public String test7(@RequestBody Map<String,Object> map){
    //        model.addAttribute("msg", map);
    //        System.out.println(map.toString());
    //        return "test";
    //    }
    }
    

      

  • 相关阅读:
    优化慢执行或慢查询的方法
    Top K问题的两种解决思路
    优先队列实现 大小根堆 解决top k 问题
    进程间的八种通信方式----共享内存是最快的 IPC 方式
    二叉树基础之按层打印
    按层打印二叉树--每行打印一层
    给定一颗完全二叉树,给每一层添加上next的指针,从左边指向右边
    缓存与数据库一致性保证
    一致性哈希算法原理
    Linux复制指定目录下的文件夹结构
  • 原文地址:https://www.cnblogs.com/sun-/p/14045450.html
Copyright © 2011-2022 走看看