commandLink does not invoke action listener, but commandButton works fine

Arka Bhaduri picture Arka Bhaduri · Mar 14, 2013 · Viewed 8k times · Source

I want to invoke one method through a link from Facelets:

My Facelets code is like:

<h:commandButton value="A" actionListener="#{customerData.searchedCustomerListA}" />

<h:commandLink value="A" actionListener="#{customerData.searchedCustomerListA}"/>

Backing bean code is like:

public void searchedCustomerListA(ActionEvent ae){
    customerName = "A";
    leftCustomerListAvailable.clear();
    if(customerDataBean.getSearchedCustomerList(customerName)!= null)
        leftCustomerListAvailable =customerDataBean.getSearchedCustomerList("A");
}

The same code is working for <h:commandButton> but not working for <h:commandLink>. How is this caused and how can I solve it?

Answer

BalusC picture BalusC · Mar 14, 2013

The technical difference between <h:commandLink> and <h:commandButton> is that the link uses JavaScript to submit the parent form. So if it doesn't work while a syntactically equivalent button works fine, then that can only mean that either JavaScript is disabled in browser, or that the jsf.js file containing the mandatory helper functions isn't included in the page (which you should easily have noticed by seeing JS errors in the JS console of browser's builtin developer toolset).

So, to fix this problem, you need to verify if JS is enabled in browser and that you've a <h:head> component instead of plain HTML <head> in the template, so that JSF will be able to auto-include the jsf.js file.

Or, if your application's business requirements requires that the application functions as designed with JS disabled, then you should stick to <h:commandButton> and throw in some CSS to make it to look like a link (e.g. remove background, padding, border, inset, etc).