http://stenlylee.iteye.com/blog/317179 学习这位朋友的这篇文章,我在详细写下实现步骤。
首先我的例子环境是hibernate+spring+jsf。
再次,backbean上的代码稍微修改一下,如下
public class ImageData implements Serializable {
private static final long serialVersionUID = -8161565799237268271L;
private BufferedImage image;
Integer width = 60;
Integer height = 20;
Color bankgroud = new Color(0xFFFFFF);
Color drawColor = new Color(0x000000);
Font textFont = new Font("Comic Sans ms", Font.PLAIN, 18);
String str = "0,a,2,x,e,5,j,7,l,9,1,b,c,d,4,f,g,h,i,6,k,8,m,n,o,p,q,r,s,t,u,v,w,3,y,z";
Random random = new Random();
public void paint(OutputStream out, Object data) throws IOException {
if (data instanceof ImageData) {
// create the image
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(bankgroud);
graphics.fillRect(0, 0, width, height);
graphics.setColor(drawColor);
graphics.drawRect(0, 0, width - 1, height - 1);
graphics.setFont(textFont);
// 绘制干扰线
for (int i = 0; i < 160; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
graphics.setColor(getRandColor(100, 250));
graphics.drawLine(x, y, x + xl, y + yl);
}
// 绘制验证码
graphics.setColor(drawColor);
String code = "";
String[] array = str.split(",");
for (int i = 0; i < 4; i++) {
String rand = array[random.nextInt(36)];
code += rand;
graphics.drawString(rand, 13 * i + 6, 16);
}
/**
* 这里和上面那位朋友用的有点不一样,我没有自己去写facesUtil这个类,
* 我这里直接把随机生成的代码保存session
*/
FacesContext facesContext = FacesContext.getCurrentInstance();
try {
HttpSession session = (HttpSession) facesContext
.getExternalContext().getSession(true);
session.setAttribute("code", code);
} catch (Exception e) {
e.printStackTrace();
}
// FacesUtil.getSession().setAttribute("code", code);
graphics.dispose();
}
ImageIO.write(image, "JPEG", out);
}
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
然后,这bean要写进jsf的配置文件里面
<managed-bean>
<managed-bean-name>validateCodeImageData</managed-bean-name>
<managed-bean-class>
com.express.bb.ImageData
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
然后要把生成的随机码显示到jsf页面 这里需要用到richfaces的标签显示,可以去http://www.jboss.org/jbossrichfaces/downloads/这里下包(我用的3.1版本的),当把包导进工程之后,
在web.xml文件中添加以下代码
<!-- 配置richfaces -->
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>blueSky</param-value>
</context-param>
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
最后,就可以在你要显示 的页面加上
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<a4j:mediaOutput element="img" cacheable="flase" session="false"
rendered="true" createContent="#{validateCodeImageData.paint}"
value="#{validateCodeImageData}" mimeType="image/jpeg" />
到这里就可以运行一下工程看效果了。
good luck !!
分享到:
相关推荐
中文名: 经典Java EE企业应用实战--基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 原名: 经典Java EE企业应用实战--基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 作者: 李刚 资源格式: PDF 版本: 第一版 出版社: 电子...
2.6 jsf框架的实现 2.7 ajax框架的实现 2.8 使用jdbc连接数据库 2.9 小结 第3章 实现各种框架的集成 3.1 spring框架与其他框架的集成原理 3.2 实现ssh三种框架环境集成 3.3 实现spring与...
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 定义对宠物信息...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...
util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...
做了一个登陆的界面 我想加个验证码 效果如下: 这个验证码的图片是放在 一个textfield里面的 , 我想将验证码放到 这个textfield后面 ,请教 如何实现? login.js 代码如下: LoginPanel = function() { var win, f;...