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

      

  • 相关阅读:
    easyexcel: The maximum length of cell contents (text) is 32,767 characters
    分库分表情况下添加索引
    如何保证消息顺序执行(Rabbitmq/kafka)
    MySQL Boolean类型的坑
    Redis居然还有比RDB和AOF更强大的持久化方式?
    ThreadLocal的应用场景和注意事项有哪些?
    spring boot 设置tomcat post参数限制
    并发慎用——System.currentTimeMillis()
    Java多线程中static变量的使用
    临时修改session日期格式冲突问题
  • 原文地址:https://www.cnblogs.com/sun-/p/14045450.html
Copyright © 2011-2022 走看看