Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

commandLink does not invoke action listener, but commandButton works fine

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?

like image 494
Arka Bhaduri Avatar asked Mar 14 '13 10:03

Arka Bhaduri


People also ask

Why is my action listener not working when using commandlink?

Sometimes, when using <h:commandLink>, <h:commandButton> or <f:ajax>, the action, actionListener or listener method associated with the tag are simply not being invoked. Or, the bean properties are not updated with submitted UIInput values.

Why is my H command link not working?

If your h:commandLink is inside a h:dataTable there is another reason why the h:commandLink might not work: The underlying data-source which is bound to the h:dataTable must also be available in the second JSF-Lifecycle that is triggered when the link is clicked.

Why doesn't the command button work on first click?

This is caused by a bug in view state handling which is reported as JSF spec issue 790 and currently fixed in JSF 2.3. For older JSF versions, you need to explicitly specify the ID of the <h:form> in the render of the <f:ajax>. See also h:commandButton/h:commandLink does not work on first click, works only on second click.


1 Answers

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).

like image 101
BalusC Avatar answered Sep 18 '22 18:09

BalusC