RestFul风格的程序如何跳转页面呢?
RestFul风格就是直接返回JSON格式的字符串吧?
那如何实现页面的跳转呢?
21
收起
正在回答
1回答
同学你好,1、是的,一般是返回json类型的字符串。
2、 数据跳转方式:3种方式。
1)设置ModelAndView对象 , 根据view的名称 , 和视图解析器跳到指定的页面 。
1 | public class ControllerTest1 implements Controller {<br> public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {<br> //返回一个模型视图对象<br> ModelAndView mv = new ModelAndView();<br> mv.addObject("msg","ControllerTest1");<br> mv.setViewName("Hello");<br> return mv;<br> }<br>}<br> |
2)通过设置ServletAPI
1 | @Controller <br> public class ResultGo {<br> @RequestMapping ( "/test/t1" )<br> public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {<br> rsp.getWriter().println( "测试1" );<br> }<br> @RequestMapping ( "/test/t2" )<br> public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {<br> //重定向<br> rsp.sendRedirect("/index.html"); <br> }<br> @RequestMapping("/test/t3")<br> public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {<br> //转发<br> req.setAttribute("msg","测试2");<br> req.getRequestDispatcher("/WEB-INF/jsp/test.html").forward(req,rsp);<br> }<br><br>}<br> |
3)通过SpringMVC来实现转发和重定向。
1 | @Controller <br> public class ResultSpringMVC {<br> @RequestMapping ( "/test1" )<br> public String test1(){<br> //转发一<br> return "/index.html";<br> }<br><br> @RequestMapping("/test2")<br> public String test2(){<br> //转发二<br> return "forward:/index.html";<br> }<br><br> @RequestMapping("/test3")<br> public String test3(){<br> //重定向<br> return "redirect:/index.html";<br> }<br>}<br> |
注:在后期课程中会详细运行,同学现阶段了解一下即可,在后期可以详细学习。
祝学习愉快!
java工程师2020版
- 参与学习 人
- 提交作业 9404 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧