我的方法中的commandButton操作中的MethodNotFoundException

时间:2017-05-17 10:23:00

标签: primefaces jsf-2

这是我的产品豆  我有这个错误 javax.el.MethodNotFoundException:/Ajouter_Prod.xhtml @ 53,110 action =“#{produit.operation}”:找不到方法:com.octest.beans.Produit@192bd86c.operation()

package com.octest.beans;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;

@ManagedBean(name="produit")
@SessionScoped
@ViewScoped
public class Produit implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 7822654245453106349L;
    private int Id_Prod;
    private String description;
    private String libelle;
    private int prix;
    private String photo;
    private int nbProduit;
    private Produit selectedProd;

    public Produit getSelectedProd() {
        return selectedProd;
    }

    public void setSelectedProd(Produit selectedProd) {
        this.selectedProd = selectedProd;
    }

    public int getNbProduit() {
        return nbProduit;
    }

    public void setNbProduit(int nbProduit) {
        this.nbProduit = nbProduit;
    }

    public int getId_prod() {
        return Id_Prod;
    }
    public void setId_prod(int reference) {
        this.Id_Prod = reference;
    }
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLibelle() {
        return libelle;
    }
    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }
    public int getPrix() {
        return prix;
    }
    public void setPrix(int prix) {
        this.prix = prix;
    }
    public String getPhoto() {
        return photo;
    }
    public void setPhoto(String photo) {
        this.photo = photo;
    }
      private UploadedFile file;

        public UploadedFile getFile() {
            return file;
        }

        public void setFile(UploadedFile file) {
            this.file = file;
        }

        public void upload() {
            if(file != null) {
                FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, message);
            }
        }
    static Connection connexion;
    static void LoadDatabase(){

        try {
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException e){System.out.println("Erreur chargement driver");}
        try{
            connexion= DriverManager.getConnection("jdbc:mysql://localhost:3306/caddiebd","root","");
        }catch(SQLException e){

        }
    }


    public String operation(){
        int i=0;
        PreparedStatement preState = null;
        try{
         String sql = "INSERT INTO produits(Id_Prod, Libelle, Description,Image, Quantité,prix ) VALUES(NULL,?,?,?,?,?)";
            preState   = connexion.prepareStatement(sql); 
            preState.setString(1, libelle);
            preState.setString(2, description);
            preState.setString(3,  photo);
            preState.setLong(4, nbProduit);
            preState.setLong(5,prix);
            i = preState.executeUpdate();
           } catch(Exception e)
        {
                System.out.println(e); 
            }
            finally
            {
            try
            {
                connexion.close();
                preState.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            }
            if(i >0)
            {
                return "output";
            }
            else
            {
                return "invalid";
            }
    }


    public List<Produit> recupererProduit(){
         List<Produit> produits =new ArrayList<Produit>();
        Statement statement=null;
        ResultSet resultat =null;
        LoadDatabase();

        try{

            statement= connexion.createStatement();
            String sql="SELECT * FROM produits";

        // execution de la requete
            resultat=statement.executeQuery(sql);
       // recuperation des donnees

            while(resultat.next()){

            int ref=resultat.getInt("Id_Prod");
            String libelle=resultat.getString("Libelle");
            String description=resultat.getString("Description");
            int prix=resultat.getInt("prix");
            int quantite=resultat.getInt("Quantite");
            String photo=resultat.getString("Image");           
            Produit article = new Produit();

            article.setId_prod(ref);
            article.setLibelle(libelle);
            article.setPrix(prix);
            article.setDescription(description);
            article.setNbProduit(quantite);
            article.setPhoto(photo);

            produits.add(article);
            }

        }catch(SQLException e){ System.out.println("probleme de syntaxe sql");
        } finally{
            //fermeture de la connexion
            try{
                if(resultat !=null)
                    resultat.close();
                if(statement != null)
                    statement.close();
                if(connexion != null)
                    connexion.close();
            }catch (SQLException ignore){

            }
        }

    return produits;
    }



}

这是我的xhtml页面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>

</h:head> 
<h:body> 
 <h2>Formulaire d'inscription</h2>
 <h:form>
  <p:fieldset legend="Ajouter Client" widgetVar="regWidget" style="width: 600px;">
   <h:panelGrid columns="3" width="550" border="0">
    <h:outputLabel value="Libelle" />
    <p:inputText value="#{produit.libelle}" 
       id="libelle"
        required="true" 
        requiredMessage="Le libelle du produit est obligatoire"
        >
    </p:inputText>
    <p:message for="libelle"/>

     <h:outputLabel value="Description" />
    <p:inputText value="#{produit.description}"   id="desc"
       required="fals"
       validatorMessage="">
      </p:inputText>
    <p:message for="desc" />

    <h:outputLabel value="Quantité" style="margin-top:5cm"/>
    <p:inputNumber value="#{produit.nbProduit}"
       id="nbProduit"
       required="true"
       validatorMessage="Quantité obligatoire">
      </p:inputNumber>
        <p:message for="nbProduit" />

    <h:outputLabel value="Prix" />
    <p:inputNumber value="#{produit.prix}"
       id="prix"
       required="true"
       validatorMessage="Quantité obligatoire">
      </p:inputNumber>
    <p:message for="prix" />

   <p:growl id="messages" showDetail="true" />
    <p:fileUpload value="#{produit.file}" mode="simple" skinSimple="true"/>

    <p:commandButton value="Envoyer" icon="ui-icon ui-icon-check" action="#{produit.operation}" ajax="false"></p:commandButton>
   </h:panelGrid>
  </p:fieldset>

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

这是错误代码

javax.faces.el.MethodNotFoundException: javax.el.MethodNotFoundException: /Ajouter_Prod.xhtml @53,110 action="#{produit.operation}": Method not found: com.octest.beans.Produit@192bd86c.operation()
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1366)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

我真的不明白为什么错误

0 个答案:

没有答案