java.lang.NoSuchMethodError:uges.servlets.MyQuery:method <init>()V not found </init>

时间:2012-05-10 19:17:57

标签: jsp tomcat servlets jess

我正在使用Tomcat v 7和Jess v 7.0

这是我得到的例外

root cause 

javax.servlet.ServletException: java.lang.NoSuchMethodError: uges.servlets.MyQuery:       method <init>()V not found
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.jav  a:911)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.catalog_jsp._jspService(catalog_jsp.java:121)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.NoSuchMethodError: uges.servlets.MyQuery: method <init>()V not found
org.apache.jsp.catalog_jsp._jspService(catalog_jsp.java:69)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是我的MyQuery类的源代码     package uges.servlets;

import jess.*;;

public class MyQuery
{

private static QueryResult result;
public MyQuery(Rete engine) throws JessException
{
    getQuery(engine);
}
public QueryResult getQuery(Rete engine) throws JessException
{
    result = engine.runQueryStar("all-products", new ValueVector());
    return result;
}
public String getString(String str) throws JessException
{
    String srtResult;
    srtResult = result.getString(str);
    return srtResult;

}
public Float getFloat(String str) throws JessException
{
    float flt;
    flt = result.getFloat(str);
    return flt;

}
public boolean next() throws JessException
{
    boolean next;
    next = result.next();
    return next;

}
}

这是Catalog Servlet     package uges.servlets;

import jess.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Catalog extends BaseServlet {

public void doGet(HttpServletRequest request,
                   HttpServletResponse response)
    throws IOException, ServletException {
    checkInitialized();

    try {
        String customerId =
            (String) request.getParameter("customerId");
        if (customerId == null || customerId.length() == 0) {
            dispatch(request, response, "/index.html");
            return;
        }

        request.getSession().invalidate();
        HttpSession session = request.getSession();

        session.setAttribute("customerId", customerId);
        session.setAttribute("orderNumber",
                             String.valueOf(getNewOrderNumber()));

        ServletContext servletContext = getServletContext();
        Rete engine = (Rete) servletContext.getAttribute("engine");

        //engine.reset();
          MyQuery result = new MyQuery(engine);
          //engine.runQueryStar("all-products", new ValueVector());
            request.setAttribute("queryResult",result);               

    } catch (JessException je) {
        throw new ServletException(je);
    }

    dispatch(request, response, "/catalog.jsp");
}

private int getNewOrderNumber() throws JessException {
    ServletContext servletContext = getServletContext();
    Rete engine = (Rete) servletContext.getAttribute("engine");
    int nextOrderNumber = 
        engine.executeCommand("(get-new-order-number)").intValue(null);
    return nextOrderNumber;
}

public void destroy() {
    try {
        ServletContext servletContext = getServletContext();
        Rete engine = (Rete) servletContext.getAttribute("engine");
        String factsFileName =
            servletContext.getInitParameter("factsfile");
        File factsFile = new File(factsFileName);
        File tmpFile =
            File.createTempFile("facts", "tmp", factsFile.getParentFile());
        engine.executeCommand("(save-facts " + tmpFile.getAbsolutePath() +
                              " order recommend line-item next-order-number)");
        factsFile.delete();
        tmpFile.renameTo(factsFile);

    } catch (Exception je) {
        // Log error
    } 
}


}

JSP catalog.jsp

<HTML>
<%@ page import="jess.*" %>
<jsp:useBean id="queryResult" class="uges.servlets.MyQuery" scope="request"/>

<HEAD>
<TITLE>Ordering from Tekmart.com</TITLE>
</HEAD>

<BODY>
<H1>Tekmart.com Catalog</H1>
Select the items you wish to purchase and press "Check Order" to continue.
<FORM action="/Order/recommend" method="POST">
<TABLE border="1">
<TR><TH>Name</TH>
    <TH>Catalog #</TH>
    <TH>Price</TH>
    <TH>Purchase?</TH>
</TR>
<% while (queryResult.next()) {
    String partNum =
            queryResult.getString("part-number");%> 
   <TR>
     <TD><%= queryResult.getString("name") %></TD>
     <TD><%= queryResult.getString("part-number") %></TD>
     <TD><%= queryResult.getFloat("price") %></TD>
     <TD><INPUT type="checkbox" name="items"
                value=<%= '"' + partNum + '"'%>></TD>
   </TR> 
 <% } %>                 
</TABLE>
<INPUT type="submit" value="Check Order">
</FORM>
</BODY>
</HTML>

任何线索将不胜感激。 谢谢,

2 个答案:

答案 0 :(得分:2)

JSP引擎正在尝试使用no-arg构造函数通过反射实例化您的类。您尚未定义no-arg构造函数,因此会发生此错误。 bean必须具有无参数构造函数。如果需要在bean上设置属性,请使用jsp:set-property

如果绝对无法添加无参数构造函数,则必须在jsp:use-bean标记之外对其进行实例化,并自行将其添加到适当的上下文中,如

< %   
  pkg.Foo foo = new pcg.Foo(constructor-arg);  
  context.setAttribute("foo", foo);  
% >  

这违反了无参数构造函数的基本JavaBean要求。

我相信一旦它在适当的范围内创建,其他JSP可以用jsp:use-bean引用它。

答案 1 :(得分:0)

您已经在预处理servlet中已经在请求范围内设置queryResult

request.setAttribute("queryResult",result);               

所以你根本不需要使用<jsp:useBean>。完全删除以下行:

<jsp:useBean id="queryResult" class="uges.servlets.MyQuery" scope="request"/>

因为您的servlet已将其设置为请求属性,所以${queryResult}中的对象已经在EL中可用。但是由于类本身的奇怪设计,很难在EL中正确访问它,所以你需要求助于旧学校的 scriptlet 。您只需要先request.getAttribute("queryResult")获取它。

要获得更好的设计方法,请查看servlets tag wiki pageShow JDBC ResultSet in HTML in JSP page using MVC and DAO pattern