spring mvc 结果跳转方式

来源:互联网 发布:qq群发广告软件 编辑:程序博客网 时间:2024/05/29 03:47

无视图解析器

public class HelloController {    @RequestMapping("/hello")    public void hello(HttpServletRequest req,HttpServletResponse res) throws IOException{        //通过HttpServletResponse输出        //res.getWriter().println("result spring mvc api");        //通过HttpServletResponse实现重定向        //res.sendRedirect("index.jsp");        //通过HttpServletRequest实现转发        try {            req.setAttribute("msg", "by setAttribute");            req.getRequestDispatcher("index.jsp").forward(req, res);        } catch (ServletException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    //通过spring mvc实现转发和重定向--无视图解析器    @RequestMapping("/hello1")    public String hello1(){        //转发1        //return "index.jsp";        //转发2        //return "forward:index.jsp";        //重定向        return "redirect:index.jsp";    }    //通过spring mvc实现转发和重定向--有视图解析器    @RequestMapping("/hello2")    public String hello2(){        //转发        return "hello";    }}

有视图解析器 modelandview封装

@RequestMapping("/hello")    public ModelAndView hello(HttpServletRequest req,HttpServletResponse res){        ModelAndView mv=new ModelAndView();        mv.addObject("msg", "hello spring mvc annotation");        mv.setViewName("hello");            return mv;          }