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";
    //    }
    }
    

      

  • 相关阅读:
    idea + springboot 的java后台服务器通过小米推送
    python编程之禅
    温故而知新-MySQL高级编程
    温故而知新-mysql的一些语法show,describe,explain,fulltext
    温故而知新-PHP文件操作函数
    温故而知新-正则单词和strlen
    php使用tcpdf实现在线PDF功能
    温故而知新-array_walk和sizeof和array_count_values()和extract()
    温故而知新-strtok函数
    mysql中的blob和text区别
  • 原文地址:https://www.cnblogs.com/sun-/p/14045450.html
Copyright © 2011-2022 走看看