我正在尝试使用 jsf 创建搜索方法,但我的代码有问题谁能告诉我我做错了什么。该方法应该允许用户输入和查询并将结果传递给表,我有 2 个类.
当我测试代码以查看它是否有效时,我收到此错误
javax.el.MethodNotFoundException:/index.xhtml @19,84 action="#{search.searchresult}":找不到方法:Controller.Search@f09285.searchresult()
Java 代码
@ManagedBean
public class Search {
private String q;
private List <Testpaper>test;
public List<Testpaper> getTest() {
return test;
}
public void setTest(List<Testpaper> test) {
this.test = test;
}
public String getQ() {
return q;
}
public void setQ(String q) {
this.q = q;
}
public void getSearchresult()throws SQLException{
test = new TestDAO().Searches(q);
}
}
public class TestDAO {
@Resource(name="jdbc/GradeSprout2")
DataSource ds;
public List<Testpaper> Searches(String q)throws SQLException {
List<Testpaper> test = new ArrayList<Testpaper>();
if(ds==null)
throw new SQLException("Can't get data source");
//get database connection
Connection con = ds.getConnection();
if(con==null)
throw new SQLException("Can't get database connection");
PreparedStatement ps
= con.prepareStatement(
"select * from test where testname like ?;");
try{
ps.setString(1, "%" + q + "%");
ResultSet result = null;
result = ps.executeQuery();
while (result.next()) {
Testpaper testpaper = new Testpaper();
testpaper.setTestname(result.getString("testname"));
test.add(testpaper);
}}catch(Exception e1){
}
finally{
try{
con.close();
}
catch(Exception e2){
}
}
return test;
}
public TestDAO(){
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/GradeSprout2");
} catch (NamingException e) {
e.printStackTrace();
}
}
}
JSF 代码
<h:form>
<h:inputText size="95" value="#{search.q}"/>
<br></br>
<br></br>
<h:commandButton value="Find Test" action="#{search.searchresult}">
</h:commandButton>
</h:form>
<h:dataTable value="#{search.test}" var="test" rendered="#{not empty search.test}">
<h:column>#{test.testname}</h:column>
</h:dataTable>
<h:outputText value="No matches found!"
rendered="#{not empty search.q and empty search.test}" />