Call JavaScript in a bean

1

What I want to do is the following. When a request is registered in the database in the interface a message of successful registration is shown, the message that I must put is one of SweetAlert .

I use JavaServer Faces and I want to know if in a method that is in a Managed Beans type SessionScoped I can call the SweetAlert and how do I do it?

Thank you very much for your help.

These are SweetAlert's messages: link

    
asked by Lina Cortés 24.05.2016 в 15:10
source

2 answers

1

What you want to do can not be done. One solution is to define some variable within the bean and place it inside a h: inputHidden, from java once the order registration is successfully done, set that variable with the message that you want to show. For example:

<h:inputHidden value="#{Bean.variableMensaje}" id="mensaje" > </h:inputHidden>
<h:commandButton value="Registrar Pedido" id="butonRegistro">
 <f:ajax listener="#{Bean.registrarPedido()}" render="mensaje" onevent="funcion que muestre mensaje" />
</h:commandButton>

And from the method registerPedido () you set the variable "variableMessage" with the message that you want to show from the interface.

In the js function you get the value of the inputHidden and that is how the message you set from the bean will show you.

    
answered by 15.06.2016 в 18:14
0

Using the onevent attribute of <f:ajax /> you can do it.

<h:commandButton value="Registrar pedido" action="#{bean.registrarPedido()}">
    <f:ajax execute="@form" onevent="mostrarMensaje(data.status) />
</h:commandButton>

Now, you have to be careful with onevent because by default it will run in the three phases of each JSF AJAX request: begin , complete and success . To ensure that the message is displayed only once, it must be done in the complete or success phase.

function mostrarMensaje(status) {
    // luego de la petición AJAX
    if(status === 'success') {
        alert('Producto registrado');
    }
}
    
answered by 15.06.2016 в 18:25