-1

I am trying to create a search method using jsf but I am having problem with my code could anyone tell me what I am doing wrong.the method should allow the user to enter and query and pass the results to a table,I have 2 class.

When I test the code to see if it works I get this error

javax.el.MethodNotFoundException: /index.xhtml @19,84 action="#{search.searchresult}": Method not found: Controller.Search@f09285.searchresult()

Java code

@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 code

<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}" />
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user2491007
  • 29
  • 10
  • Please add your JSF code to do a better analysis of your problem and complete the code of the first class (class name, is managed bean, etc). – Luiggi Mendoza Jun 16 '13 at 16:22
  • For what I know, you are doing this wrong: asking the wrong question. – darijan Jun 16 '13 at 16:31
  • 2
    For people that do not know anything about JSF and downvote/vote to close: please **refrain** to do this if you don't understand. – Luiggi Mendoza Jun 16 '13 at 16:31
  • @darijan why is this a wrong question? – Luiggi Mendoza Jun 16 '13 at 16:32
  • You are asking what is wrong, but not giving us what is the problem, the stack trace in case of an error, etc... – darijan Jun 16 '13 at 16:34
  • 2
    @darijan if you had written a basic application using JSF you could notice that there are errors that will never appear in the stacktrace such as validation and conversion errors (they could be logged but depending on how you defined the log levels). Also, there are at least 10 possible causes for a `` to not fire a server call explained here: [h:commandLink / h:commandButton is not being invoked](http://stackoverflow.com/a/2120183/1065197). The fact that this question does not help us to get the direct answer can probably be since OP's new to the site. – Luiggi Mendoza Jun 16 '13 at 16:38
  • Again, please edit your question and add your JSF code. – Luiggi Mendoza Jun 16 '13 at 16:42

1 Answers1

2

The error message is very specific: you do not have a searchresult method in your search managed bean. In order to make this work, you should change the method getSearchresult name for searchresult.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332