简体   繁体   中英

commandButton doesn't invoke managed bean action

I've been reading and searching among the many pages with similar questions, but I cannot find why my commandButton is not invoking the action (I have debugged it and that is the problem). My code looks simple but... doesn't work. Maybe it's a newbie problem, but I don't know where it is.

I'm writing a portlet for liferay using JSF2 and Liferay Faces Alloy.

I've also read the question commandLink/commandButton/ajax backing bean action/listener method not invoked , very educational for me, but none of the points have solved my problem.

Here is my mainView.xhtml file:

<?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>

And here is my SessionScoped ManagedBean file, 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;
    }
}

I have also tried returning a String (even not necessary), with an actionListener method and even with ajax but nothing. Can anybody help me? Thanks a lot.

Maybe your mojarra listner is not configured correctly.

Follow one of the two following sub-steps

a. Add the liferay-faces-init.jar dependency in each Liferay JSF project by adding the following code to each pom.xml :

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

b. Add the following code in each WEB-INF/web.xml of all your JSF projects :

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

Why not initialize texto in the declaration?

private String texto = "Jereje";

public void addGlobalSuccessInfoMessage(){
    //faces message
}

//Getter Setter

Then you can remove the entire PostConstruct method. Also if all you want to do is set a string and update without navigation just use f:setPropertyActionListener with af:ajax for updating your messages. You can use action if you want by returning null or void, or actionListener and set the string inside of that, but I'm not seeing why you'd want to. This link may help with those options... Answer by Balus C . May want to remove any extra logic from your getter method as well. This won't affect your problem but it's cleaner and creating a success message on a getter seems problematic.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM