2016-04-15 46 views
-1

我一直在閱讀和搜索具有類似問題的許多頁面,但我找不到爲什麼我的commandButton沒有調用動作(我調試過它,這是問題)。我的代碼看起來很簡單,但...不起作用。也許這是一個新手問題,但我不知道它在哪裏。commandButton不會調用託管bean動作

我正在寫一個使用JSF2和Liferay Faces Alloy的liferay portlet。

我也讀過commandLink/commandButton/ajax backing bean action/listener method not invoked的問題,對我很有教育意義,但沒有一點能解決我的問題。

這裏是我的mainView.xhtml文件:

<?xml version="1.0"?> 

<f:view 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:aui="http://liferay.com/faces/aui" 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 

    <h:head /> 
    <h:body> 
     <h:form> 
      <h:messages globalOnly="true" layout="table" /> 
      <h:outputText id="esteTexto" value="#{ramSession.texto}" /> 
      <br /> 
      <h:commandButton action="#{ramSession.add}" styleClass="btn btn-default" value="#{ramSession.texto}"> 
      </h:commandButton> 
     </h:form> 
    </h:body> 
</f:view> 

這裏是我的SessionScoped ManagedBean文件,RamSession.java:

@ManagedBean 
@SessionScoped 
public class RamSession extends AbstractBaseBean implements Serializable { 

    private static final long serialVersionUID = 919724848720360000L; 
    private String texto; 

    public void add() { 
     this.texto = new String("Entrando"); 
    } 

    @PostConstruct 
    public void postConstruct() { 
     this.texto = new String("Jereje"); 
    } 

    public String getTexto() { 
     logger.info("gettingTexto"); 
     addGlobalSuccessInfoMessage(); 
     return this.texto; 
    } 

    public void setTexto(String texto) { 
     this.texto = texto; 
    } 
} 

我也試圖返回一個字符串(甚至不是必要的) ,一個actionListener方法,甚至與Ajax,但沒有。有誰能夠幫助我?非常感謝。

+0

所以,當你刪除'',它開始工作?如果不是,爲什麼它包含在問題中? – BalusC

+0

我刪除了但它無法正常工作。 我不理解你是什麼意思,當你問爲什麼它包含在問題中時,我只希望它在沒有工作時出現。評估 感謝您的時間! :) – ferda

+0

通過這種方式,您可以進一步降低可能的原因,並通過減少不會導致問題的干擾噪音來改善您的問題。 – BalusC

回答

1

也許你的mojarra listner配置不正確。

按照以下子步驟

一兩個中的一個。通過添加以下代碼以每pom.xml中添加在每個Liferay的JSF項目的liferay-面-init.jar依賴性:

<dependency> 
    <groupId>com.liferay.faces</groupId> 
    <artifactId>liferay-faces-init</artifactId> 
    <version>3.1.3-ga4</version> 
</dependency> 

添加以下代碼在你所有的JSF項目的每個WEB-INF/web.xml文件:

<listener> 
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
</listener> 
0

爲什麼不在聲明中初始化texto?

private String texto = "Jereje"; 

public void addGlobalSuccessInfoMessage(){ 
    //faces message 
} 

//Getter Setter 

然後您可以刪除整個PostConstruct方法。 此外,如果你只想設置一個字符串並且在沒有導航的情況下更新,只需使用f:setPropertyActionListener和f:ajax來更新消息。如果你想通過返回null或者void或者actionListener來設置字符串,那麼你可以使用action,但是我沒有看到你想要的原因。此鏈接可能有助於這些選項... Answer by Balus C。 可能還想從您的getter方法中刪除任何額外的邏輯。這不會影響你的問題,但它更清潔,並且在吸氣劑上創建成功消息似乎有問題。

<h:form id="myForm"> 
    <h:messages globalOnly="true" layout="table" /> 
    <h:outputText id="esteTexto" value="#{ramSession.texto}" /> 
    <br /> 
    <h:commandButton value="#{ramSession.texto}" actionListener="#{ramSession.addGlobalSuccessInfoMessage()}" styleClass="btn btn-default"> 
     <f:setPropertyActionListener value="Entrando" target="#{ramSession.texto}" /> 
     <f:ajax render="myForm" /> 
    </h:commandButton> 
</h:form> 
+0

感謝您的回覆,但在聲明中初始化文本不起作用。正如我在更新中所說的那樣,在.xml文件中可能存在一些錯誤的配置或聲明,但我找不到它,因爲當我開始一個具有相同代碼的新項目時, – ferda

相關問題