• springMvc--请求的跳转和传值


    springMvc--请求的跳转和传值

    forword跳转页面的三种方式

    1.使用serlvet

    复制代码
    /**
         * 使用forward跳转,传递基本类型参数到页面
         *     注意:
         *         1.使用servlet原生API Request作用域
         *         
         */
        @RequestMapping("/test")
        public String test(HttpServletRequest request,HttpServletResponse response){
            String name = "张小三";
            request.setAttribute("name",name);
            return "/back/attr";
        }
    复制代码

    2.使用Model对象

    复制代码
    /**
         * 使用forward跳转,传递基本类型参数到页面
         *     注意:
         *         1.使用springmvc 封装好的Model对象(底层就是request作用域)
         */
        @RequestMapping("/test1")
        public String test1(Model model){
            String name = "张小四";
            model.addAttribute("name", name);
            return "back/attr";
            
        }
    复制代码

    3.使用ModelAndView

    复制代码
    /**
         * 使用modelAndView
         *     注意事项
         *         modelAndView对象中的数据只能被ModelAndView对象的视图获取
         */
        @RequestMapping("/test2")
        public ModelAndView test2(ModelAndView modelAndView){
            String name = "张小五";
            modelAndView.setViewName("back/attr");
            modelAndView.addObject("name", name);
            return  modelAndView;
             
        }
    复制代码

     当然也可以通过new 一个ModelAndView对象来实现

    @RequestMapping("/test3")
        public ModelAndView test3(){
            String name = "张小六";
            return new ModelAndView("back/attr", "name", name);
        }

    redirect跳转到页面

     使用servlet

    复制代码
    /**
         * 使用redirect跳转 向页面传递数据
         *         1.使用Servlet原生API  Session  ServletContext
         */
        
        @RequestMapping("/test4")
        public String test4(HttpServletRequest request,HttpSession session){
            String name = "张晓霞";
            session.setAttribute("name", name);
            return "redirect:/back/attr.jsp";
        }
    复制代码

     使用ModelAndView

    复制代码
    /**
         * 使用redirect跳转 向页面传递数据
         *         1..使用ModelAndView对象  modelAndView对象会把model中的数据以?形式拼接到地址栏后 可以使用${param.key}接受
         */
        @RequestMapping("/test5")
        public ModelAndView test5(){
            return new ModelAndView("redirect:/back/attr.jsp","name","小张张");
        }
    复制代码

    跳转到Controller中的方法

    forword跳转

    redirect跳转类似

    跳转到相同类中的方法:

    复制代码
    /**
         * 使用forword跳转到相同类中的某一方法
         * 注意:
         *         1.不需要加上类上的@RequestMapping的值
         */
        @RequestMapping("/test00")
        public String test00(){
            return "forward:test1";
        }
    复制代码

    跳转到不同类中的方法

    复制代码
    /**
         * 使用forword跳转到不同类中的某一方法
         * 注意:
         *         1.需要加上类上的@RequestMapping的值:比如 :/hello
         */
        @RequestMapping("/test01")
        public String test01(){
            return "forward:/hello/test";
        }
    复制代码
  • 相关阅读:
    scrapy爬虫框架实例二
    查看系统信息
    scrapy中ROBOTSTXT_OBEY = True的相关说明
    scrapy爬虫框架实例一,爬取自己博客
    一个节点rac+单节点dg网络配置(listener.ora与tnsnames.ora)
    lsnrctl启动报错,Linux Error: 29: Illegal seek
    单机11g ogg 双向DML复制
    OGG 进程清除、重建
    OGG 11g Checkpoint 详解
    ogg日常运维命令
  • 原文地址:https://www.cnblogs.com/libin6505/p/9857797.html
Copyright © 2020-2023  润新知