自定义结果类型(result的type属性)

本文介绍如何在Struts2中自定义一个结果类型,用于生成验证码。通过分析Struts2的默认结果类型,创建一个名为CodeResult的新结果类型,继承自StrutsResultSupport,并实现视图输出。在Action中定义一个简单的获取验证码的action,然后在struts.xml配置文件中声明自定义结果类型并指定参数。最后,通过在web.xml中注册Struts2并部署到Tomcat来验证效果。

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

需求:

自定义一个结果类型,用于自动生成验证码,生成验证需要借助一个第三方工具validatecode.jar,当然也可以自己实现

分析:

要想实现一个自定义的结果类型,那么,我们可以参考struts2官方是如何实现现有的结果类型的。我们在struts.xml中定义package元素的时候,一般会继承一个struts-default.xml的文件,这个文件中包含了struts2里的很多核心内容,结果类型也在其中。我们可以在struts2的核心jar包中找到这个文件,打开来参考一下。我这里以struts2-core-2.3.15.3.jar为例:

struts核心包解压

用解压软件打开这个jar包,在根目录下,就可以找到这个struts-default.xml文件,再次打开这个文件,找到我们要的result-types定义的地方:

<result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
        </result-types>

以上可以看出,struts2提供了这些结果类型。
我们现在要自定义一个结果类型,那么,就可以参照这里的结果类型进行定义,我们以默认的结果类型dispatcher为例,我们看到它的实现类为org.apache.struts2.dispatcher.ServletDispatcherResult
在myeclipse中,ctrl+shift+t 找到这个类

这里写图片描述

分析这个类,我们看到,这个类主要有两点,继承了一个类为StrutsResultSupport,实现了里面的deExecute()方法,在方法中,实现了视图的输出

这里写图片描述

于是,我们得出,我们的自定义结果类型也可以如此来实现。

返回结果的使用,这里就不缀述了,既然找到方法了,那么,下面就开始动手吧。

详细步骤:

我们新建一个页面,这个页面只是为了显示验证码,就不再做任何的业务了。
在这个页面中,我们生成验证码是访问的一个action,最核心的就只有这一句:

验证码:

<input/><img alt="验证码" src="${pageContext.request.contextPath}/verifycode.action">

既然要访问这个action,那么我们就来写这个action,这个action没有任何写的,就是一个普通的action,里面的getCode()方法直接返回success

package demo.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 验证码获取Action
 * @author Minhellic
 *
 */
public class VerifyCodeAction extends ActionSupport {

    /**
     * 这里直接返回,验证码通过返回的type来获取
     * @return
     */
    public String getCode() {
        return SUCCESS;
    }

}

这样做的目的就是在使用时,直接使用result的type属性来指定我们自定义的返回结果类型,用这个返回结果类型来输出验证码。所以,下一步就是要定义我们的重头戏:自定义返回结果类型:

我们来定义好我们的返回结果类型:继承类StrutsResultSupport,实现里面的deExecute()方法,在方法中,实现视图的输出
在自定义CodeResult中,我们可以提供一些参数,并为这些参数都提供setter方法,那么在使用的时候,就可以在struts.xml中的Action > result 下,使用param标签注入值,这样使用我们的返回结果类型更加灵活

package demo.results;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
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;

/**
 * 自定义Result:
 * 1.直接或间接地实现接口com.opensymphony.xwork2.Result,这里采用的是继承StrutsResultSupport类
 * 2.重写doExcute()方法,在该方法中实现视图的输出
 * @author Minhellic
 *
 */
public class CodeResult extends StrutsResultSupport {

    private int width = 120;//验证码宽
    private int height = 80;//验证码高
    private int codeCount = 4;//验证码字符数
    private int lineCount = 100;//干扰线条数

    /*
     * 为每个属性添加set方法,以方便可以在struts.xml中的Action > result 下,使用param标签注入值
     */
    public void setWidth(int width) {
        this.width = width;
    }

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

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

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

    /**
     * 重写父类的方法,以向页面输出验证码
     */
    @Override
    protected void doExecute(String arg0, ActionInvocation arg1)
            throws Exception {
        //借用第三方工具包ValidateCode.jar生成验证码
        ValidateCode vc = new ValidateCode(width, height, codeCount, lineCount);
        BufferedImage bi = vc.getBuffImg();

        //得到响应并输出
        HttpServletResponse response = ServletActionContext.getResponse();
        ImageIO.write(bi, "jpeg", response.getOutputStream());
    }

}

OK,现在自定义的返回结果类型CodeResult已经有了,那么就是使用了

在struts.xml中,使用result-types标签来引用自定义的返回结果类型

<!-- 引用自定义的返回类型 -->
        <result-types>
            <result-type name="code" class="demo.results.CodeResult"></result-type>
        </result-types>

然后,在定义action的result时,就可能指定type为自定义的返回结果类型了,同时,由于我们自定义返回结果类型时,还提供了一些参数,那么,也可以使用param标签对这些参数进行注入

<!-- action定义 -->
        <action name="verifycode" class="demo.action.VerifyCodeAction" method="getCode">
            <!-- 在result中,使用type属性指定返回的类型为自定义的code,如果不指定,会是dispatcher -->
            <result name="success" type="code">
                <!-- 由于自定义的返回类型中,对这些属性提供了set方法,所以这里可以使用param标签注入值 -->
                <param name="width">300</param>
                <param name="height">30</param>
                <param name="codeCount">5</param>
                <param name="lineCount">30</param>
            </result>
        </action>

完整的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>

    <package name="demo" extends="struts-default">

        <!-- 引用自定义的返回类型 -->
        <result-types>
            <result-type name="code" class="demo.results.CodeResult"></result-type>
        </result-types>

        <!-- action定义 -->
        <action name="verifycode" class="demo.action.VerifyCodeAction" method="getCode">
            <!-- 在result中,使用type属性指定返回的类型为自定义的code,如果不指定,会是dispatcher -->
            <result name="success" type="code">
                <!-- 由于自定义的返回类型中,对这些属性提供了set方法,所以这里可以使用param标签注入值 -->
                <param name="width">300</param>
                <param name="height">30</param>
                <param name="codeCount">5</param>
                <param name="lineCount">30</param>
            </result>
        </action>
    </package>

</struts>

最后,一定不要忘记在web.xml中注册struts2哈

  <!-- struts2的核心过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter> 
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

最后,部署到tomcat,验证一下结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值