0

我正在使用 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 类包 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;

}
}

这是目录 Servlet 包 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 目录.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>

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

4

2 回答 2

2

JSP 引擎正在尝试使用无参数构造函数通过反射来实例化您的类。您尚未定义无参数构造函数,因此会发生此错误。一个 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.

于 2012-05-10T20:20:02.813 回答
0

您已经在预处理 servlet设置queryResult了请求范围。

request.setAttribute("queryResult",result);               

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

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

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

要获得更好的设计方法,请检查servlets 标记 wiki 页面Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern

于 2012-05-11T12:16:57.670 回答