Spring MVC结果跳转方式

来源:互联网 发布:东华软件java笔试题 编辑:程序博客网 时间:2024/05/29 04:37

1.设置设置ModelAndView对象,根据view名和视图解析器,跳转到对应的视图(视图解析器前缀+view-name+视图解析器后缀)

ModelAndView mView = new ModelAndView();        mView.addObject("msg","hhhh anotation");        mView.setViewName("hello");        return mView;

2. 通过Servlet api来实现(不需要试图解析器)

通过HttpServletResponce输出

public void hello(HttpServletRequest req,HttpServletResponse res) throws IOException {          res.getWriter().println("servlet api");}

通过HttpServletResponce实现重定向

public void hello(HttpServletRequest req,HttpServletResponse res) throws IOException {          res.sendRedirect("index.jsp");    }

通过HttpServletResponce实现转发

public void hello(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {          req.setAttribute("msg", "forward servlet api");         req.getRequestDispatcher("index.jsp").forward(req, res);    }

3. 使用springmvc 实现重定向与转发(不需视图渲染器)

实现转发1

    public String hello(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {          return "index.jsp";     }

实现转发2

public String hello1(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {          return "forward:index.jsp";    }

实现重定向

    public String hello1(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {          return "redirect:index.jsp";    }

4. 使用spring mvc实现重定向与转发(需要视图解析器)

实现转发

public String hello(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {          return "hello";    }

注意:重定向实现return “redirect:index.jsp”;无需视图解析器

阅读全文
'); })();
1 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刺青和纹身的区别 纹身图案大全图片 剌青 彼岸花小图案刺青 马宗跃为什么刺瞎罗琦 刺马案 刺马 刺马主题曲 刺魂 猎日传奇之刺魂 若迷 怒鲨神刺 刺鲸 刺鲸 蟹总 刺鲸蟹总全文免费阅读 刺鳅多少钱一斤 中华刺鳅图片 刺鳐 防鸟刺 刺鸟 刺梅花适合家里养么 陈师行剑刺竹子是真的么 刻刀 美术刻刀 电动刻刀 木工刻刀 小刻刀 手工刻刀 木雕刻刀 版画刻刀 刻刀工具 刻刀图片 剪纸刻刀 啄木鸟刻刀 电磨刻刀 玉刻刀 电动刻刀头 刻刀怎么用 刻刀画 刁刻刀 刻刀一套多少钱 刻刀的用法图解