Wednesday, October 10, 2007

How to Rerender 'Mandatory' InputText with A4J

I was working on A4J (Ajax for JSF) for a week now. Just when I'm about to like A4J (Ajax for JSF), I hit a dead end with a bug. Initially, when I started writing this article, it was intended as a Bug report. But while I was preparing the snapshots, kachiingg..!!, my email received a notification from JBoss forum. Anxiously, I clicked on the URL in the mail, after several minutes of waiting (yes, JBoss, you guys need to do something with your website performance), I've received a solution from 'SergeySmirnov'. Russian I presumed. So from Bug report, this article turned to a solution. That's a miracle of the Internet. OK, back to my problem.

Supposely, A4J makes it simple for a developer to apply Ajax capabilities on their current JSF applications. And I admit, it is. After a week trying on A4J, I've been able to make cool Ajax call between my client page and server.

Here is the problem. I have a simple form, where I display a name in two format. One with an <h:inputtext> tag and another one in the <h:outputtext> tag.



Very simple. Below is the code for each object on the page.


Input field code.


Text output code.


Reset button code.

As a conventional JSF way, I'd put 'immediate' attribute equals to 'true' for my <a4j:commandbutton>, in order to by pass the validation since the <h:inputtext> 'required' attribute is 'true'. The thing is, when I clicked on 'Reset' it only 'rerenders' the text output. Input field value, remains. Hmm. That's not right. It should clears off both, input and output objects.


Thanks to 'SergeySmirnov' from JBoss.com forum, he gaves a solution. Instead of using 'immediate' attribute to 'true', I should use <a4j:region> to surround my reset code like below.


As you can guess by now, it works. My reset buttons clears off both objects. By surrounding my reset button with <a4j:region>, it will only submit this particular region as an Ajax request and omits the others.


Yeah... A4J rules!

4 comments:

Rui Santos said...

Man, I was looking a lot for this solution, and it easily works! Congratulations!

Mariana said...

What I needed too! Thanx!

Unknown said...

Wohoo, that saved my ass from rewriting my whole app. thanks!!

Williams said...

Thanks, it was what I needed