`
Mr_Chunlei
  • 浏览: 28729 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Struts2 result类型,global-result,动态result,声明异常

 
阅读更多

一,result的type类型

<action name="hello" class="com.thridProject.action.HelloAction">
			<result type="dispatcher" name="success">/hello.jsp</result>
			<result type="redirect" name="success">/hello.jsp</result>
		</action>
		<action name="r2">
			<result type="chain" name="success">hello</result>
			<result type="redirectAction" name="success">hello</result>
		</action>

通常最常用的四种类型,当然还有freemarker类型,不在阐述。

我们都知道在页面跳转还有两种方式,服务器跳转和浏览器重新发送跳转。(forward和redirect)

dispatcher是默认的类型,是服务器forward类型跳转,不过跳转的页面必须是jsp或者html的页面类型

redirect是浏览器重发跳转,跳转到额页面必须是jsp或者是html的类型,也可以是action

所以另两个就不难理解了,都是action跳转,其中chain是服务器action跳转,而redirectAction就是redirect类型的action跳转了。


二、redirect和redirectAction chain的区别
struts2中关于result的返回类型一般我们是转发到一个jsp页面或者是html页面等,但是struts2中的result的返回类型还有redirect,redirectAction,chain。对于这三种返回类型之间肯定是有区别的,下面我们来看看关于redirect redirectAction chain这三种struts2的返回类型之间的区别。

当使用type=“redirectAction” 或type=“redirect”提交到一个action并且需要传递一个参数时。这里是有区别的:
使用type=“redirectAction”时,结果就只能写Action的配置名,不能带有后缀:“.action”

使用type=“redirect”时,结果应是action配置名+后缀名

1.redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。

2.redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。

3.chain:action处理完后转发到一个action,请求参数全部丢失,action处理结不会

三、Global-results配置,可以实现多个result,但在当前包中action的result中没有找到result时会调用

<global-results>
		<result name="">/xxxx.jsp</result>
		<result name=
		</global-results>

四、动态result实现

1)我们前面有一种使用通配符动态的为result来实现动态页面的跳转

2)中我们可以使用${}其中大括号可以是action中定义的属性值,可以去值栈中的值来配置result

3)也可以通过${}来动态的传参

五、声明异常

1)可以对action执行发现的异常进行捕捉,并且跳转到异常页面,这个时候的Struts.xml的配置为:

<action name="hello" class="com.thridProject.action.HelloAction">
			<result type="dispatcher" name="success">/hello.jsp</result>
			<exception-mapping result="error" exception="Exception类型"></exception-mapping>
			<result name="error">/error.jsp</result>
		</action>
2)

同样也存在Gloable-exception-mapping,同时Gloable-result必须写在Gloable-exception-mapping前面。

3)使用继承其他包来实现异常异常的共同处理

4)异常的实现以及Struts功能的实现大多数都是由于Struts存在了拦截器的效果, 这个拦截器就跟filter差不多,我们也可以定义自己的拦截器,但是我们定义了自己的拦截器,然后再使用时,最好加上原本的struts默认的拦截器,这样才能保证struts的全部功能。

分享到:
评论

相关推荐

    解决struts2下载异常的jar包 struts2-sunspoter-stream-1.0.jar

    添加如下内容 --&gt; &lt;result-types&gt; &lt;result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX"/&gt; &lt;/result-types&gt; &lt;action name="download" class="com.DownloadAction"&gt; &lt;...

    Struts2详解,Struts2与Struts1的区别

    Struts2和Struts1的常见区别 二. Struts2入门 3. 快速配置Struts2环境 4. 开发一个Struts2简单实例 5. struts.xml文件 三. Struts2深入开发 6. 常用标签配置和使用 7. 常用&lt;result&gt;标签配置和使用 8....

    jfreechar 整合struts2.1.8版本生成线图,饼图,柱形图

    type : result类型 不写则选用superpackage的type struts-default.xml中的默认为dispatcher --&gt; &lt;result name="success" type="dispatcher"&gt;/talk.jsp&lt;/result&gt; &lt;!-- 参数设置 name:对应Action中...

    structs2动态添加表单

    &lt;result name="IOException" &gt;/error/ioexception.jsp&lt;/result&gt; &lt;result name="SQLException" &gt;/error/sqlexception.jsp&lt;/result&gt; &lt;/global-results&gt; &lt;global-exception-mappings&gt; &lt;exception-mapping result=...

    轻松短租网

    &lt;package name="default" namespace="/" extends="struts-default"&gt; &lt;global-results&gt; &lt;result name="error"&gt;/error.jsp&lt;/result&gt; &lt;/global-results&gt; &lt;global-exception-mappings&gt; &lt;exception-mapping ...

    struts2拦截器

    &lt;package name="mySturtsInterceptor" extends="struts-default"&gt; &lt;interceptor name="MyInterceptor" class="com.cn.system.interceptor.MyInterceptor"&gt;&lt;/interceptor&gt; &lt;interceptor-stack name=...

    SSH开发纪要整合解决四大问题(中文、jar包冲突、延时加载、模块化)文档

    DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"&gt; &lt;struts-config&gt; &lt;data-sources /&gt; &lt;form-beans &gt; ...

    OA_struts2_hibernate3_spring2.5.rar

    &lt;package name="struts2" extends="struts-default"&gt; &lt;global-results&gt; &lt;result name="add_success" type="redirect"&gt;/common/pub_add_success.jsp&lt;/result&gt; &lt;result name="del_success" type="redirect...

    spring3.2+strut2+hibernate4

    -- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 --&gt; &lt;constant name="struts.action.extension" value=","/&gt; &lt;!-- 当配置文件修改后,系统自动加载该文件。开发阶段建议打开此功能...

    拦截器和控制器的区别

    2、拦截器不依赖于servlet容器,通过动态代理实现,过滤器依赖于servlet容器 3、拦截器在方法前后,异常前后等调用,而过滤器只能在请求前和请求后各调一次。 4、拦截器可以利用依赖注入,因此在spring框架程序中,...

    struts json 类型异常返回到js弹框问题解决办法

    struts json 类型异常返回到js弹框问题解决办法 当struts 框架配置了异常时 例如: &lt;package name=sysCnn namespace=/ extends=json-default&gt; &lt;global&gt; &lt;result name=error&gt;/WEB-INF/pages/error.jsp&lt;/result&gt; ...

    前端-后端java的Util类的工具类

    │ struts.xml │ ├─28个java常用的工具类 │ │ Base64.java │ │ Base64DecodingException.java │ │ CConst.java │ │ CharTools.java │ │ ConfigHelper.java │ │ Counter.java │ │ CTool.java │ │...

    Struts in Action中文版

    2. 深入 STRUTS架构..................................................................................37 2.1. 随便谈谈......................................................................................

    struts in Action

    2. 深入STRUTS 架构..................................................................................37 2.1. 随便谈谈.......................................................................................

Global site tag (gtag.js) - Google Analytics