jsf给出方法未找到异常虽然它在那里,javax.el.MethodNotFoundException

Jas*_*son 1 jsf el jsf-2

尝试h:dataTable使用支持 bean显示 a 时出现以下异常

javax.faces.el.MethodNotFoundException: javax.el.MethodNotFoundException: /table.xhtml @29,36 action="#{user.editEmployee}": Method not found: com.jason.jsf.User@1df228e.editEmployee()
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
javax.faces.component.UICommand.broadcast(UICommand.java:311)
javax.faces.component.UIData.broadcast(UIData.java:912)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Run Code Online (Sandbox Code Playgroud)

当我使用这些文件执行以下代码时,因为我是 jsf 的新手并且正在学习,请帮忙做一些解释

雇员.java

 package com.jason.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "employee", eager = true)
@SessionScoped
public class Employee {

    private String Id, name;
    private boolean canEdit;

    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Employee(String id, String name) {
        super();
        Id = id;
        this.name = name;
    }

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isCanEdit() {
        return canEdit;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的 User.java

    package com.jason.jsf;

import java.util.ArrayList;
import java.util.Arrays;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "user", eager = true)
@SessionScoped
public class User {

    private static final long serialVersionUID = 1L;

    private String name;
    private String id;

    private static final ArrayList<Employee> employees = new ArrayList<Employee>(
            Arrays.asList(new Employee("John", "Marketing"), new Employee(
                    "Robert", "Marketing"), new Employee("Mark", "Sales"),
                    new Employee("Chris", "Marketing"), new Employee("Peter",
                            "Customer Care")));




    public ArrayList<Employee> getEmployees() {
        return employees;
    }

     public String addEmployee() {
     Employee employee = new Employee(name, id);
     employees.add(employee);
     return null;
     }

     public String deleteEmployee(Employee employee) {
     employees.remove(employee);
     return null;
     }

      public String editEmployee(Employee employee){
          employee.setCanEdit(true);
          return null;
       }

       public String saveEmployees(){
          //set "canEdit" of all employees to false 
          for (Employee employee : employees){
             employee.setCanEdit(false);
          }     
          return null;
       }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的 table.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">
<h:head>
    <title>JSF tutorial</title>
    <h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>
    <h:form>
        <h:dataTable value="#{user.employees}" var="emp"
            styleClass="employeeTable" headerClass="employeeTableHeader"
            rowClasses="employeeTableOddRow,employeeTableEvenRow">
            <h:column>
                <f:facet name="header">Name</f:facet>
                <h:inputText value="#{emp.name}" size="10" rendered="#{emp.canEdit}" />
                <h:outputText value="#{emp.name}" rendered="#{not emp.canEdit}" />
            </h:column>
            <h:column>
                <f:facet name="header">ID</f:facet>
                <h:inputText value="#{emp.id}" size="20" rendered="#{emp.canEdit}" />
                <h:outputText value="#{emp.id}" rendered="#{not emp.canEdit}" />
            </h:column>
            <h:column>
                <f:facet name="header">Edit</f:facet>
                <h:commandButton value="Edit" action="#{user.editEmployee}"
                    rendered="#{not emp.canEdit}">
                </h:commandButton>
            </h:column>
        </h:dataTable>
        <br />
        <h:commandButton value="Save Employees" action="#{user.saveEmployees}" />

    </h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

我已经提到了不同的类似问题,但没有得到适合我的问题的答案。请帮我解决

提前致谢。

Kon*_*kov 5

据我所知,该editEmployee(Employee employee)方法有一个 type 参数Employee,但是您没有为此参数传递值,这就是它尝试调用具有相同名称但没有参数的方法的原因。

由于没有这样的,它会抛出一个MethodNotFoundException.

由于您使用的是 JSF 2.0+,您可以像这样传递参数:

<h:commandButton value="Edit" action="#{user.editEmployee(emp)}"
                 rendered="#{not emp.canEdit}">
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)