Arka Bhaduri
Arka Bhaduri

Reputation: 91

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?

Upvotes: 9

Views: 8567

Answers (3)

molholm
molholm

Reputation: 2032

I my case the cause of this issue was a poorly configured url rewriting filter. One of the filters patterns unintentionally matched http://localhost:8080/mysite/javax.faces.resource/jsf.js.xhtml?ln=javax.faces which prevented jsf.js from being loaded. Check this answer: Clicking h:commandLink causes Uncaught ReferenceError: mojarra is not defined.

Upvotes: 0

BalusC
BalusC

Reputation: 1108722

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

Upvotes: 3

neni
neni

Reputation: 813

Try this, This sould work.

    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:form>

    <h:commandLink type="button" action="#{testBean.tsetLink}"> 
          <h:outputText value="A" />
    </h:commandLink>

</h:form>
</html>

ManagedBean

@ManagedBean
@RequestScoped
public class TestBean {

    public void tsetLink(){
    System.out.println("Link clicked!!!!!!!!!!!!");
    }
} 

Upvotes: 0

Related Questions