页面重定向跳转的方式

在一次面试的时候被问到页面跳转有多少种方式,当时回答的不好,现在总结如下:

1.在server端sendRedirect重定向

response.sendRedircet("http://auto.sina.com.cn");
  • 执行该语句后server会发送一个body为空的http response,状态码为302,在respose header中location属性,值为http://auto.sina.com.cn。浏览器接收到response后读取location信息,向url发起请求,地址栏变成url。
  • 可以跨域

2.在server端使用ReqestDispatcher进行forward请求转发

//1.
request.getRequestDispatcher("http://auto.sina.com.cn").forward(request, response);
//2.
request.getSession().getServletContext().getRequestDispatcher("http://auto.sina.com.cn").forward(request, response);
  • server内部转发,浏览器地址栏不会发生变化
  • 两个资源共享request里面的数据
  • ServletContext和ServletContext获取getRequestDispatcher的区别是,前者path参数必须是绝对路径,后者path参数可以是绝对或者相对

3.使用JSP的response对象重定向

response是JSP的内置对象

//1.
<%
  response.sendRedirect("http://auto.sina.com.cn");
%>
//2.
<%
  response.setStatus(302);  
  response.setHeader("location", "http://auto.sina.com.cn");
%>

在browser端使用Javascript进行重定向

<script type="text/javascript">
  //1.
  window.location="http://auto.sina.com.cn";
  //2.
  location.href="http://auto.sina.com.cn";
  //3.
  location.assign("http://auto.sina.com.cn");
  //4.
  location.replace("http://auto.sina.com.cn");
</script>

4.在browser端使用html标签进行重定向

<!--1.-->
<a href="http://auto.sina.com.cn"> 跳转</a>
<!--2.-->
<!-- 5秒钟后跳转到指定页面 -->
<meta http-equiv="refresh" content="5;url=http://auto.sina.com.cn"/>

5.SpringMVC页面跳转方式

 如果跳转到WEB-INF目录下面的页面,直接通过超链接的形式是不能直接跳转的,必须经过后端来进行跳转

(1).使用视图解析:
当配置好了视图解析器时,controller中返回的是一个普通的字符串,则跳转到/WEB-INF/jsp/hello.jsp

<!-- 配置JSP视图 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
   <property name="prefix" value="/WEB-INF/jsp/"/>
   <property name="suffix" value=".jsp"/>      
   <property name="contentType" value="text/html;charset=UTF-8"/>
   <property name="order" value="1"/>
</bean>
@RequestMapping(value="/to",method={RequestMethod.POST,RequestMethod.GET})
public String to(@ModelAttribute("message") String msg, @ModelAttribute("to") String to){
        System.out.println("-----------------------------");
        System.out.println("message:"+msg);
        System.out.println("to:"+too);
        System.out.println("-----------------------------");
        return "hello";
    }

(2).不使用视图解析
如果controller字符串中有redirectforward关键字时,将会忽略视图解析器,直接跳转到controller对应的请求方法,而不是跳转到页面,比如hello方法中继续做页面的跳转

return "redirect:hello"
return "forward:hello"

(3).使用ModelAndView请求转发:需要使用视图解析

   @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,
                                      javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {

        ModelAndView mv = new ModelAndView();
        //封装要显示到视图的数据
        mv.addObject("msg","hello world");
        //视图名
        mv.setViewName("hello");
        return mv;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,661评论 1 92
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,281评论 19 139
  • IOC 控制反转容器控制程序对象之间的关系,而不是传统实现中,有程序代码之间控制,又名依赖注入。All 类的创建,...
    irckwk1阅读 1,037评论 0 0
  • 网上很多人在问“刚买了保险,发现不(被)合(骗)适(了),怎么办?”。 不合适的保险犹如鸡肋“食之,无味,弃之可惜...
    一只旅行箱阅读 299评论 0 1
  • 今儿开始收心啃书了,这是18年读的第三本书,因为有了在读群,前两本读的都很积极而且收获了太多意想不到的惊喜,一群...
    娇娇87阅读 233评论 1 1