【问题标题】:commandButton doesn't invoke managed bean actioncommandButton 不调用托管 bean 操作
【发布时间】:2016-04-15 13:06:02
【问题描述】:

我一直在阅读和搜索许多有类似问题的页面,但我找不到我的 commandButton 没有调用操作的原因(我已经调试过它,这就是问题所在)。我的代码看起来很简单,但是......不起作用。可能是新手问题,但不知道出处。

我正在使用 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,但什么也没有。有谁能够帮助我?非常感谢。

【问题讨论】:

  • 那么,当你删除&lt;c:choose&gt;&lt;c:when&gt;&lt;c:otherwise&gt;,它就开始工作了?如果不是,为什么它会包含在问题中?
  • 我删除了两个 但它无论如何都不起作用。当您问为什么将其包含在问题中时,我不明白您的意思,我只希望它在没有工作时出现Assessment 感谢您的时间! :)
  • 通过这种方式,您可以进一步确定潜在原因并通过减少不会导致问题的干扰噪音来改善您的问题。
  • 好的,对不起,你是对的 :) 我正在尝试以这种方式解决它。也许缺少一些 ui 组件?我找不到问题的原因
  • 更新:经过一次又一次的测试,我意识到代码没问题。 .xml 文件中可能有一些错误的配置或声明,但我找不到它。我用相同的代码开始了一个新项目,它可以工作。如果有人想测试我已经保存了整个 Eclipse 项目,我会在某一天回到问题上来搜索问题所在。

标签: jsf jsf-2 liferay commandbutton method-invocation


【解决方案1】:

也许您的 mojarra 监听器配置不正确。

执行以下两个子步骤之一

a. 通过将以下代码添加到每个 pom.xml 中,在每个 Liferay JSF 项目中添加 liferay-faces-init.jar 依赖项:

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

b.在所有JSF项目的每个WEB-INF/web.xml中添加以下代码:

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

【讨论】:

    【解决方案2】:

    为什么不在声明中初始化texto?

    private String texto = "Jereje";
    
    public void addGlobalSuccessInfoMessage(){
        //faces message
    }
    
    //Getter Setter
    

    然后您可以删除整个 PostConstruct 方法。 此外,如果您只想设置一个字符串并在没有导航的情况下更新,只需使用 f:setPropertyActionListener 和 f:ajax 来更新您的消息。如果需要,您可以通过返回 null 或 void 或 actionListener 并在其中设置字符串来使用操作,但我不明白您为什么要这样做。此链接可能对这些选项有所帮助...Answer by Balus C。 可能还想从您的 getter 方法中删除任何额外的逻辑。这不会影响您的问题,但它更干净,并且在 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>
    

    【讨论】:

    • 感谢您的回复,但在声明中初始化文本不起作用。正如我在更新中所说,.xml 文件中可能有一些错误的配置或声明,但我找不到它,因为当我使用相同的代码开始一个新项目时它可以工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2011-06-14
    • 2012-07-25
    • 2016-06-26
    • 1970-01-01
    相关资源
    最近更新 更多