我正在尝试搜索我使用JSF开发的数据库作为前端技术。我收到一条错误说明如下:
无法找到带[0]参数的方法[searchContractors]
这是我使用过的代码。任何人都可以告诉我,如果有明显的事情我做错了,因为我无法理解为什么我会收到这个错误。谢谢你的帮助。
JSF代码
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Search</title>
</head>
<body>
<h:form id="searchForm">
<H2>Search</H2>
<H4>Please select the county that you live in.
</H4>
<table>
<tr>
<td><h:outputLabel for="county">
<h:outputText id="countyLabel" value="County" />
</h:outputLabel></td>
<td><h:selectOneMenu id="countyName"
value="#{searchBean.countyId}">
<f:selectItems value="#{registerBean.counties}" var="county"
itemLabel="#{county.name}" itemValue="#{county.id}" />
</h:selectOneMenu></td>
</tr>
<tr>
<td><h:commandButton id="searchContractors"
action="#{searchBean.searchContractors(searchBean.countyId)}">
<h:outputText value="Search Contractors" />
</table>
</h:form>
</body>
</html>
Java代码
@ManagedBean
@SessionScoped
public class SearchBean implements Serializable {
private static final long serialVersionUID = -2107387060867715013L;
private static final String PERSISTENCE_UNIT_NAME = "NeedABuilderUnit";
private static EntityManagerFactory factory;
private int countyId;
public List<BusinessAccount> searchContractors(int countyId) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
List<BusinessAccount> contractorList = new ArrayList<BusinessAccount>();
em.getTransaction().begin();
Query myQuery = em.createQuery("SELECT u FROM BusinessAccount u WHERE u.county.id=:CountyId");
myQuery.setParameter("CountyId", countyId);
contractorList=myQuery.getResultList();
em.getTransaction().commit();
em.close();
return contractorList;
}
public int getCountyId() {
return countyId;
}
public void setCountyId(int countyId) {
this.countyId = countyId;
}
}
答案 0 :(得分:0)
以下内容应该有效:
<ui:param name="countyId" value="#{searchBean.countyId}" />
...
<h:commandButton id="searchContractors"
action="#{searchBean.searchContractors(countyId)}">