commandButton actionListener
Hi,
I've been searching on how I can add an actionListener to my commandButton. But can't figure it out.
So what I wanna do is very simple I guess. It's just counting how many times a commandButton has been clicked.
Anyone can give a good solution how to do this?
Thanks
[304 byte] By [
eLIXa] at [2007-11-27 10:56:35]

# 2
Have you tried the actionListener attribute of the commandButton component? E.g.
<h:commandButton value="Go" actionListener="#{myBean.go}" />
where MyBean is something like
public class MyBean
{
private int count = 0;
public void go(ActionEvent event)
{
count++;
}
}
I'm assuming this is just a toy application to get things going; if this is for production you may need some synchronization.
BalusC beat me to it I see.
# 4
Seems I'm getting an error when I click the button now.
Here's what I have:
<h:commandButton value="Volgende Vraag" action="#{EnqueteMBean.saveQa}" actionListener="#{EnqueteMBean.countClicks}" />
public class EnqueteMBean {
private int tellerClicks = 1;
public void countClicks(ActionEvent event) {
tellerClicks++;
System.out.println("Vraag: " + tellerClicks);
}
Error:
javax.servlet.ServletException: Exception while invoking expression #{EnqueteMBean.countClicks}
javax.faces.webapp.FacesServlet.service(FacesServlet.java:152)
org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
What could be the problem here?
Message was edited by:
eLIX
Message was edited by:
eLIX
eLIXa at 2007-7-29 12:03:54 >

# 5
> I'd rather to make the 'count' variabele static or at
> least make the bean session scoped. If this bean is
> request scoped, then the value of count will never be
> higher than 1.
Well, he didn't indicate what scope he wanted to count the button clicks on. I would rather make the bean application scope than use a static variable, it will be easier to synchronize; assuming he wants to count clicks app wide. Otherwise it should probably be session scoped.
# 6
Nevermind.
I tought it was impossible to count in my action method first. But now I see it is possible so the actionListener won't be needed anymore.
Still handy to know how I can implement an actionListener.
So thnx!
eLIXa at 2007-7-29 12:03:54 >
