You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Matthew Young <gi...@gmail.com> on 2008/03/26 00:55:10 UTC

How to Ajax refresh captcha image (a image backed by DynamicImageResource)?

I want to be able to show a new captcha image via an AjaxFallbackLink.  But
I don't know what to do in onClick to make the captcha img tag reload.

public MyPage extends WebPage {

     private CaptchaImageResouce captchaImageResource;

     public MyPage() {

        add(new Image("captchaImg", new PropertyModel(this,
"captchaImageResource")) {
            private static final long serialVersionUID = 1L;
            @Override protected void onBeforeRender() {
                regenerateCaptcha();            // make a new image
                super.onBeforeRender();
            }
        }.setOutputMarkupId(true));

       add(new AjaxFallbackLink("link") {
            @Override public void onClick(AjaxRequestTarget target) {

                // WHAT DO I DO HERE? Is there some javascript to make the
img tag reload?
                if (target != null) {
                    target.addComonent(MyPage.this.get("captchaImg"));   //
This is not enough to make it show
                }
            }

        }
    }


    public void regenerateCaptcha() {
        captchaResource = new CaptchaImageResource(randomString(3, 5));
    }
}

Re: How to Ajax refresh captcha image (a image backed by DynamicImageResource)?

Posted by praveenhomkar <pr...@yahoo.com>.
Hi I am able to reload the captcha image in the following way(Using JCaptcha)

package com.sybase365.mobiliser.web.consumer.pages.signup;
1>Write a class for dynamic image as follows.

public abstract class CaptchaImage extends NonCachingImage {
    public CaptchaImage(String id, final String challengeId) {
	super(id);
	setImageResource(new DynamicImageResource() {
	    protected byte[] getImageData() {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		String challengeId = String.valueOf(System.currentTimeMillis());
		BufferedImage challenge = getImageCaptchaService()
			.getImageChallengeForID(challengeId,
				Session.get().getLocale());
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
		try {
		    encoder.encode(challenge);
		    return os.toByteArray();
		} catch (Exception e) {
		    throw new RuntimeException(e);
		}
	    }	   	});    }
    protected abstract ImageCaptchaService getImageCaptchaService();
}

2> add it to form and provide a link to refresh it

img = generateCaptcha();// see this code in 3rd step
	form.add(img);

	form.add(new AjaxLink("captchLink") {
	    @Override
	    public void onClick(AjaxRequestTarget target) {
		// counter++;

		// target.addComponent(generateCaptcha());
		target.addComponent(form.replace(generateCaptcha()));

	    }
	});

3>public NonCachingImage generateCaptcha() {
	String challengeId = String.valueOf(System.currentTimeMillis());
	img = new CaptchaImage("captchaImage", challengeId) {
	    @Override
	    protected ImageCaptchaService getImageCaptchaService() {
		return captchaService;
	    }
	};
	img.setOutputMarkupId(true);
	return img;

    }
4> html
<p> </p>
				 # click me 


5> Don't forget to add the following in application-context.xml if u r using
spring injection

<bean
class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService"
id="imageCaptchaService"/>

For any queries mail me praveenhomkar@yahoo.com

Thanks
Praveen Homkar




--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-to-Ajax-refresh-captcha-image-a-image-backed-by-DynamicImageResource-tp1858686p3744243.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to Ajax refresh captcha image (a image backed by DynamicImageResource)?

Posted by Martijn Dashorst <ma...@gmail.com>.
s/image/noncachingimage/

On 3/26/08, Matthew Young <gi...@gmail.com> wrote:
> I want to be able to show a new captcha image via an AjaxFallbackLink.  But
>  I don't know what to do in onClick to make the captcha img tag reload.
>
>  public MyPage extends WebPage {
>
>      private CaptchaImageResouce captchaImageResource;
>
>      public MyPage() {
>
>         add(new Image("captchaImg", new PropertyModel(this,
>  "captchaImageResource")) {
>             private static final long serialVersionUID = 1L;
>             @Override protected void onBeforeRender() {
>                 regenerateCaptcha();            // make a new image
>                 super.onBeforeRender();
>             }
>         }.setOutputMarkupId(true));
>
>        add(new AjaxFallbackLink("link") {
>             @Override public void onClick(AjaxRequestTarget target) {
>
>                 // WHAT DO I DO HERE? Is there some javascript to make the
>  img tag reload?
>                 if (target != null) {
>                     target.addComonent(MyPage.this.get("captchaImg"));   //
>  This is not enough to make it show
>                 }
>             }
>
>         }
>     }
>
>
>     public void regenerateCaptcha() {
>         captchaResource = new CaptchaImageResource(randomString(3, 5));
>     }
>  }
>


-- 
Buy Wicket in Action: http://manning.com/dashorst
Apache Wicket 1.3.2 is released
Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.2

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org