Struts2的<result>结果

本文介绍了Struts2框架中的结果视图概念,包括局部和全局逻辑视图的区别及其应用,并详细讲解了如何通过自定义结果类型来生成CAPTCHA图像的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.结果视图:局部和全局逻辑视图

1.1 局部逻辑视图:只服务于当前的动作


1.2 全局逻辑视图:服务于所有的动作

//全局逻辑视图:所有动作没有name = success 的局部视图,到全局视图找


更好的利用package的继承性组织全局视图:


2.自定义结果类型:CAPTCHA图像

2.1所有的结果类型都是直接或间接的实现了Result接口



2.2自定义结果类型的开发步骤

1) 直接或间接实现Result接口

//输出随机验证码图片
public class CaptchaResult extends StrutsResultSupport {
	private int width = 120;
	private int height = 20;
	
	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	protected void doExecute(String finalLocation, ActionInvocation invocation)
			throws Exception {
		ValidateCode code = new ValidateCode(width, height, 4, 9);
		HttpServletResponse response = ServletActionContext.getResponse();
		code.write(response.getOutputStream());
	}

}

2) 在struts.xml中声明结果类型
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="default" extends="struts-default" abstract="true">
		<result-types>
			<result-type name="captcha" class="com.itheima.results.CaptchaResult"></result-type>
		</result-types>
	</package>
</struts>

3) 在动作中使用自定义的结果类型
	<package name="p1" extends="default">
		<action name="captcha">
			<result name="success" type="captcha">
				<param name="width">120</param>
				<param name="height">25</param>
			</result>
		</action>
		<action name="action1"></action>
	</package>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值