Struts2学习7-(验证码)

来源:互联网 发布:乌克兰胖爸 知乎 编辑:程序博客网 时间:2024/06/07 22:19

1、生成验证码

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 linoer *自定义结果类型 *1、继承StrutsResultSupport,重写doExecute方法 */public class MyResult extends StrutsResultSupport{private int width;private int height;private int numStyle;private int lineNum;/* (non-Javadoc) * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation) */@Overrideprotected void doExecute(String arg0, ActionInvocation arg1) throws Exception {// TODO Auto-generated method stub//使用第三方生成验证码的jar包/* * 1、拷贝jar包 * 2、创建ValidateCode对象 * 3、获取响应对象输出流 * 4、输出到浏览器 *///参数详解:1、图像宽高度,2、数字格式,3、干扰条数ValidateCode code = new ValidateCode(width,height,numStyle,lineNum);//获取响应对象HttpServletResponse response = ServletActionContext.getResponse();//输出到浏览器code.write(response.getOutputStream());//ImageIO.write(image, "jpg", response.getOutputStream());}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;}public int getNumStyle() {return numStyle;}public void setNumStyle(int numStyle) {this.numStyle = numStyle;}public int getLineNum() {return lineNum;}public void setLineNum(int lineNum) {this.lineNum = lineNum;}}




2、xml配置

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>欢迎登陆</title></head><body><form action="" method="post">用户名:<input type="text" name="username"><br/>密码:<input type="password" name="password"><br/>验证码:<input type="text" name="valicateCode"><img src="${pageContext.request.contextPath}/captchaAction.action"><input type="submit" value="登陆"/></form></body></html>


0 0
原创粉丝点击