JSF:找不到方法

时间:2013-07-24 21:39:21

标签: jsf

我在开发应用程序时遇到此错误

javax.el.MethodNotFoundException: /test.xhtml @18,99 
action="#{ComplexeController.saveComplexe}": Method not found:
fr.code.parc.controller.ComplexeController@97aead.saveComplexe()

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>

实体 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;
    }
}

现在 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();
        }


    }

和最后 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";
    }    
}

我该如何解决?

3 个答案:

答案 0 :(得分:5)

在视图中,您可以调用saveComplexe

action="#{ComplexeController.saveComplexe}"

ComplexeController只有SaveComplexe方法。

改为

action="#{ComplexeController.SaveComplexe}"

或将ComplexeController中的方法重命名为saveComplexe

答案 1 :(得分:0)

2解决方案:
解决方案1。
test.xhtml中的变化: complexeController.saveComplexe [注意complexeController中的第一个字母是小写的]

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

解决方案2。
ComplexeController.java中的更改:

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

阅读以下有关在JSF中配置ManagedBeans的信息:

有两种方法可以使用 @ManagedBean 将Java Bean类公开给Managed Bean类。

方式1。

@ManagedBean
public class ComplexeController{
....
}

在这种情况下,暴露了相同名称但第一个字母的bean是较小的情况,即你可以在Facelet中访问bean

#{complexeController}

方式2。

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

在这种情况下,暴露了相同名称但第一个字母的bean是较小的情况,即你可以在Facelet中访问bean

#{myBean}

答案 2 :(得分:0)

尝试使用第一个char到低位来访问ManagedBean e.g。

action="#{complexeController.saveComplexe}"

而不是

action="#{ComplexeController.saveComplexe}"