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

来源:互联网 发布:python货币汇率转换 编辑:程序博客网 时间:2024/05/12 20:56

一,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的全部功能。

原创粉丝点击