0

Here is just one command link with onclick method:

<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>

And simple bean:

@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;
    }
}

Here what i heve in the server log:

    .......
    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

Why the method calling two times just on page loading ? And why by clicking on commandLink the metod calling twice too ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vololodymyr
  • 1,996
  • 5
  • 26
  • 45
  • 3
    I can't explain the double call based on information provided so far, but I can tell that the `onclick` attribute isn't intented to invoke a bean action. It's intented to represent some JavaScript code which is to be executed during the HTML DOM `click` event. You need `action` attribute instead. Exactly like as shown in every sane JSF book/tutorial/resource. Or, if yours really shows code like that, throw it far away. To be on the safe side, I'd also take a JSF pause and learn some basic HTML/JS first. It appears that you had no idea that JSF is basically a HTML/JS code generator. – BalusC Feb 06 '14 at 19:01
  • @BalusC, action attribute is working correctly. Thank you for the unswer. I feel so dumb :( I've learned HTML and basic JS, but it seems it not enough. – Vololodymyr Feb 06 '14 at 20:30

1 Answers1

3

use an the actionListener attribute of the commandButton instead of onclick

dan
  • 91
  • 5
  • Why is the method called two times just on page loading instead of one time as expected? Why are you recommending `actionListener` instead of `action`? – BalusC Feb 06 '14 at 19:03
  • I recoment `actionListener` instead of `action` becuase the function returns void. `action` use be used to return a String which can be used for navigation. – dan Feb 06 '14 at 19:59
  • As to why it is being called twice, I am with you on that aspect. It appears that it might not be determinable from the info provided. There is probably something else going on. – dan Feb 06 '14 at 20:00
  • So you're implying that `action` can't return `void`? This is wrong. See also http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener/ – BalusC Feb 06 '14 at 20:15
  • Im not implying that, it was a recommendation to use `actionListener` instead of `action`. That implies that both could be used. `action` most certainly can return void. – dan Feb 06 '14 at 20:47
  • Then why exactly `actionListener` instead of `action`? Did you click and read that link? :) – BalusC Feb 06 '14 at 20:51