Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSF中的绑定对象初始化_Java_Jsf - Fatal编程技术网

Java JSF中的绑定对象初始化

Java JSF中的绑定对象初始化,java,jsf,Java,Jsf,我在应用范围内有这个bean public class User { private UICommand link; private String name; public User(){ System.out.println("User.User()"); name = "Test Link"; } public UICommand getLink() { System.out.println("User

我在应用范围内有这个bean

public class User {
    private UICommand link;
    private String name;
    public User(){
        System.out.println("User.User()");
        name = "Test Link";
    }

    public UICommand getLink() {
        System.out.println("User.getLink()");
        System.out.println(link==null?"link is null":"link is not null");
        return link;
    }
    public void setLink(UICommand link) {
        System.out.println("User.setLink()");
        this.link = link;
        System.out.println("link: "+link.toString());
    }
    public void change(){
        System.out.println("User.change()");
    }
    //setter and getter for name
}
我在jsp页面上有这个jsf

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

UICommand
每次用户单击链接时,对象都是不同的!!!另外,我相信
getLink()
在该对象第一次加载到页面上时只运行一次,但如果是这种情况,那么页面将不会反映最新的UICommand对象

否,每次构建/恢复组件树时,都会得到UICommand的全新实例。但是这些实例从JSF状态保存机制恢复它们的状态


但是您不应该过度使用绑定。几乎没有一个好的理由这样做。如果这样做,请始终使用bean的请求范围,否则会遇到问题。

您真的需要绑定吗?在我看来,同时使用绑定和值不是一个好主意。这只是为了学习,如果创建UICommand的新实例,它如何维护状态?它们实现了一个称为StateHolder的接口。该接口包含saveState()和restoreState()方法。见:
//When page loads
User.User()
User.getLink()
link is null
User.setLink()
link: javax.faces.component.html.HtmlCommandLink@14e4ce7

//when user clicks the link 
User.setLink()
link: javax.faces.component.html.HtmlCommandLink@6fcc9c
User.change()