0

first of all, I am very new to JSF and web development.

What I want to do is to print a message in the console if the user click an outputlabel.

{businessBean.select()} gets called only on page refresh and not when I click.

<h:outputLabel styleClass="cat" id ="businesstasks" onclick="setColor('businesstasks'); #{businessBean.select()}">
                 Business and productivity                      
            </h:outputLabel>

My test bean:

@ManagedBean(name="businessBean")
@SessionScoped
public class businessBean {
    public void select(){
        System.out.print("Test");
    }
}

I get "Test" only when I refresh the page but I want to get it on every click.

Thanks for helping!

Community
  • 1
  • 1
JoeBl
  • 51
  • 2

1 Answers1

1

Is there a reason you 'need' the outputLabel to execute an action? Because onclick will execute it as a javascript, and that interaction can cause issues if you're calling the bean action method. You could use <h:commandLink action="#{businessBean.select}" /> within your output label.

Ognjen Mišić
  • 1,219
  • 17
  • 37
  • I think you mean the onclick attribute itself will get executed as javascript. You cannot execute any bean method via a javascript event (at least not using expression language like this). Any EL method call like this in this xhtml will get executed on initial page load, it is more often used when the method returns a string so that the result gets output into the resulting HTML. – kingdc Jul 29 '17 at 19:41