zoukankan      html  css  js  c++  java
  • SpringMVC后台跳转回前端的方式

    Controller.java

    @Controller // 表示是控制器
    public class ResultController {
        // 1.视图解析器方式,需要配置视图解析器
        @RequestMapping("/result1") // 设置映射
        public ModelAndView result1(HttpServletRequest req, HttpServletResponse resp) {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg", "hello result 1");
            // 转发,不设置名字就和@RequestMapping()里的名字一样
            mv.setViewName("hello1");
            // 重定向 后缀加不加都可以, 带参数必须加上
            // mv.setViewName("redirect:hello1.jsp");
            return mv;
        }
    
        // 2.HttpServlet API方式,不需要配置视图解析器
        @RequestMapping("/result2")
        public void result2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setAttribute("msg", "hello result 2-2");
            // 重定向,带参数不能有空格?
            resp.sendRedirect("hello2-1.jsp?msg=hello-result-2-2");
            // 转发
            // req.getRequestDispatcher("hello2-2.jsp").forward(req, resp);
        }
    
        // 3.SpringMVC方式,不需要配置视图解析器
        @RequestMapping("/result3")
        public String result3(RedirectAttributes attr) {
            // 转发 1.不传参数,不能加后缀,但需视图解析器。2.要传参数,必须加上后缀
            // return "hello3-1";
            // return "hello3-1.jsp?msg=hello result 3-1";
            // 两种一样,这种要后缀
            // return "forward:hello3-1.jsp?msg=hello result 3-1";
            // 重定向 这种要后缀,还需在xml文件加<mvc:annotation-driven />
            // attr.addAttribute("msg", "hello result 3-2");
            return "redirect:hello3-2.jsp";
        }
    }

    springmvc.xml配置

    略(配置视图解析器)

  • 相关阅读:
    BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(分块)
    BZOJ 4241 历史研究(分块)
    BZOJ 3110 [Zjoi2013]K大数查询(整体二分)
    hdu 5412 CRB and Queries(整体二分)
    POJ2104 K-th Number(整体二分)
    luogu P3157 [CQOI2011]动态逆序对(CDQ分治)
    陌上开花(CDQ分治)
    BZOJ 1176[Balkan2007]Mokia(CDQ分治)
    BZOJ 3626 LCA(离线+树链剖分+差分)
    bzoj1592 Making the Grade
  • 原文地址:https://www.cnblogs.com/xiaohongxin/p/6516645.html
Copyright © 2011-2022 走看看