2014-10-27 81 views
0

我在做一個JSF項目,遇到以下問題。JSF/Primefaces span onClick自動調用Backing Bean

當我創建以下範圍:

<ui:composition> 
    <div id="header" class="header"> 
     <p style="float: right; padding-right: 20px"> 
      Welcome, #{infoGet.username} <span class="glyphicon glyphicon-off" style="color: darkred; cursor: pointer;" onclick="#{Login.logout()}" /> 
     </p> 
    </div> 
</ui:composition> 

它會調用這個方法退出()

public void logout() { 
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 
    try { 
     FacesContext.getCurrentInstance().getExternalContext().redirect("Landing.xhtml"); 
    } catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 

但是頁面上加載的onClick內的跨度會導致自動調用,如果我這樣做:

public void logout() { 
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 
} 

並刷新頁面會話將失效。

有沒有辦法從跨度調用「onClick」方法?我確實需要它是一個標籤,所以我可以正確使用Bootstrap圖標元素。我知道onClick通常用於Javascript,但它似乎符合JSF的邏輯。

編輯與解決方案通過@ luiggi門多薩

改組成以:

<ui:composition> 
    <div id="header" class="header"> 
     <h:form style="float: right; padding-right: 20px"> 
      <h:commandLink action="#{Login.logout()}" styleClass="clearCommand"> 
       <span class="glyphicon glyphicon-off" style="color: darkred; cursor: pointer;" /> 
      </h:commandLink> 
     </h:form> 
     <p style="float: right; padding-right: 20px"> 
      Welcome, #{infoGet.username} 
     </p> 
    </div> 
</ui:composition> 

製造clearCommand:

#clearCommand { 
    cursor: pointer; 
} 

左登錄,因爲它是,一切現在工作了。

回答

1

您不應將任何表達式語言直接調用到非JSF組件中。什麼你要找的是一個<h:commandLink>代替:

<h:form> 
    <h:commandLink action="#{Login.logout()}" styleClass="foo"> 
     <span style="...">logout</span> 
    </h:commandLink> 
</h:form> 

哪裏foo是你清除一個<a>默認格式CSS類。然後,您可以使用常見的HTML <span>組件將所需的CSS應用於註銷文本。