0

我一直在阅读和搜索许多有类似问题的页面,但我找不到为什么我的 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 返回一个字符串(甚至没有必要),但什么也没有。有谁能够帮我?非常感谢。

4

2 回答 2

1

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

遵循以下两个子步骤之一

一个。通过将以下代码添加到每个 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>

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

<listener>
     <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
于 2016-06-28T14:53:00.097 回答
0

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

private String texto = "Jereje";

public void addGlobalSuccessInfoMessage(){
    //faces message
}

//Getter Setter

然后您可以删除整个 PostConstruct 方法。此外,如果您只想设置一个字符串并在没有导航的情况下进行更新,只需使用 f:setPropertyActionListener 和 af:ajax 来更新您的消息。如果需要,您可以通过返回 null 或 void 或 actionListener 并在其中设置字符串来使用操作,但我不明白您为什么要这样做。此链接可能对这些选项有所帮助...由 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>
于 2016-04-16T03:42:57.017 回答