2013-03-14 48 views
5

我想通過從Facelets的一個鏈接調用一個方法:commandLink不會調用動作監聽器,但的commandButton工作正常

我Facelets的代碼如下:

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

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

支持bean代碼如下:

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

相同的代碼工作<h:commandButton>但不工作<h:commandLink>。這是如何造成的,我該如何解決這個問題?

+0

當你點擊'commandLink'時,是否在瀏覽器的JavaScript控制檯中看到任何錯誤? – partlov 2013-03-14 10:14:19

+0

嘗試使用'action'而不是'actionListener'。 – pepuch 2013-03-14 10:26:32

+0

嗨pepuch,我也試着採取行動,但沒有得到結果。 – 2013-03-14 11:01:10

回答

0

試試這個,這可行。

<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!!!!!!!!!!!!"); 
    } 
} 
+0

嗨neni,我試着用你的解決方案。但仍然沒有得到結果。我也嘗試了actionListener而不是actionListener。但結果是一樣的。 – 2013-03-14 11:00:08

+0

JSF版本有問題嗎? – 2013-03-14 11:00:31

+0

您正在使用哪個版本的JSF?你是否使用任何組件庫,如primefaces,豐富的面孔? – neni 2013-03-14 11:05:04

3

<h:commandLink><h:commandButton>之間的技術區別在於,鏈接使用JavaScript提交父窗體。因此,如果在語法上等效的按鈕無法正常工作的情況下它不起作用,那麼這隻能意味着在瀏覽器中禁用了JavaScript,或者頁面中沒有包含強制幫助函數的jsf.js文件(您應該輕鬆通過在瀏覽器的內置開發工具集的JS控制檯中看到JS錯誤已經注意到了)。

因此,要解決這個問題,你需要驗證,如果JS在瀏覽器中啓用,你已經一個<h:head>組件,而不是普通的HTML <head>的模板,使JSF將能夠自動包括jsf.js文件。或者,如果您的應用程序的業務需求要求應用程序的功能設計爲禁用了JS,那麼您應該堅持<h:commandButton>並拋出一些CSS,使其看起來像一個鏈接(例如刪除背景,填充,邊框,插入等)。

0

我也遇到過這個問題。我發現當commandButton的形式相同時,commandLink將不起作用。當我將commandLink移動到另一個表單時,它可以像往常一樣調用動作。另一種方法是向commandLink添加immediate =「true」屬性。

<h:form> 
…… 
<div class="control-group"> 
    <div class="controls"> 
     <h:commandButton value="Login" action="#{usrCtrl.login}" class="btn btn-primary"/> 
     <h:commandLink action="#{usrCtrl.register}" value="test" immediate="true"/> 
    </div> 
</div> 
</h:form> 

閱讀JSF and the 「immediate」 Attribute – Command Components看看它爲什麼起作用。