JSF onclick方法调用两次(不点击)

时间:2014-02-06 18:32:52

标签: jsf primefaces

这里只是一个带onclick方法的命令链接:

<h:form>
    <h:panelGroup layout="block" id="ya">
        <p:commandLink value="Current value is: #{newClass.counter}" onclick="#{newClass.go()}" update="ya"/>    
    </h:panelGroup>
</h:form>

简单的豆子:

@ManagedBean
@SessionScoped
public class NewClass implements Serializable{

    private int counter;

    public void go(){
        counter++;
        System.out.println("!method go() is called: "+counter);
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}

这是我在服务器日志中所做的:

    .......
    INFO:   Initializing Mojarra 2.2.0 ( 20130502-2118 https://svn.java.net/svn/mojarra~svn/tags/2.2.0@11930) for context '/WebSite'
    INFO:   Running on PrimeFaces 4.0
    INFO:   Loading application [WebSite] at [/WebSite]
    INFO:   WebSite was successfully deployed in 1 654 milliseconds.
    INFO:   !method go() is called: 1
    INFO:   !method go() is called: 2

为什么方法只在页面加载时调用两次? 为什么点击commandLink也会调用两次metod?

1 个答案:

答案 0 :(得分:3)

使用commandButton的actionListener属性而不是onclick