Rami Raddaoui
Rami Raddaoui

Reputation: 75

JSF: method not found

I'm getting this error while developping my application

javax.el.MethodNotFoundException: /test.xhtml @18,99 
action="#{ComplexeController.saveComplexe}": Method not found:
[email protected]()

test.xhtml :

<h:body>
<h1>Génération des Complexes</h1>
<h:form>
    Nom Complexe: <h:inputText value="#{ComplexeController.complexe.nomComp}"/><br/>
    Nom Zone: <h:selectOneMenu id="nomZone" value="#{ComplexeController.complexe.zoneParc}" converter="#{GenericConverter}">
        <f:selectItems value="#{ZoneController.remplireItem()}"/>
</h:selectOneMenu>
<br/>
<h:commandButton action="#{ComplexeController.saveComplexe}" value="Insérer un nouveau complexe"/>
<h:commandButton action="#{ComplexeController.updateComplexe}" value="Modifier un complexe"/>
<br/>
<h:commandLink action="complexe" value="acceuil"/>
</h:form>
</h:body>

the entity Complexe.java

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Complexe generated by hbm2java
 */
@Entity
@Table(name="COMPLEXE"
    ,schema="PROJET"
)
public class Complexe  implements java.io.Serializable {


     private String nomComp;
     private ZoneParc zoneParc;
     private Set<Parc> parcs = new HashSet<Parc>(0);

    public Complexe() {
    }


    public Complexe(String nomComp) {
        this.nomComp = nomComp;
    }
    public Complexe(String nomComp, ZoneParc zoneParc, Set<Parc> parcs) {
       this.nomComp = nomComp;
       this.zoneParc = zoneParc;
       this.parcs = parcs;
    }

     @Id 

    @Column(name="NOM_COMP", unique=true, nullable=false, length=30)
    public String getNomComp() {
        return this.nomComp;
    }

    public void setNomComp(String nomComp) {
        this.nomComp = nomComp;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="NOM_ZONEE")
    public ZoneParc getZoneParc() {
        return this.zoneParc;
    }

    public void setZoneParc(ZoneParc zoneParc) {
        this.zoneParc = zoneParc;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="complexe")
    public Set<Parc> getParcs() {
        return this.parcs;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (nomComp != null ? nomComp.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Complexe)) {
            return false;
        }
        Complexe other = (Complexe) object;
        if ((this.nomComp == null && other.nomComp != null) || (this.nomComp != null && !this.nomComp.equals(other.nomComp))) {
            return false;
        }
        return true;
    }

    public void setParcs(Set<Parc> parcs) {
        this.parcs = parcs;
    }
}

and now ComplexeDaoImpl.java

    import config.HibernateUtil;
    import fr.code.parc.model.Complexe;
    import java.util.List;
    import org.hibernate.Session;
    import org.hibernate.Transaction;

    /**
     *
     * @author raddaouirami
     */
    public class ComplexeDaoImpl implements ComplexeDao{

        @Override
        public List<Complexe> list() {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            List complexes = session.createQuery("from Complexe").list();
            t.commit();
            return complexes;
        }

        @Override
        public Complexe getComplexe(String nomComp) {
            Session session = HibernateUtil.getSessionFactory().openSession();
            return (Complexe) session.load(Complexe.class, nomComp);
        }

        @Override
        public void save(Complexe complexe) {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.save(complexe);
            t.commit();
        }

        @Override
        public void update(Complexe complexe) {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.update(complexe);
            t.commit();
        }

        @Override
        public void remove(Complexe complexe) {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.delete(complexe);
            t.commit();
        }


    }

and Finally ComplexeController.java

import fr.code.parc.dao.ComplexeDao;
import fr.code.parc.dao.ComplexeDaoImpl;
import fr.code.parc.model.Complexe;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author raddaouirami
 */

@ManagedBean
@SessionScoped
public class ComplexeController {

    private Complexe complexe;
    private DataModel listeComplexes;
    private int selectedItemIndex;


    /**
     * Creates a new instance of ComplexeController
     */
    public ComplexeController() {
        complexe = new Complexe();
    }
    /* public Complexe getSelected() {
        if (complexe == null) {
            complexe = new Complexe();
            selectedItemIndex = -1;
        }
        return complexe;
    }*/

    public DataModel getListeComplexes() {
        List<Complexe> liste = new ComplexeDaoImpl().list();
        listeComplexes = new ListDataModel(liste);
        return listeComplexes;
}

    /**
     * @return the complexe
     */
    public Complexe getComplexe() {
        return complexe;
    }

    /**
     * @param complexe the complexe to set
     */
    public void setComplexe(Complexe complexe) {
        this.complexe = complexe;
    }


    public String preparationAddComplexe(){
        setComplexe(new Complexe());
        return "test";
}
    public String preparationEditComplexe(){
        setComplexe((Complexe)(getListeComplexes().getRowData()));
        return "test";
}

    public String DeleteComplexe(){
        Complexe complexes = (Complexe)(getListeComplexes().getRowData());
        ComplexeDao dao = new ComplexeDaoImpl();
        dao.remove(complexes);
        return "complexe";
    }
    public String SaveComplexe(){
        ComplexeDao dao = new ComplexeDaoImpl();
        dao.save(getComplexe());
        return "complexe";
    }
    public String UpdateComplexe(){
        ComplexeDao dao = new ComplexeDaoImpl();
        dao.update(complexe);
        return "complexe";
    }    
}

How can I solve it?

Upvotes: 1

Views: 16151

Answers (3)

minibi
minibi

Reputation: 50

Try to access the ManagedBean with first char to low case e.g.

action="#{complexeController.saveComplexe}"

instead of

action="#{ComplexeController.saveComplexe}"

Upvotes: 0

Serhii Shevchyk
Serhii Shevchyk

Reputation: 39436

In view you invoke saveComplexe

action="#{ComplexeController.saveComplexe}"

ComplexeController has only SaveComplexe method.

Either change to

action="#{ComplexeController.SaveComplexe}"

or rename method in ComplexeController to saveComplexe

Upvotes: 5

Kishor Prakash
Kishor Prakash

Reputation: 8151

2 Solutions:
Solution 1.
Change in test.xhtml : complexeController.saveComplexe [Notice first letter in complexeController is in smaller case]

<h:commandButton action="#{complexeController.saveComplexe}" value="Insérer un nouveau complexe"/>
<h:commandButton action="#{complexeController.updateComplexe}" value="Modifier un complexe"/>

Solution 2.
Change in ComplexeController.java:

@ManagedBean(name="ComplexeController ")
@SessionScoped
public class ComplexeController {
....
}

Read the Following Info about Configuring ManagedBeans in JSF:

There are 2 ways to use @ManagedBean to expose a Java Bean class to Managed Bean class.

Way 1.

@ManagedBean
public class ComplexeController{
....
}

In this case the bean exposed with same name but the first letter is smaller case, i.e., you can access the bean in Facelet as

#{complexeController}

Way 2.

@ManagedBean(name="myBean")
public class ComplexeController{
....
}

In this case the bean exposed with same name but the first letter is smaller case, i.e., you can access the bean in Facelet as

#{myBean}

Upvotes: 0

Related Questions