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

      

  • 相关阅读:
    一些业内有名的网站收集
    WCF重载
    FCKEditor fckconfig.js配置,添加字体和大小 附:中文字体乱码问题解决
    查询第几条到第几条的数据的SQL语句
    SPOJ 9939 Eliminate the Conflict
    UVA 10534 Wavio Sequence
    HDU 3474 Necklace
    POJ 2823 Sliding Window
    UVA 437 The Tower of Babylon
    UVA 825 Walking on the Safe Side
  • 原文地址:https://www.cnblogs.com/sun-/p/14045450.html
Copyright © 2011-2022 走看看