Take the 2-minute tour ×

This question already has an answer here:

I have this bean in application scope.

public class User {
    private UICommand link;
    private String name;
    public User(){
        name = "Test Link";

    public UICommand getLink() {
        System.out.println(link==null?"link is null":"link is not null");
        return link;
    public void setLink(UICommand link) {
        this.link = link;
        System.out.println("link: "+link.toString());
    public void change(){
    //setter and getter for name

I have this jsf on jsp page.

<h:commandLink binding="#{user.link}" action="#{user.change}" value="#{user.name}"/>

I thought that the UICommand object would be reused (by sending the serialized state of the object along with the HTML output) and thus maintain the state and binding. But I get this sysoutput.

//When page loads
link is null
link: javax.faces.component.html.HtmlCommandLink@14e4ce7

//when user clicks the link 
link: javax.faces.component.html.HtmlCommandLink@6fcc9c

UICommand object is different each time the user clicks the link!!! Also i believe getLink() runs only once when that object is first loaded on page but if that's the case then the page woudn't reflect the latest UICommand object!

share|improve this question

marked as duplicate by BalusC, 宮本 武蔵, Christoph, CanSpice, Alex Feb 28 '13 at 18:23


Do you really need binding? In my humble opinion it is not a good idea to use bindings and values at the same time. –  András Tóth Feb 23 '13 at 7:48
it's just for learning purpose –  John Feb 23 '13 at 8:25
add comment

1 Answer

No, each time the component tree is built/restored, you get completely new instances of UICommand. But these instances restore their state from the JSF state saving mechanism.

But you shouldn't use bindings intensively. There is almost never a good reason to do so. If you do so, always use request scope for the bean, because you will run into problems otherwise.

share|improve this answer
how does it maintain state if it create new instances of UICommand? –  John Feb 23 '13 at 8:51
They implement an interface called StateHolder. The interface contains saveState() and restoreState() methods. See: docs.oracle.com/javaee/6/api/javax/faces/component/… –  chkal Feb 23 '13 at 9:51
add comment
