Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How best to implement h:outputlink click counter and save value to database?  RSS feed

 
Victor Ade
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm trying to get the total number of clicks  on a link in my webapp.

How do I implement this considering houtputlink don't have action attribute and calling a backing bean method with <p:remote command name="me"/> which will be triggered thus: <h:outputlink onclick="me()"/> wouldn't do justice to when user of the link right click on it to choose context menu I.e (open in new tab, new window, new incognito window etc).
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
outputLink generates a raw HTML link tag (<a href="....">). You can initiate a javascript action and still jump to the link indicated, but you'll have to ask over in the JavaScript forum about that. To update a database, you'll have to perform an AJAX call before jumping.

Use outputLink when you must jump to a location external to your webapp or to an internal non-JSF URL. Otherwise use commandLink, which, of course, binds to a backend action and thus makes the update trivial.
 
Victor Ade
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

like you suggested tim, to implement this i try to send the  clicked url  through ajax to a servlet that Updates the database. but i guess i'm still missing something as the code woudnt work.
here's what im doing thats not working. i didnt download jquery from cdn cause my project is predominantly primefaces based. adding jquery from cdn make my app behave abnormally as i have witnessed it. long story short, here's my code:



Here's what my h:outpulink looks like




and the servelet i'm trying to do database action in;

 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jquery is already part of PrimeFaces. The only restriction there is that Primefaces might be using an older version of jQuery, in which case there's a little tweaking that you have to do to make everything work with the same jQuery version.

Note that the "$" notation for jQuery will really annoy the EL expression processor, which thinks that "$" heralds a variable read operation. use the "jQuery" form instead:



If you are using JSF version 2 - and by now you should be, the "f:ajax" element under the h:outputLink would be much cleaner and you could make your database operation be an AJAX action method in a backing bean.

Regardless, if you attach JavaScript to a link-click action, remember that you are responsible for making the jump yourself - the Javascript event overrides the builtin jump. So use one of the navigational assists such as "location = link" as part of your post-script or after-AJAX commands.
 
Victor Ade
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks for your time Tim. I'm on PrimeFaces 6.0, Jsf 2.2. The last time I checked I think we have now jsf 2.3 and PF 6.7? which is yet to have a community version.
Away from that really, I'd like to know what you mean by

Tim Holloway wrote:
Regardless, if you attach JavaScript to a link-click action, remember that you are responsible for making the jump yourself - the Javascript event overrides the builtin jump. So use one of the navigational assists such as "location = link" as part of your post-script or after-AJAX commands.


Do you mean that I'll be the one to 'direct' the link to for example open in a new browser tab? Would target="_blank "  not work because of JavaScript onclick event?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!