It's not a secret anymore!
The moose likes JSF and the fly likes Problems getting Ajax to work properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Problems getting Ajax to work properly" Watch "Problems getting Ajax to work properly" New topic

Problems getting Ajax to work properly

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155

I am having a problem getting Ajax to work correctly. In the xhtml file I have here I am using Ajax to render some input being enabled or disabled, and it works correctly. However, further down I am also using Ajax to render an included xhtml file which contains a <h:selectOneMen/> amongst other controls, and is tagged with id="photoEnabled". When the checkbox is not checked, a dummy display tagged by id="photoDisabled" is show in its place simply for a tidy presentation on the page, and is disabled.

For some reason this does not work, and I do not understand why. However, if I replace:

it does work correctly, but the problem is that further up on the page and not shown here, I have some other input fields, and when the status of the checkbox is changed, the fields are cleared because the whole form is rendered, which I do not want.

The relevant xhtml code is here, with the two Ajax tags shown, one which works and the other which does not.

In my Java class I have various getters and setters, which all work correctly, include those associated with the checkboxes, and dummy Ajax method which does nothing but appears to be required.

Part of the code is here:

I am having a similar problem with some checkboxes earlier in the listing, and for them to work I have to use "@form" rather than specific ids, so again input fields are cleared when a checkbox is clicked.

Can someone kindly help me with the strange situation which I do not understand, and should some code be placed in the updateCheck() method in my bean?

Many thanks and I look forward to an interesting reply.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Although I haven't been able to work extensively with the f:ajax tag, my experience with the RichFaces equivalent tells me that these things can be touchy sometimes. One thing you might try is to qualify the render targets like so: "@form.photoEnabled".

Reading the generated HTML can't hurt, although that sort of stuff tends to render rather cryptically.

An IDE is no substitute for an Intelligent Developer.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
Many thanks, and in the mean time the problem seems to have been resolved. The problems seems to have been that ui:fragment does not generate HTML, and I associated an id with a component, the same ui"fragment tag, that may not be rendered. So I kept the Ajax code as previously, namely:

then changed the code that does the rendering to this:

which now appears to work correctly, and does not affect the rest of the form.

The part above the comment is rendered when the check box is checked, and the part below is rendered as a disabled component when the check box is not checked. I am doing this as it looks better than making the menu appear when the check box is checked.
I agree. Here's the link:
subject: Problems getting Ajax to work properly
It's not a secret anymore!