尝试使用支持bean
显示h:dataTable
时出现以下异常
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)
当使用这些文件执行以下代码时,因为我是jsf的新手并且我正在学习,请帮助解释一下
Employee.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;
}
}
这是我的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;
}
}
这是我的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>
我提到了不同的类似问题,但没有得到适合我的问题的答案。请帮我解决问题
提前致谢。
答案 0 :(得分:4)
据我所知,editEmployee(Employee employee)
方法的参数类型为Employee
,但是您没有为此参数传递值,这就是为什么它会尝试调用具有相同名称但没有参数的方法。
由于没有这样的内容,它会抛出MethodNotFoundException
。
由于你正在使用JSF 2.0+,你可以传递这样的参数:
<h:commandButton value="Edit" action="#{user.editEmployee(emp)}"
rendered="#{not emp.canEdit}">
</h:commandButton>
答案 1 :(得分:1)
要计算的次数太多,我已经有了MethodNotFoundException,很明显,该方法在bean上公开可用。如果我暂时为action或actionListener指定不同的现有方法,JSF将找到不同的方法。
清洁构建和部署是不够的; 我必须彻底重命名并移动“缺失”方法,然后清理构建,然后找到“缺失”(但现在重命名)的方法。一旦它工作,我将重命名的方法更改回其原始的“缺失”名称,然后清理构建,然后不再缺少以前的“缺失”方法。
我很想知道Eclipse Kepler 4.3.0引起这个恼人问题的原因。