struts2自定义返回类型

1、自定义返回类,继承StrutsResultSupport类

package cn.jieou.struts.resulttype;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
import cn.dsna.util.images.ValidateCode;
/**
 * 自定义返回类型
 * 实现返回图片验证码功能
 * @author LMJ
 *
 */
public class Captcharesult extends StrutsResultSupport{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /*
     * 定义参数,使用getter\setter方法注入
     * */
    private Integer width;
    private Integer height;
    private Integer codeCount;
    private Integer lineCount;

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

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

    public Integer getCodeCount() {
        return codeCount;
    }

    public void setCodeCount(Integer codeCount) {
        this.codeCount = codeCount;
    }

    public Integer getLineCount() {
        return lineCount;
    }

    public void setLineCount(Integer lineCount) {
        this.lineCount = lineCount;
    }

    @Override
    protected void doExecute(String finalLocation, ActionInvocation invocation) 
            throws Exception {
        // TODO Auto-generated method stub
        HttpServletResponse response = ServletActionContext.getResponse();
        //使用验证码jar包
        ValidateCode validateCode = new ValidateCode(width, height, codeCount, lineCount);
        //以流的形式输出
        validateCode.write(response.getOutputStream());
    }
}

2、在配置文件注册到strut2中
注意:在当前package里面注册是局部性的,如果比较多地方用到该返回类型,应该使用全局配置

<!-- 这里是我自己定义的一个struts配置,继承struts-default -->
<package name="MJ-struts" extends="struts-default">
        <result-types>
        <!-- 这里把自定义的返回类型注册到strut2中 -->
            <result-type name="captcha" class="cn.jieou.struts.resulttype.Captcharesult"/>
        </result-types>
        <!-- 这里定义全局返回结果,只要继承了该配置,方法返回结果是captcha的就会执行返回类型对应类中的doExecute方法 -->
        <global-results>
            <result name="captcha" type="captcha">
                <param name="width">200</param>
                <param name="height">100</param>
                <param name="codeCount">4</param>
                <param name="lineCount">4</param>
            </result>
        </global-results>
    </package>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级奶爸MJCX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值