1)编写一个类,直接或间接实现com.opensymphony.xwork2.Result 接口,一般是继承于org.apache.struts2.dispatcher.StrutsResultSupport类。然后实现doExecute()方法,输出结果即可。
public class CaptchaResults extends StrutsResultSupport{
@Override // 输出结果即可
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
ValidateCode va = new ValidateCode(120,130,4,100);
BufferedImage image = va.getBuffImg();
// 输出验证码
HttpServletResponse response = ServletActionContext.getResponse();
ImageIO.write(image, "jpeg", response.getOutputStream());
}
}
2)声明结果类型才能使用
<package name="p1" extends="struts-default">
<!-- 结果类型定义 -->
<result-types>
<result-type name="captcha" class="com.pangu.results.CaptchaResults"></result-type>
</result-types>
<action name="captcha">
<result name="success" type="captcha">
<param name="width">200</param>
<param name="height">200</param>
<param name="numConut">4</param>
<param name="grLineNum">200</param>
</result>
</action>
</package>