struts result type redirect 重定向和转发的区别

来源:互联网 发布:笑傲江湖武功 知乎 编辑:程序博客网 时间:2024/04/29 19:51

我们在使用struts2进行配置struts.xml的时候,<result>中有个tyep属性用来配置跳转类型

默认不写时tyep="dispatcher",我们可以根据我们的需要选择跳转类型

例如:<result name="list" type="redirect">User_list</result>

这个result-type的具体类型可以在对应的struts2-croe-2.*.jar或者struts2源代码中的struts-default.xml中找到,在这个文件中找到<result-type>标签,所有的result-type都在其中有定义.代码如下:

 

[html] view plaincopyprint?
  1. <result-types>  
  2.       <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>  
  3.       <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>  
  4.       <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>  
  5.       <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>  
  6.       <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>  
  7.       <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>  
  8.       <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>  
  9.       <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>  
  10.       <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>  
  11.       <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />  
  12.   </result-types>  

 具体介绍如下:

chain   
      用来处理Action链,将一个action的执行与另外一个配置好的action串连起来。用第一个action的getter方法和第二个action的setter方法来完成action之间属性的复制。 
    com.opensymphony.xwork2.ActionChainResult   
  
dispatcher     
    用来转向JSP页面,这是默认的结果类型,假如在action配置中没有配置其他的结果类型,它就会被使用   
    org.apache.struts2.dispatcher.ServletDispatcherResult   
  
freemaker   
      处理FreeMarker模板   
      org.apache.struts2.views.freemarker.FreemarkerResult   
  
httpheader   
      控制非凡HTTP行为的结果类型     
     org.apache.struts2.dispatcher.HttpHeaderResult   
  
redirect   
      重定向到一个URL     
      org.apache.struts2.dispatcher.ServletRedirectResult   
  
redirectAction   
    重定向到一个Action   
    org.apache.struts2.dispatcher.ServletActionRedirectResult   
  
stream   
      向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据   
     org.apache.struts2.dispatcher.StreamResult   
  
velocity   
      处理Velocity模板   
     org.apache.struts2.dispatcher.VelocityResult   
  
xslt   
     处理XML/XLST模板   
     org.apache.struts2.views.xslt.XSLTResult   
  
plainText   
      显示原始文件内容,例如文件源代码   
    org.apache.struts2.dispatcher.PlainTextResult


重定向和转发的区别

区别一:

  重定向时浏览器上的网址改变

  转发是浏览器上的网址不变

区别二:

  重定向实际上产生了两次请求

转发只有一次请求 

重定向: 

  发送请求 -->服务器运行-->响应请求,返回给浏览器一个新的地址与响应码-->浏览器根据响应码,判定该响应为重定向,自动发送一个新的请求给服务器,请求地址为之前返回的地址-->服务器运行-->响应请求给浏览器 

转发: 

  发送请求 -->服务器运行-->进行请求的重新设置,例如通过request.setAttribute(name,value)-->根据转发的地址,获取该地址的网页-->响应请求给浏览器 

区别三:

  重定向时的网址可以是任何网址

  转发的网址必须是本站点的网址

详解:

  重定向:以前的request中存放的变量全部失效,并进入一个新的request作用域。
转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。

正文开始: 

  先是看上去不同,他们的调用分别如下:
  request.getRequestDispatcher("apage.jsp").forward(request, response);//转发到apage.jsp
response.sendRedirect("apage.jsp");//重定向到apage.jsp
在jsp页面中你也会看到通过下面的方式实现转发:
  <jsp:forward page="apage.jsp" />
  我在初学jsp的时候,对这两个概念非常模糊,看别人的例子的时候,也是一头雾水,不知道什么时候该用哪个。希望下面的解说能对你有所帮助。
  提到转发和重定向就不得不提到request作用域。很多初学者都知道当我们提交一个表单时,就创建了一个新的请求。实际上,当我们点击一个链接时,也创建了一个新的请求。那么一个请求的作用于到底有多大呢?例如:
在页面a.jsp中有一个链接<a href="b.jsp?id=1">这是指向b的一个链接,而且还带了一个参数</a>。当我们点击这个连接的时候,就产生了一个请求,为了明确起见,我们把它叫做requestA->B。现在,在b.jsp页面中我们就可以从这个请求中获取信息了。在b.jsp中你可以写入out.println(request.getParameter("id"))进行测试。下面更复杂一点,我们在b.jsp页面中增加下面的语句:
  request.setAttribute("name","funcreal");
out.println(request.getAttriblute("name"));//成功显示了name变量的值。
  现在在b.jsp中再增加一个链接:<a href="c.jsp?age=23">这是指向c的一个链接,而且还带了一个参数</a>,当我们点击这个连接的时候,将产生一个新的请求,这时requestA-B也就安息了,新的请求叫做requestB-C。同样的道理,在c.jsp中,我们可以访问到的变量只有age,因为id,name这两个变量都属于requestA-B,此时他已经不存在了。下面是源代码:
a.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<a href="b.jsp?id=1">指向b.jsp,而且还带了一个参数id=1。requestA-B现在诞生了</a>
</body>
</html> 

b.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
request.setAttribute("name","Func Real");
out.println("name=" + request.getAttribute("name"));
%>
<a href="c.jsp?age=23">requestA-B已经结束了。指向c.jsp,而且还带了一个参数age=23</a>
</body>
</html> 

c.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
out.println("name=" + request.getAttribute("name"));
out.println("age=" + request.getParameter("age"));
%>
</body>
</html> 

  那么转发又是怎么回事呢?现在增加一个页面叫做d.jsp,并且在c.jsp中</body>前面增加一句<jsp:forward page="d.jsp"/>
d.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
requestB-C的魔爪已经伸到了d.jsp页面
<%
out.println("age=" + request.getParameter("age"));
%>
</body>
</html>
运行程序,你会发现c页面中的内容没有显示出来,因为forward是自动执行的,地址栏中虽然是c.jsp但实际上,但浏览器中显示的已经是d.jsp的内容了,而且看到了从b.jsp传过来的参数。你可以简单得这样理解:转发,就是延长了requestB-C的作用域,<jsp:forward page="d.jsp"/>,这一句话实际上是把c.jsp和d.jsp粘到了一起,他们就像是在一个页面中。
如果你用过struts,那么你就知道为什么在Action中,最后一句几乎总是mapping.findForward("xxx");了。因为我们在这个Action中设置的请求作用域的变量都将会在下一个页面(也许是另一个Action)中用到,所以要用转发。 

总结:
用重定向和转发不是一个习惯问题。而是什么情况下必须用什么的问题。