1)編寫一個(gè)類前酿,直接或間接實(shí)現(xiàn)com.opensymphony.xwork2.Result 接口陌宿,一般是繼承于org.apache.struts2.dispatcher.StrutsResultSupport類贡未。然后實(shí)現(xiàn)doExecute()方法宏多,輸出結(jié)果即可瑞凑。
public class CaptchaResults extends StrutsResultSupport{
@Override // 輸出結(jié)果即可
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
ValidateCode va = new ValidateCode(120,130,4,100);
BufferedImage image = va.getBuffImg();
// 輸出驗(yàn)證碼
HttpServletResponse response = ServletActionContext.getResponse();
ImageIO.write(image, "jpeg", response.getOutputStream());
}
}
2)聲明結(jié)果類型才能使用
<package name="p1" extends="struts-default">
<!-- 結(jié)果類型定義 -->
<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>