0

In my XHTML page I'm calling a method from my @ManagedBean class. The method should be executed only when I click the link which to is linked the method:

  <h:link value="Continue Reading &raquo;" 
       outcome="contracts/resources/data/imag2_facebook.html"  
onclick="#{sessionFilter.incForLink2('contracts/resources/imagini/facebook.jpg','Facebook down? Current problems and status', 'contracts/resources/imagini/facebook.jpg')}" />

It's weird why the method is executed at every page refresh without clicking on the link. I want the method to execute only when I click on the link. I have also tried with h:commandLink but the results are the same. Do you have an idea what I need to change, or what is wrong? The @ManagedBean class is application scoped. The method I call is the following:

public void incForLink2(String link, String title, String imgAllLink) {
    Article article = links.get(link);
    if (article != null) {
        Integer pageHits = article.getPageHits();
        article.setPageHits(pageHits + 1);
        System.out.println(link + " = " + pageHits);
    } else {
        Article article1 = new Article(1, title, link, imgAllLink);
        links.put(link, article1);
    }
}
  • I have to find the duplicate, but it is because if you include EL in anything other than an action method it is called on page load to render the html. An onclick is not an action method in the jsf sense, (it kind of is in the html context) – Kukeltje Jun 12 '16 at 17:49
  • Possible duplicate of [JSF page onload executes button click](http://stackoverflow.com/questions/25704443/jsf-page-onload-executes-button-click) – Kukeltje Jun 12 '16 at 17:50
  • Thanks for the answer @Kukeltje , I'll try to change and come back if it works with h:commandButton – Flaviu Bodea Jun 12 '16 at 17:57
  • 1
    Or `commandLink` and http://stackoverflow.com/questions/28623664/can-not-invoke-bean-action-method-in-onclick-attribute-of-houtputlink is also related – Kukeltje Jun 12 '16 at 17:59
  • I've tried with h:commandButton and with h:commandLink. Still not working. I refresh the page and the method is automatically called. It's so weird. – Flaviu Bodea Jun 12 '16 at 18:12
  • Then you have something else in the page that has the same problem. OR... did you still use the 'onclick'? Then read the links I referred to carefully... onclick is for javascript, not method calls (as mentioned in both the links I posted) – Kukeltje Jun 12 '16 at 18:26

1 Answers1

-1

onClick is a handler for JavaScript in the webbrowser! The Method you call is Java and executed on the server!

Java renders the Website before the website is sent to the webbrowser, in this phase is the method is invoked. After the sending to the webbrowser the javascript can be executed, this is a second phase. You simply need a new request to the server for executing java-methods. I prefer to use a4j:jsFunction.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • 1
    That is exactly what is stated in the duplicates I referred to or did I miss something? And `h:link` was introduced in JSF 2, so an `f:ajax` could be used to instead of the a4j that pre-dates it. And afaics there is no need to use an ajax call here. A `commandLink` would be sufficient since the OP wants to do page navigation. – Kukeltje Jun 12 '16 at 19:42
  • @Kukeltje You commented 2 possible duplicates that makes it wague for me to vote for close by duplicate. – Grim Jun 12 '16 at 21:14
  • @Kukeltje it's working with h:commandLink, I also needed to change the way I made the page forward. Now I'm doing the page forward from my Java class and I call the method from h:commandLink with the action attribute. Thanks for the answer! – Flaviu Bodea Jun 13 '16 at 10:18
  • @FlaviouBodea: please don't accept answers like this. The duplicates are there. The answer about the a4j stuff is even superceeded by f:ajax, the stuff about the additional request is plain out wrong. People should be stimulated to vote for duplicates, not encouraged to gain reputation by answering these in a less good way. – Kukeltje Jun 13 '16 at 22:47
  • @Kukeltje I did not suggest a additional request for additional execution of the method: I suggest a new request for the execution of the method not the original request for the method. Also a duplicate not yet has been found, even if I also think there is a matching answers that should be preferred. To vote-down a answer has a their own meaning and should not be used to lead the OP to find duplicates. Also, why do you think the OP like to make page-navigation? – Grim Jun 14 '16 at 01:32