• SpringMVC 学习(五)转发,重定向和传参


    6. 转发和重定向

    Spring MVC 的底层是 servlet,因此在 Spring MVC 中也存在转发和重定向的概念。

    • 对于转发而言,其目的页面可以在 WEB-INF 目录下
    • 重定向的目的页面不允许在 WEB-INF 目录下,因为重定向相当于用户再次发起一次请求,而用户不允许直接访问 WEB-INF 目录下的资源。

    6.1 转发

    使用 forward: 即可实现转发

    • 转发至目的页面:forward:目的页面相对站点的路径

      return "forward:WEB-INF/jsp/test.jsp";

    • 转发至控制器:forward:控制器

      return "forward:/restAdd/1/2";

    处理器适配器在返回 ModelAndView 时默认使用转发方式

    6.2 重定向

    使用 redirect: 即可实现重定向

    • 重定向至目的页面:redirect:目的页面相对站点的路径

      return "redirect:index.jsp";

    • 重定向至控制器:redirect:控制器

      return "redirect:/restAdd/1/2";

    重定向与转发不可使用 RestFul 风格直接在浏览器带参数进行访问

    7. 接收请求参数和数据回显

    7.1 请求参数接收

    • 请求参数与被请求函数形参名称相同

      http://localhost:8080/user/rec?name=why

      @Controller
      @RequestMapping("/user")
      public class UserController {
      
          @GetMapping("/rec")
          public String rec(String name, Model model) {
              System.out.println("name: " + name);
              model.addAttribute("msg", "name: " + name);
              return "test";
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    • 请求参数与被请求函数形参名称不同

      http://localhost:8080/user/rec?username=why

      @Controller
      @RequestMapping("/user")
      public class UserController {
      
          @GetMapping("/rec")
          public String rec(@RequestParam("username") String name, Model model) {
              System.out.println("name: " + name);
              model.addAttribute("msg", "name: " + name);
              return "test";
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    • 被请求函数形参为对象

      http://localhost:8080/user/recObj?id=1&name=why&age=1

      @Controller
      @RequestMapping("/user")
      public class UserController {
          @GetMapping("/recObj")
          public String recObj(User user, Model model) {
              System.out.println("user: " + user);
              model.addAttribute("msg", "user: " + user);
              return "test";
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public class User {
      
          private int id;
      
          private String name;
      
          private int age;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

    7.3 数据回显

    (1) ModelAndView

    public class HelloController implements Controller {
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg","HelloSpringMVC!");
            mv.setViewName("hello");
            return mv;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (2) Model

    @GetMapping("/recObj")
    public String recObj(User user, Model model) {
        System.out.println("user: " + user);
        model.addAttribute("msg", "user: " + user);
        return "test";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (3) ModelMap

    Model extends ModelMap extends LinkedHashMap

    @GetMapping("/recObj")
    public String recObj(User user, ModelMap model) {
        System.out.println("user: " + user);
        model.addAttribute("msg", "user: " + user);
        return "test";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

  • 相关阅读:
    JavaEE Bean作用域与生命周期
    “互联网+”谋定现代农业-国稻种芯-万祥军:产业体系提升农业
    PMP项目管理概述
    MongoDB第一话 -- Docker安装MongoDB以及Springboot集成MongoDB
    js原型链以及实现继承的手段
    在浏览器上输入一个网址后,发生了什么?
    欧科云链与HashKey Exchange达成合作,助力香港虚拟资产合规化
    ipad触控笔有必要买原装吗?ipad2023手写笔推荐
    企业在混合办公模式下无缝网络连接搭建秘籍
    npm基本使用
  • 原文地址:https://blog.csdn.net/qq_42651415/article/details/133241628