day25_Struts2学习笔记_01 (8)

  1、编写一个普通类,继承自StrutsResultSupport的类,并且重写doExcecute方法。此为自定义结果类型的类。

package com.itheima.web.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;
/**
 * 自定义结果类型 
 *         第一步:编写一个普通类,继承自StrutsResultSupport的类,并且重写doExcecute方法。
 *         第二步:在struts.xml中进行配置。
 *         第三步:在配置action时,type属性指定声明的结果类型名称。
 * 
 * @author cmj
 */

public class CAPTCHAResult extends StrutsResultSupport {
    // 通过配置文件,调整生成图片的大小
    private int width;
    private int height;
    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;
    }
    /*
     * 使用第三方生成验证码的jar包 
     *      1.拷贝ValidateCode.jar到工程lib目录 
     *      2.创建ValidateCode的对象
     *      3.获取响应对象输出流 
     *      4.输出到浏览器
     */

    // Servlet的中原来怎么写,现在还怎么写
    @Override
    protected void doExecute(String finalLocation, ActionInvocation invocation) 
            throws Exception 
{
        // 创建ValidateCode的对象,该对象的构造参数详解:1:图像宽度   2.图像高度   3.数字的格式   4.干扰线条数
        ValidateCode code = new ValidateCode(width, height, 410);
        // 获取响应对象输出流
        HttpServletResponse response = ServletActionContext.getResponse();
        // 输出到浏览器
        code.write(response.getOutputStream());
    }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppdpj.html