2012-05-10 33 views
0

我使用Tomcat五,七,和傑斯v 7.0java.lang.NoSuchMethodError:uges.servlets.MyQuery:法<init>()V沒有找到

這是我得到

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

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

+0

謝謝:)但如果我不能添加一個無參數的構造函數。我怎麼可以設置屬性?請給我一個例子!另一件事我搜索了很多,發現這樣的錯誤是因爲罐子版本有問題!任何線索?! – Lama

+0

查看我的更新回答 –

+0

感謝您的回答,但爲什麼有些bean需要實現Serialize接口? –

0

您已經在預處理servlet 已經設置請求範圍內的queryResult

request.setAttribute("queryResult",result);    

,這樣你就不會需要使用<jsp:useBean>可言。完全刪除以下行:

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

因爲你的servlet已經將其設置爲請求的屬性,對象是已經在EL可通過${queryResult}。但由於類本身的奇怪的設計,很難正確地訪問它EL,所以你需要ineed求助於老同學小腳本。你只需要先獲得request.getAttribute("queryResult")

要獲得更好的設計方法,請檢查servlets tag wiki pageShow JDBC ResultSet in HTML in JSP page using MVC and DAO pattern

相關問題