• SpringMVC框架学习笔记(4)——结果跳转方式


    1.设置ModelAndView对象。根据View和视图解析器跳转到指定页面(视图解析器前缀+viewname+视图解析器后缀)

    @Override
    public ModelAndView handleRequest(HttpServletRequest req,
            HttpServletResponse resp) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("msg", "first spring mvc app");
        return mv;
    }

    2.通过ServletAPI对象来实现,不需要配置视图解析器

    通过HttpServletRequest输出

    resp.getWriter().print("Use httpServletAPI");

    通过HttpServletRequest实现重定向

    @RequestMapping(value = "/hi.do")
    public void hello(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendRedirect("hello.jsp");
    }

    通过HttpServletRequest实现转发

    @RequestMapping(value = "/hi.do")
    public void hello(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setAttribute("msg", "testRequestDispatcher");
        req.getRequestDispatcher("hello.jsp").forward(req, resp);
    }

    3.通过springMVC来实现重定向和转发

    转发

    @RequestMapping("/hi2")
    public String hello2() {
        //转发1
        //return "index";
        //转发2
        return "forward:index.jsp";
    }

    重定向

    @RequestMapping("/hi3.do")
    public String hello3(ModelMap map) {
        map.addAttribute("msg", "helloworld");
        return "redirect:index.jsp";
    }
  • 相关阅读:
    OpenCV 实现图像拼接例子
    XML 可扩展标记语言
    XML和YAML的区别与使用方法
    OpenCV 感兴趣区域ROI和logo添加技术
    OpenCV 详解掩膜mask
    OpenCV 读写xml和yml文件
    OpenCV 如何生成能在无OpenCV环境下运行的exe
    OpenCV 图像拼接和图像融合技术
    OpenCV 特征检测和特征匹配方法汇总
    OpenCV 图像矫正技术深入探讨
  • 原文地址:https://www.cnblogs.com/huangjian2/p/6650037.html
Copyright © 2020-2023  润新知