struts2的result的type属性

简介: 一共有两个属性name和type  name这里就不介绍了  type    返回结果的类型,值可以从default-struts.properties中看到看到 常用的值:dispatcher (默认)转发、redirect 重定向、redirectAction  重定向到Action、chain 转发到Action、还有一个是stream一般用于文件下载的  这里不得不提下转发和重定向的区别: 这里不得不提下重定向与请求转发的区别一转发是服务器行为,重定向是客户端行为。

一共有两个属性name和type

 

 

name这里就不介绍了

 

 

type    返回结果的类型,值可以从default-struts.properties中看到看到

 

常用的值:dispatcher (默认)转发、redirect 重定向、redirectAction  重定向到Action、chain 转发到Action、还有一个是stream一般用于文件下载的 

 

这里不得不提下转发和重定向的区别:

 

这里不得不提下重定向与请求转发的区别

转发是服务器行为,重定向是客户端行为。为什么这样说呢,这就要看两个动作的工作流程:

转发过程:客户浏览器发送http请求——》web服务器接受此请求——》调用内部的一个方法在容器内部完成请求处理和转发动作——》将目标资源发送给客户;在这里,转发的路径必须是同一个web容器下的url,其不能转向到其他的web路径上去,中间传递的是自己的容器内的request。在客户浏览器路径栏显示的仍然是其第一次访问的路径,也就是说客户是感觉不到服务器做了转发的。转发行为是浏览器只做了一次访问请求。

重定向过程:客户浏览器发送http请求——》web服务器接受后发送302状态码响应及对应新的location给客户浏览器——》客户浏览器发现是302响应,则自动再发送一个新的http请求,请求url是新的location地址——》服务器根据此请求寻找资源并发送给客户。在这里location可以重定向到任意URL,既然是浏览器重新发出了请求,则就没有什么request传递的概念了。在客户浏览器路径栏显示的是其重定向的路径,客户可以观察到地址的变化的。重定向行为是浏览器做了至少两次的访问请求的。

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

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

 

重定向,其实是两次request

第一次,客户端request   A,服务器响应,并response回来,告诉浏览器,你应该去B。这个时候IE可以看到地址变了,而且历史的回退按钮也亮了。重定向可以访问自己web应用以外的资源。在重定向的过程中,传输的信息会被丢失。

例子:

response.sendRedirect("loginsuccess.jsp");

请求转发是服务器内部把对一个request/response的处理权,移交给另外一个

对于客户端而言,它只知道自己最早请求的那个A,而不知道中间的B,甚至C、D。传输的信息不会丢失。

例子:

       RequestDispatcherdis=request.getRequestDispatcher(“loginsuccess.jsp”);

       Dis.forward(request,response);

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

转发只有一次请求 

 

重定向: 

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

转发: 

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

三:

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

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

重定向与请求转发使用

前后两个页面 有数据传递 用请求转发,没有则用重定向。
比如servlet查询了数据需要在页面显示,就用请求转发。
比如servlet做了update操作跳转到其他页面,就用重定向。

 

注意>  dispatcher和redirect的小区别: redirect可以重定向一个新的Action而dispatcher不可以,转发只能是chain

 

具体使用方式和实例

 

在本机D:\Javasoftware\struts\struts-2.3.16.3-all\struts-2.3.16.3\docs目录下有home.html帮助文档,guides>result type可以找寻实例,参数是固定的,需要注意。

redirect

重定向

<result name="success" type="redirect">
  <param name="location">foo.jsp</param>
</result>

或者  

<result name="success" type="redirect"> foo.jsp</result>

也可重定向到Action

<result name="success" type="redirect"> /namespace/foo.jsp</result>

  

 

 

 

redirectAction  (可以用redirect代替。)
 
 
1 <package name="package1" extends="struts-default">
2     <action name="login" class="...">
3         <!-- Redirect to another namespace -->
4         <result type="redirectAction">
5             <param name="actionName">xxx</param>
6             <param name="namespace">/xx</param>
7         </result>
8     </action>
9 </package>

 

 
 
  1. <struts>  
  2.   
  3.     <!-- package 包的作用和java中的类包是非常类似的,它主要用于管理一组业务功能相关的action -->  
  4.   
  5.     <package name="bao1" extends="struts-default" namespace="/">  
  6.   
  7.     <!-- 配置一个Action -->  
  8.   
  9.        <action name="login" class="cn.action.LoginAction">  
  10.   
  11.                <!-- 指定Result的类型为plaintext类型 -->  
  12.   
  13.            <result type="redirectaction">  
  14.   
  15.               <!-- 指定重定向的actionName -->  
  16.   
  17.               <param name="actionNam e">aaa</param>  
  18.   
  19.               <!-- 指定重定向的Action所在的命名空间 -->  
  20.   
  21.               <param name="namespace">/</param>  
  22.   
  23.            </result>  
  24.   
  25.        </action>  
  26.   
  27.     </package>  
  28.   
  29.     <package name="csdn1" extends="struts-default" namespace="/">  
  30.   
  31.     <!-- 配置一个Action -->  
  32.   
  33.        <action name="aaa" class="cn.csdn.hr.action.ChongAction">  
  34.   
  35.            <!-- 指定Result的类型为plaintext类型 -->  
  36.   
  37.            <result>./aaa.jsp</result>  
  38.   
  39.            <!--配置一个redirect-action结果类型的result,重定向同一个命名 空间的Action -->  
  40.   
  41.            <result type="redirectaction" name="error">error</result>  
  42.   
  43.        </action>  
  44.   
  45.        <action name="error">  
  46.   
  47.            <result>error.jsp</result>  
  48.   
  49.        </action>  
  50.   
  51.     </package>  
  52.   
  53. </struts>  


chain

1 <package name="package1" extends="struts-default">
2     <action name="login" class="...">
3         <!-- Redirect to another namespace -->
4         <result type="chain">
5             <param name="actionName">xxx</param>
6             <param name="namespace">/xx</param>
7         </result>
8     </action>
9 </package>

  

dispatcher是默认这里就不写了。

 

目录
相关文章
|
4月前
|
SQL IDE Java
hibernate5 Cannot create TypedQuery for query with more than one return using requested result type
hibernate5 Cannot create TypedQuery for query with more than one return using requested result type
24 0
|
Java 数据库连接 mybatis
mybaits报错:The content of element type “resultMap“ must match “(constructor?,id*,result*,associati。。。
mybaits报错:The content of element type “resultMap“ must match “(constructor?,id*,result*,associati。。。
474 0
|
7月前
|
Java
【ES异常】mapper [sortNum] of different type, current_type [long], merged_type [keyword]
【ES异常】mapper [sortNum] of different type, current_type [long], merged_type [keyword]
50 0
|
4月前
|
SQL XML Java
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
54 0
|
5月前
|
JavaScript 前端开发 索引
TS - interface和type的区别
TS - interface和type的区别
22 0
|
Java 数据库连接 mybatis
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
1592 0
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
组件是默认值报错:Props with type Object/Array must use a factory function to return the default value
组件是默认值报错:Props with type Object/Array must use a factory function to return the default value
307 0
组件是默认值报错:Props with type Object/Array must use a factory function to return the default value
ResultSet object has no attribute ‘text‘
ResultSet object has no attribute ‘text‘
195 0
ResultSet object has no attribute ‘text‘
Caused by: 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,globa
在Struts-2.3的配置文件struts.xml中,Caused by: 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-exception-mappings?,action*)"。
1283 0
JakartaEE Struts2 The content of element type "package" must match "(result-types?,interceptors?,...
异常信息: The content of element type "package" must match "(result-types?,interceptors?,default-interceptor- ref?,default-action...
1046 0