[Ajax4JSF] 求助个ajax4jsf做自刷新验证码的问题

qiujy 2007-11-29
我用ajax4jsf做了一个自刷新验证码的例子,在第一次点击验证码能重新产生一个新的图片,但点第二次时去不行了,不知道是不是跟我们自己封装ajax的函数一样,请求链接要跟个随机数才行,但这个ajax4jsf不知道怎么加呀。不知道怎么解决?哪位能告知。。
这个是页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>

<html>
<head>
<title>用户登录页面</title>
</head>
 
<body>
<f:view>
<a4j:region id="a4j_2">
<h2>用户登录页面</h2>
<h:form id="loginForm">
<h:panelGrid columns="3">
<h:outputLabel value="用户名"></h:outputLabel>

<h:inputText id="txtUserName" value="#{UserBean.userName}">
<a4j:support actionListener="#{UserBean.validateUserName}"
event="onblur" reRender="result"></a4j:support>
</h:inputText>

<h:panelGroup>
<h:message for="txtUserName"></h:message>
<h:outputText value="#{UserBean.validUserMsg}"
id="result" style="color:red"/>
</h:panelGroup>

<h:outputLabel value="密码"></h:outputLabel>
<h:inputText id="txtPwd" value="#{UserBean.password}"></h:inputText>
<h:message for="txtPwd"></h:message>

<h:outputLabel value="验证码"></h:outputLabel>
<h:panelGroup>
<h:inputText id="txtCode" value="#{UserBean.code}" size="10"/>
<a4j:outputPanel id="detail_media">
      <a4j:commandLink reRender="detail_media">
        <a4j:mediaOutput element="img" cacheable="false"  
            session="false" createContent="#{UserBean.paint}"  
            value="#{imageData}" mimeType="image/jpeg" border="0"/>
    </a4j:commandLink>
</a4j:outputPanel>
</h:panelGroup>
<h:outputText value="#{UserBean.validateCodeMsg}" style="color:red"/>
</h:panelGrid>
<h:commandButton action="#{UserBean.login}" value="提交"/>
</h:form>
</a4j:region>
</f:view>
</body>
</html>

这个是后台Bean:

package org.qiujy.web.controller;

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.FacesContext;
import javax.faces.event.ActionEvent;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;

import org.qiujy.domain.ImageData;


public class UserBean {
private String userName;
private String password;
private String code; //用户输入的验证码

private String validUserMsg;
private String validateCodeMsg;

/**
* @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
*/
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
*/
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 getValidateCodeMsg() {
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.matches("\\w{6,20}")){
this.validUserMsg = "用户名不合法!";
}
}

public void paint(OutputStream out, Object data) throws IOException {
    if (data instanceof ImageData) {   
    ImageData imageData = (ImageData) data;   
        // 生成一个在1000-9999之间的随机数
    Random randomNumber = new Random();
        String keyCode = randomNumber.nextInt(8999) + 1000 + "";
        //把产生的随机数保存到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.getHeight(), 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, 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.getCurrentInstance()
.getExternalContext().getSession(true);
String keyCode = (String)session.getAttribute("keyCode");

if(keyCode.equals(this.getCode())){
return "success";
}else{
this.validateCodeMsg = "验证码不正确";
return null;
}
}

}
这个是图片信息bean:
package org.qiujy.domain;

import java.awt.Color;
import java.awt.Font;


public class ImageData implements java.io.Serializable {

private int width = 60;

private int height = 20;

private Color background = new Color(0xDCDCDC);

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 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;
}

/**
* @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;
}
}
qiujy 2007-12-18
自已解决了,原来是我用的ajax4jsf是1.1.1,它不支持IE7和FireFox2.0,唉.看来JSF方面的高手还是很难找的哦.
spc 2008-09-08
我也做了个差不多的,但一次也刷不了,你为什么要用一个outputPanel?
spc 2008-09-08
我加上了照样刷不了...
maxwell 2008-09-10
<a4j:outputPanel ajaxRerender="true">
</>

让他自动刷新而不是根据ID刷新
试试看
rockjava 2008-09-11
用servlet或是jsp做个验证码,然后用js控制定时请求servlet或是jsp就行了
用不a4j这么麻烦
Global site tag (gtag.js) - Google Analytics