Does not respond ActionListener CommandButton Web App

0

I have a web application made in Netbeans 8.1 , Java 8 and Maven

First of all in my class Managed Bean Controller.java place the label @ViewScope . When I compiled and deployed the application I got the error

  

Severe: Exception while loading the app Serious: Undeployment failed   for context / CustomerData1 Grave: Exception while loading the app:   CDI deployment failure: WELD-000072: Bean declaring a passivating scope   must be passivation capable. Bean: Managed Bean [class   net.ensode.glassfish.jsfajax.Controller] with qualifiers [@Default   @Any @Named] org.jboss.weld.exceptions.DeploymentException:   WELD-000072: Bean declaring a passivating scope must be passivation   capable. Bean: Managed Bean [class   net.ensode.glassfish.jsfajax.Controller] with qualifiers [@Default   @Any @Named]

So what I did was remove the label @ViewScoped and the application compiled correctly and also the deploy but when I click on the command button the method code calculateTotal is not executed, it simply does not do any action. Does the @ViewScoped tag have to be removed from the controller.java code?

I also attach the xhtml file.

import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Named;


//@ViewScoped elimine esta etiqueta y el proyecto compilo y deploy ok.


@Named
public class Controller {

  private String text;
  private int firstOperand;
  private int secondOperand;
  private int total;

  public Controller() {
  }

  public void calculateTotal(ActionEvent actionEvent) {
    total = firstOperand + secondOperand;
  }

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  public int getFirstOperand() {
    return firstOperand;
  }

  public void setFirstOperand(int firstOperand) {
    this.firstOperand = firstOperand;
  }

  public int getSecondOperand() {
    return secondOperand;
  }

  public void setSecondOperand(int secondOperand) {
    this.secondOperand = secondOperand;
  }

  public int getTotal() {
    return total;
  }

  public void setTotal(int total) {
    this.total = total;
  }
}
<html xmlns="http://www.w3.org/1999/xhtml"
         xmlns:h="http://java.sun.com/jsf/html"
         xmlns:f="http://java.sun.com/jsf/core">
     <h:head>
       <title>JSF Ajax Demo</title>
     </h:head>
      <h:body>
        <h2>JSF Ajax Demo</h2>
       <h:form>
      <h:messages/>
      <h:panelGrid columns="2">

        <h:outputText value="Echo input:"/>
        <h:inputText id="textInput" value="#{controller.text}">
          <f:ajax render="textVal" event="keyup"/>
        </h:inputText>

        <h:outputText value="Echo output:"/>
        <h:outputText id="textVal" value="#{controller.text}"/>
      </h:panelGrid>
      <hr/>
      <h:panelGrid columns="2">
        <h:panelGroup/>
        <h:panelGroup/>
        <h:outputText value="First Operand:"/>
        <h:inputText id="first" value="#{controller.firstOperand}" size="3"/>
        <h:outputText value="Second Operand:"/>
        <h:inputText id="second" value="#{controller.secondOperand}" size="3"/>
        <h:outputText value="Total:"/>
        <h:outputText id="sum" value="#{controller.total}"/>
        <h:commandButton actionListener="#{controller.calculateTotal}"
                         value="Calculate Sum">
          <f:ajax execute="first second" render="sum"/>
        </h:commandButton>

      </h:panelGrid>
    </h:form>
  </h:body>
</html>

Thanks for your help.

    
asked by harpazo 28.01.2016 в 01:52
source

3 answers

2

I recommend you enable the use of the @ViewScoped annotation in your bean and add that your Controller implements the Serializable interface. This is to solve the problem of WELD-000072, based on Java Error: WELD-000072 Managed bean declaring to passivating scope must be passivation capable .

Then, I recommend making the following changes to your code:

@Named
@ViewScoped
public class Controller implements Serializable {
    //...
    public void calculateTotal() {
        total = firstOperand + secondOperand;
    }
    //...
}

And in the Facelets code:

<h:commandButton action="#{controller.calculateTotal}"
    value="Calculate Sum">
    <f:ajax execute="first second" render="sum"/>
</h:commandButton>

I recommend using action before actionListener . You can find more explanation of the differences between them in the following link: Differences between action and actionListener . BalusC is a guru in JSF.

    
answered by 28.01.2016 в 16:44
1

Try to put action instead of actionlistener since action serves for buttons and listeners for checkboxes, radios ...

The action would be executed when clicking and every time the value changes (that's what the actionlistener would do)

    
answered by 28.01.2016 в 14:02
1

To complement the answer of Luiggi Mendoza, I tell you a couple of things.

All backing bean must have a scope . A scope defines the scope of that bean in the application. JSF 2.2 offers the following scopes:

  • ViewScoped: exists while in view)
  • RequestScoped: only exists for the duration of the HTTP request)
  • FlowScoped: exists while the defined flow lasts)
  • ConversationScoped: exists while the conversation is not ending by calling coversation.end() . The conversation can be maintained between several pages.
  • SessionScoped: exists for the duration of the HTTP session)
  • ApplicationScoped: exists while the application is up)

When to use each scope?

You have the answer here

A backing bean must be serialized unless it is RequestScoped . This is because the views are saved in session that would be HttpSession and as you know all session attributes must be serializable (because they persist on the hard drive or other media). For this reason, most of your backing beans should implement Serializable .

    
answered by 31.01.2016 в 19:41