struts2 的Action类
ValidateImgAction.javajava 代码
-
packagecn.gx80.control.action.basic;
-
-
importjava.io.ByteArrayInputStream;
-
importjava.io.ByteArrayOutputStream;
-
importjava.io.IOException;
-
importjava.util.Map;
-
-
importorg.apache.struts2.interceptor.SessionAware;
-
-
importcn.gx80.service.util.ValidateImageService;
-
importcn.gx80.util.Key;
-
-
-
-
-
-
-
@SuppressWarnings("unchecked")
-
publicclassValidateImgActionextendsStreamBasicActionimplementsSessionAware{
-
-
privatestaticfinallongserialVersionUID=6894525175454169331L;
-
-
privatestaticfinalStringDefault_ValidateCode="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
-
privateMapsession;
-
-
privateintwidth;
-
privateintheight;
-
privateintfontSize;
-
privateintcodeLength;
-
-
privateValidateImageServicevalidateImageService;
-
-
-
publicValidateImgAction(){
-
this.contentType="image/jpeg";
-
width=80;
-
height=20;
-
fontSize=16;
-
codeLength=4;
-
}
-
-
publicvoidsetSession(Mapsession){
-
this.session=session;
-
}
-
-
-
publicvoidsetWidth(intwidth){
-
this.width=width;
-
}
-
-
publicvoidsetHeight(intheight){
-
this.height=height;
-
}
-
-
-
publicvoidsetFontSize(intfontSize){
-
this.fontSize=fontSize;
-
}
-
-
publicvoidsetCodeLength(intcodeLength){
-
this.codeLength=codeLength;
-
}
-
-
publicvoidsetValidateImageService(ValidateImageServicevalidateImageService){
-
this.validateImageService=validateImageService;
-
}
-
-
publicStringexecute()throwsException{
-
session.put(Key.ValidateCodeByLogin,createInputStream(ValidateImageService.Disturb_Type_Simple));
-
returnSUCCESS;
-
}
-
-
-
privateStringcreateInputStream(intdisturbType)throwsIOException{
-
ByteArrayOutputStreambos=newByteArrayOutputStream();
-
StringvalidateCode=null;
-
validateCode=validateImageService.createValidateCode(disturbType,fontSize,bos,width,height,getText("System.validateCode",Default_ValidateCode),codeLength);
-
inputStream=newByteArrayInputStream(bos.toByteArray());
-
bos.close();
-
returnvalidateCode;
-
}
-
}
验证码生成接口 ValidateImageService.java
java 代码
-
packagecn.gx80.service.util;
-
-
importjava.io.ByteArrayOutputStream;
-
-
-
-
-
-
-
publicinterfaceValidateImageService{
-
-
-
-
StringDefault_ValidateCode="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
-
-
-
intDisturb_Type_Default=0;
-
-
-
-
intDisturb_Type_Simple=1;
-
-
-
-
intDisturb_Type_Complex=2;
-
-
-
-
-
-
-
-
-
-
-
-
-
publicabstractStringcreateValidateCode(intdisturbType,intfontSize,ByteArrayOutputStreambos,intwidth,intheight,StringvalidateCode,intcodeLength);
-
-
}
验证码生成的实现 ValidateImageServiceImp.java
java 代码
-
packagecn.gx80.service.util;
-
-
importjava.awt.Color;
-
importjava.awt.Font;
-
importjava.awt.Graphics;
-
importjava.awt.image.BufferedImage;
-
importjava.io.ByteArrayOutputStream;
-
importjava.io.IOException;
-
importjava.util.Random;
-
-
importjavax.imageio.ImageIO;
-
importjavax.imageio.stream.ImageOutputStream;
-
-
-
publicclassValidateImageServiceImpimplementsValidateImageService{
-
-
publicStringcreateValidateCode(intdisturbType,intfontSize,ByteArrayOutputStreambos,intwidth,intheight,StringvalidateCode,intcodeLength){
-
BufferedImagebImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
-
Graphicsg=bImg.getGraphics();
-
Randomrandom=newRandom();
-
-
if(null==validateCode||validateCode.isEmpty()){
-
validateCode=Default_ValidateCode;
-
}
-
if(fontSize>=height){
-
fontSize=height-1;
-
}
-
-
drawOutline(g,width,height);
-
switch(disturbType){
-
caseDisturb_Type_Simple:
-
drawSimpleDisturb(g,random,width,height);
-
break;
-
caseDisturb_Type_Complex:
-
drawDisturb(g,random,width,height);
-
break;
-
default:
-
break;
-
}
-
-
Stringcode=drawCode(g,random,validateCode,codeLength,width,height,fontSize);
-
g.dispose();
-
try{
-
ImageOutputStreamimOut=ImageIO.createImageOutputStream(bos);
-
ImageIO.write(bImg,"JPEG",imOut);
-
imOut.close();
-
}catch(IOExceptione){
-
e.printStackTrace();
-
}
-
returncode;
-
}
-
-
-
-
-
-
-
-
privatestaticvoiddrawOutline(Graphicsg,intwidth,intheight){
-
g.setColor(Color.white);
-
g.fillRect(0,0,width,height);
-
g.setColor(Color.BLACK);
-
g.drawRect(0,0,width-1,height-1);
-
}
-
-
-
-
-
-
-
-
-
privatestaticvoiddrawDisturb(Graphicsg,Randomrandom,intwidth,intheight){
-
intx,y,x1,y1;
-
for(inti=0;i<width;i++){
-
x=random.nextInt(width);
-
y=random.nextInt(height);
-
x1=random.nextInt(12);
-
y1=random.nextInt(12);
-
g.setColor(getRandomColor(random,120,255));
-
g.drawLine(x,y,x+x1,y+y1);
-
g.fillArc(x,y,x1,y1,random.nextInt(360),random.nextInt(360));
-
}
-
}
-
-
-
-
-
-
-
-
-
privatestaticvoiddrawSimpleDisturb(Graphicsg,Randomrandom,intwidth,intheight){
-
g.setColor(getRandomColor(random,160,200));
-
for(inti=0;i<155;i++)
-
{
-
intx=random.nextInt(width);
-
inty=random.nextInt(height);
-
intxl=random.nextInt(12);
-
intyl=random.nextInt(12);
-
g.drawLine(x,y,x+xl,y+yl);
-
}
-
}
-
-
-
-
-
-
-
-
-
privatestaticColorgetRandomColor(Randomrandom,intpMin,intpMax){
-
pMax=(Math.abs(pMax)>255?255:Math.abs(pMax));
-
pMin=(Math.abs(pMin)>255?255:Math.abs(pMin));
-
-
intr=pMin+random.nextInt(Math.abs(pMax-pMin));
-
intg=pMin+random.nextInt(Math.abs(pMax-pMin));
-
intb=pMin+random.nextInt(Math.abs(pMax-pMin));
-
-
returnnewColor(r,g,b);
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
privatestaticStringdrawCode(Graphicsg,Randomrandom,StringvalidateCode,intcodeLength,intwidth,intheight,intfontSize){
-
intvalidateCodeLength=validateCode.length();
-
Fontfont1=newFont("Verdana",Font.BOLD,fontSize);
-
Fontfont2=newFont("serif",Font.BOLD,fontSize);
-
-
StringBuffersb=newStringBuffer();
-
intx,y;
-
for(inti=0;i<codeLength;i++){
-
x=(width/codeLength-1)*i+random.nextInt(width/(codeLength*2));
-
y=random.nextInt(height-fontSize)+fontSize;
-
sb.append(getRandomChar(validateCode,validateCodeLength,random));
-
g.setColor(getRandomColor(random,70,150));
-
if(sb.substring(i).getBytes().length>1)
-
g.setFont(font2);
-
else
-
g.setFont(font1);
-
g.drawString(sb.substring(i),x,y);
-
}
-
returnsb.toString();
-
}
-
-
-
-
-
-
-
-
-
privatestaticchargetRandomChar(StringvalidateCode,intvalidateCodeLength,Randomrandom){
-
returnvalidateCode.charAt(random.nextInt(validateCodeLength));
-
}
-
}
struts.xml 配置
xml 代码
-
<packagename="gx80-test"namespace="/test"extends="gx80-default">
-
-
<actionname="validateCode"class="cn.gx80.control.action.basic.ValidateImgAction">
-
<interceptor-refname="gx80DefaultStack"/>
-
<interceptor-refname="staticParams"/>
-
<paramname="width">100</param>
-
<paramname="height">30</param>
-
<paramname="fontSize">18</param>
-
<paramname="codeLength">5</param>
-
<resultname="success"type="stream">
-
<paramname="contentType">image/jpeg</param>
-
</result>
-
</action>
-
-
</package>
当然还有语言文件中的
验证码字符串 global.properties
java 代码
-
System.validateCode=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/u4E00/u4E8C/u4E09/u56DB/u4E94/u516D/u4E03/u516B/u4E5D/u5341/u767E/u5343/u4E07/u5E7F/u897F/u5357/u5B81/u79D1/u56ED/u5927/u9053/u8F6F/u4EF6/u4E2D/u56FD/u4EBA/u6C11/u4E07/u5C81
一切配置好后,访问
127.0.0.1:8080/gx80/test/validateCode.do
分享到:
相关推荐
在struts2下产生验证码,包含所需要的action(返回的就是验证码图片),struts配置文件,页面的调用。
主要是用strus2实现验证码功能,里面包含验证码的实现以及xml文件里面的action的书写
使用struts的流Action响应生成一个验证码返回给客户端。 可以练习struts的使用,欢迎各位下载交流。
生成验证码,本验证码为Servlet形式,可以凭开发经验,例如在Struts中,将其写在Action的方法中(void类型)从而转为Struts形式,调用时只需使img标签的src属性= 1.(servletname) 2.(methodname_actionname.action)
kaptcha验证码在使用上比其它...其中主要有几处配置,一是在Web.xml文件里增加相应的配置,后是在Action里做相关的生成及校验工作。前台采用不刷新的改变验证码,另外还做了一个手动获取验证码的功能,希望对大家有用。
生成验证码,本验证码为Servlet形式,可以凭开发经验,例如在Struts中,将其写在Action的方法中(void类型)从而转为Struts形式,调用时只需使img标签的src属性= 1.(servletname) 2.(methodname_actionname.action)
Struts 2.0技术综合应用,包括上传图片功能,明文加密算法SHA-1和MD5,上传用户头像,根据action随机生成验证码,用链接实现数据的分页处理,以及拦截器和Logger日志框架的引入,总之相当强大,学习Struts 2.0,把这...
jsp获取action传来的session和session清空以及判断 jsp获取action传来的session是jsp开发中的一种常见需求,Session是Web应用...* 实现生成登录验证码 等等。Session的使用可以提高jsp应用程序的灵活性和可扩展性。
控制层:Struts2控制器组件、Action组件 c.业务层:Bean组件 d.数据访问层:DAO组件(JDBC) 4.数据库设计 1)数据库导入 create database dangdang; //创建库 use dangdang; //进入dangdang库 set names utf8;...
\web\WebRoot\index.jsp 首页 =================================================================== D:\web\WebRoot\include\CSS.CSS D:\web\WebRoot\include\image.jsp 生成验证码的jsp ============...
20.5.4 生成随机验证码的imgNum类 358 20.5.5 用户登录页面index.jsp 359 20.5.6 验证用户登录信息的Servlet类login 360 20.6 显示宠物信息模块的实现 363 20.6.1 对应宠物的实体类User 363 20.6.2 定义对宠物信息...
技术实现:Struts2、JSP、MySQL、Jquery、Javascript、Ajax 、Json、JUnit 项目描述: 本项目主要实现了用户管理模块、商品分类展示模块、购物车模块、订单模块。 用户管理模块实现了用户注册、实时验证、用户登录...
\contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...
本章我们将会开发基于Struts2框架的Hello World.我们的Hello World程序是你开发基于Struts2框架程序的第一步.这里我们会提供循序渐进的教程来开发基于Struts2框架的Hello World程序. 教程包含基本的步骤像创建目录...
Kaptcha(Google验证码) Druid(数据源) Atomikos(数据源事物) SpringCache(缓存集成redis) Shrio(权限控制,可以精确到按钮级别,集成redis) Redis(缓存服务器) Log4j Maven Generator(Mybatis自动生成工具) 前端技术点...
国内知名的高端IT技术作家,已出版《Spring 2.0宝典》、《基于J2EE的Ajax宝典》、《轻量级J2EE企业应用实战》、《Struts 2权威指南》、《Ruby On Rails敏捷开发最佳实践》等著作。 目录: 第0章 学习Java...