结果类型视图(逻辑结果视图)
前奏:该部分内容指的就是struts配置文件中的result元素的使用
1、result元素
作用:为动作指定结果视图
属性:
name:逻辑视图的名称,对应着动作方法的返回值。默认值是success。
type:结果类型,指的就是用什么方式转到定义的页面。默认是dispatcher。
2、result元素中type的取值
type属性的取值在struts-default.xml中定义着。
常用结果类型介绍:
result:结果视图
name:结果视图名称,和动作方法的返回值一致,指明什么结果—-success,input,error,none,login
type:指明以什么方式转到页面,默认值是dispatcher——请求转发
dispatcher:转发
redirect:重定向
<action name="action2" class="com.qianfeng.web.action.TestAction3">
<result name="success" type="redirect">/success.jsp</result>
</action>
chain:转发到另一个action
转发到同一个包下的另一个action
<result name="success" type="chain">action3</result>
转发到不同包下的另一个action
<result name="success" type="chain">
<param name="namespace">/user</param>
<param name="actionName">action3</param>
</result>
redirectAction:重定向到另一个action
重定向到同一个包下的另一个action
<result name="success" type="redirectAction">action3</result>
重定向到不同包下的另一个action
<result name="success" type="redirectAction">
<param name="namespace">/user</param>
<param name="actionName">action3</param>
</result>
4、自定义结果类型
通过前面的内容,我们看出,其实结果类型就是一个类,这些类都实现了com.opensymphony.xwork2.Result接口,或者继承自该接口的实现类org.apache.struts2.dispatcher.StrutsResultSupport。这些类都有一个doExecute方法,用于执行结果视图。
综上:我们也可以自己写一个结果视图。
例子:
输出CAPTCHA图像的结果类型。
CAPTCHA(Completely Automated Public Turing Test to Tell Computers and Humans Apart 全自动区分计算机和人类的图灵测试)————>简称:验证码。
第一步:写一个类,实现接口或者继承接口的实现类
package com.qianfeng.result;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import cn.dsna.util.images.ValidateCode;
import com.opensymphony.xwork2.ActionInvocation;
public class CodeResult extends StrutsResultSupport {
/**
* 自定义结果视图
* 第一步:编写一个类,继承StrutsResultSupport类,重写doExecute方法
* 第二步:在struts.xml文件中声明结果类型
* 第三步:在配置action时,type属性指定声明的结果类型
*/
private int width;
private int height;
@Override
protected void doExecute(String arg0, ActionInvocation arg1)
throws Exception {
/*
* ValidateCode是一个ValidateCode.jar包中的一个类。该jar包是一个生成验证码的工具类
* 构造方法参数详解:
* width:宽度
* height:高度
* codeCount:验证码个数
* lineCount:干扰线条数
*/
ValidateCode code= new ValidateCode(width, height, 4,10);
//使用servletActionContext对象获取response对象
HttpServletResponse response = ServletActionContext.getResponse();
//该方法需要一个OutputStream,其实就是response对象的getOutputStream()
code.write(response.getOutputStream());
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
第二步:在struts.xml文件中配置结果类型
<package name="p3" extends="struts-default" abstract="true">
<!-- 声明结果视图 -->
<result-types>
<result-type name="code" class="com.qianfeng.result.CodeResult"></result-type>
</result-types>
</package>
第三步:在action配置时引用
<package name="p3" extends="struts-default" abstract="true">
<!-- 声明结果视图 -->
<result-types>
<result-type name="code" class="com.qianfeng.result.CodeResult"></result-type>
</result-types>
<action name="codeAction">
<result name="success" type="code">
</result>
</action>
</package>
最终结果:
扩展:通过可配置的参数,实现图像大小的调整
5、全局视图和局部视图
5.1、局部视图
5.2、全局视图
运行结果: