版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、JavaEE5学习笔记12-JSF集成AJAX使用经验总结刘岩JSF也支持AJAX,ajax4jsf是专门为JSF提供的AJAX框架。他的工作原理就是在客户端加载一个AJAX引擎,在这个引擎之上是呈现给用户的JSF页面,引擎之下就是JSF框架,也就是正常的JSF生命周期。换句话说页面上的组件一旦配置了ajax4jsf标签,那么该组件的JSF生命周期一直生活在ajax4jsf的阴影之下。用了ajax4jsf,开发者可以不用像以往的JSP页面那样,写js脚本,页面html组件触发js事件后,进行ajax请求,而是像组件监听器一样,对组件的事件进行一个监听,触发事件后,开始ajax引擎预处理,之后再
2、走JSF生命周期。生命图形如下首先下载,从山寨网站JKL/Downloadajax4jsf111jar.htm可以下载其jar包。之后再下载通用的一些jar包commons-beanutils.jarcommons-collections.jarcommons-digester.jarcommons-logging.jarweb.xml内容加入Ajax4jsf Filterajax4jsfajax4jsfFaces ServletREQUESTFORWARDINCLUDE这里声明一个过滤器,对servler名称叫做Faces Servlet的请求进行过滤,Faces Servlet就是JSF的
3、。正是中的过滤器。使得ajax的filter将会作用于直接从客户端过来的request,通过forward过来的request,通过include过来的request。就是这样一个过滤器拦截了所有的JSF请求。使用AJAX功能的页面代码用户登录页面用户登录页面注意:1):使用ajax功能必须加入ajax4jsf标签声明2):声明一个ajax作用域在这个作用域范围内的组件都可以使用AJAX功能。3):对组件进行显示的ajax声明,告诉引擎,这个组件需要进行ajax引擎处理,引擎过后才能交给JSF生命周期4):ajax4jsf也有相应的组件,比如用ajax的方式输出一个图形容器托管Bean如下/*
4、 * ClassName: UserBean.java * created on Nov 1, 2007 * Copyrights 2007-2008 qjyong All rights reserved. * EMail: */package ajax;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.faces.context.Fac
5、esContext;import javax.faces.event.ActionEvent;import javax.imageio.ImageIO;import javax.servlet.http.HttpSession;/* * author qiujy * version 1.0 * */public class UserBean private String userName;private String password;private String code; /用户输入的验证码private String validUserMsg;private String validat
6、eCodeMsg;private String myname;public String getMyname() return myname;public void setMyname(String myname) this.myname = myname;/* * return the code */public String getCode() return code;/* * param code the code to set */public void setCode(String code) this.code = code;/* * return the validUserMsg
7、 */public String getValidUserMsg() return validUserMsg;/* * param validUserMsg the validUserMsg to set */public void setValidUserMsg(String validUserMsg) this.validUserMsg = validUserMsg;/* * return the userName */public String getUserName() return userName;/* * param userName the userName to set */
8、public void setUserName(String userName) this.userName = userName;/* * return the password */public String getPassword() return password;/* * param password the password to set */public void setPassword(String password) this.password = password;/* * return the validateCodeMsg */public String getVali
9、dateCodeMsg() return validateCodeMsg;/* * param validateCodeMsg the validateCodeMsg to set */public void setValidateCodeMsg(String validateCodeMsg) this.validateCodeMsg = validateCodeMsg;public void validateUserName(ActionEvent event)System.out.println(input name= + this.userName);if(!userName.match
10、es(w6,20)this.validUserMsg = 用户名不合法!;public void paint(OutputStream out, Object data) throws IOException if (data instanceof ImageData) ImageData imageData = (ImageData) data; / 生成一个在10009999之间的随机数 Random randomNumber = new Random(); String keyCode = randomNumber.nextInt(8999) + 1000 + ; /把产生的随机数保存到
11、session中 HttpSession session = (HttpSession)FacesContext.getCurrentInstance() .getExternalContext().getSession(true); session.setAttribute(keyCode, keyCode); /生成干扰线的随机数 int outPutLine = 0; outPutLine = randomNumber.nextInt(100); BufferedImage img = new BufferedImage(imageData.getWidth(), imageData.g
12、etHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g = img.createGraphics(); g.setBackground(imageData.getBackground(); g.setColor(imageData.getDrawColor(); g.setFont(imageData.getTextFont(); /画矩形 g.clearRect(0, 0, imageData.getWidth(), imageData.getHeight(); /画干扰线 g.drawLine(outPutLine, outPutLine
13、, imageData.getWidth() - outPutLine, imageData.getHeight() - outPutLine); /画产生的随机数 g.drawString(keyCode, 10, 16); g.dispose(); ImageIO.write(img, jpeg, out); public String login()System.out.println(user-code + this.getCode();/从session中取出验证码HttpSession session = (HttpSession)FacesContext.getCurrentIn
14、stance().getExternalContext().getSession(true);String keyCode = (String)session.getAttribute(keyCode);if(keyCode.equals(this.getCode()return success;elsethis.validateCodeMsg = 验证码不正确;return null;public void isHaveName(ActionEvent event)System.out.println(input myname = + this.myname);if(liuyan.equal
15、sIgnoreCase(myname)this.validUserMsg = 用户重复!;在此用到了一个辅助类/* * ClassName: ImageData.java * created on Nov 28, 2007 * Copyrights 2007-2008 qjyong All rights reserved. * EMail: */package ajax;import java.awt.Color;import java.awt.Font;/* * author qiujy * version 1.0 * */public class ImageData implements
16、java.io.Serializable private int width = 60;private int height = 20;private Color background = new Color(0 xDCDCDC);private Color drawColor = Color.black;private Font textFont = new Font(Times New Roman, Font.PLAIN, 18);/* * return the width */public int getWidth() return width;/* * param width the
17、width to set */public void setWidth(int width) this.width = width;/* * return the height */public int getHeight() return height;/* * param height the height to set */public void setHeight(int height) this.height = height;/* * return the background */public Color getBackground() return background;/*
18、* param background the background to set */public void setBackground(Color background) this.background = background;/* * return the drawColor */public Color getDrawColor() return drawColor;/* * param drawColor the drawColor to set */public void setDrawColor(Color drawColor) this.drawColor = drawColor;/* * return the textFont */public Font getTextFont() return textFont;/* * param textFont the textFont to set */public void setTextFont(Font textFont) this.textFont = textFont;这是用于图形显示的。显示效果如下输入不正确的信息,鼠标焦点
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 深度解析(2026)《FZT 63002-2009粘胶长丝绣花线》:技术规范、行业应用与未来发展趋势全面透视
- 统编人教版六年级语文下册《骑鹅旅行记(节选)》示范课教学课件
- 深度解析(2026)《FZT 10020-2020纺织经纱上浆用聚丙烯酸类浆料试验方法 粘度测定》
- 智信中科研究网2025-2031 年度中国无人机巢市场发展方向研究报告
- 2026年上海市闸北区社区工作者招聘笔试参考题库及答案解析
- 2026年宿迁市宿城区社区工作者招聘考试参考试题及答案解析
- 2026年辽阳市宏伟区社区工作者招聘笔试模拟试题及答案解析
- 教学实验 3 利用高级计算器进行复数的运算教学设计中职基础课-职业模块 工科类-高教版-(数学)-51
- 2026年和田地区和田市社区工作者招聘考试参考题库及答案解析
- 2026年云南省丽江市社区工作者招聘考试备考题库及答案解析
- 浙江大学“六高强校”战略
- 2025年中国联通太原市分公司招聘笔试参考题库含答案解析
- 手卫生课件(完整版)
- 中国科技大学课件系列:《生物信息学》01
- 聚合性痤疮的临床特征
- 【经典文献】《矛盾论》全文
- GB/T 44120-2024智慧城市公众信息终端服务指南
- DZ/T 0430-2023 固体矿产资源储量核实报告编写规范(正式版)
- 档案盒(文件盒)标签模板(正面、侧面)
- 土压平衡盾构土仓压力设定与控制课件
- 金蝶K3供应链-课件
评论
0/150
提交评论