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