zoukankan      html  css  js  c++  java
  • 接收请求参数及数据回显

    接收请求参数及数据回显

    • 接收一个参数

      //http://localhost:8080/r/user/t1?username=julia
      @GetMapping("/user/t1")
      public String test1(@RequestParam("username") String name, Model model) {
          return "test";
      }
      
    • 接受一个对象

      //http://localhost:8080/r/user/t2?id=11&name=julia&age=16
      //前端传递的参数名必须和对象的字段名一致
      @GetMapping("/user/t2")
      public String test2(User user){
          return "test";
      }
      

    • 通过ModelAndView回显

      public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
          ModelAndView mv = new ModelAndView();
          String result = "hellospringmvc";
          mv.addObject("msg", result);
          mv.setViewName("test");
          return mv;
      }
      
    • 通过Model回显

      @GetMapping("/user/t4")
      public String test4(Model model){
          model.addAttribute("msg", "大头儿子");
          return "test";
      }
      
    • 通过ModelMap回显

      @GetMapping("/user/t4")
      public String test4(ModelMap map){
          map.addAttribute("msg", "大头儿子");
          return "test";
      }
      

    ModelAndView: 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转
    Model: 只有寥寥几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解
    ModelMap: 继承了 LinkedMap ,除了实现了自身的一些方法,同样的继承 LinkedMap 的方法和特性

  • 相关阅读:
    类中静态方法
    子类执行父类的构造方法
    MySQL grant命令使用
    Jmeter中引入class文件的方法
    了解CSS/CSS3原生变量var (转)
    Vue 开源项目库汇总(转)
    史上最全常用正则表达式 (转)
    如何实现CSS限制字数,超出部份显示点点点...
    我的博客园第一篇文章......
    平衡二叉树,AVL树之图解篇
  • 原文地址:https://www.cnblogs.com/pinked/p/12229072.html
Copyright © 2011-2022 走看看