您的位置:首页 > 编程语言 > Java开发

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

2016-07-03 20:22 585 查看
需求:

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

分析:

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



用解压软件打开这个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.ServletActionC
4000
ontext;
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,验证一下结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息