【问题标题】:HTTP Status 500 - Unable to compile class for JSP: import cannot be resolved to a typeHTTP 状态 500 - 无法为 JSP 编译类:导入无法解析为类型
【发布时间】:2014-10-01 17:31:13
【问题描述】:

以下是我在 jsp 文件中的代码:

int s_id = session.getAttribute("s_id") != null ? ((Integer)     session.getAttribute("s_id")).intValue() : 0 ;
String Pending = "Pending";
%>
<!-- <jsp:useBean id="link" scope="application" class = "Shop" /> -->
<%
Shop s = new Shop();
int g_orderQuantity=0;

错误:

HTTP Status 500 - 
--------------------------------------------------------------------------------
type Exception report
message 
description The server encountered an internal error () that prevented it from     fulfilling this request.
exception 
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 30 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 30 : <!-- <jsp:useBean id="link" scope="application" class = "Shop" /> -->

An error occurred at line: 32 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 32 : Shop s = new Shop();

页面上的一些答案:"Import cannot be resolved" with JSP 建议使用页面导入指令如下: &lt;%@ page import="pkgname.Class1"%&gt; 他们坐在哪里,你必须给出类的完全限定名称。

我已将我的 Shop 类放置如下:C:\Users\Documents\ShopSystem-AWSJavaWebProject\src\myPackage\Shop.java 现在我正在尝试使用以下语法: &lt;%@ page import="myPackage.Shop"%&gt; 但这也不起作用......它给出了一个错误,说 import myPackage 无法解析!

然后按照中提到的解决方案:Eclipse is not importing classes in jsp files 我已确保启用 Preferences->import->Web->Jsp Files->Editor->Content assistant->Add_import_instead_of_qualified_name 现在当我使用上面的行时:&lt;%@ page import="myPackage.Shop"%&gt; 我现在收到以下错误:

exception : org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 16 in the generated java file
Only a type can be imported. myPackage.Shop resolves to a package

An error occurred at line: 31 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 31: <!-- <jsp:useBean id="link" scope="application" class = "Shop" /> -->

An error occurred at line: 33 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 33 : Shop s = new Shop();

另一页:problem in importing java class in jsp 建议&lt;%@ page language="java" import="yourpackage.subpackage.*,java.util.*" %&gt; 因此我将我的代码更新为:

<%@ page language="java" import="myPackage.Shop" %>

但错误依旧!

我什至尝试过以下方法:

<%@ page language="java" import="myPackage.Shop" session="true"%>

正如To import class to JSP in Tomcat6 所建议的那样 但错误定义没有变化!

在另一个网站上,它说为要加载的类创建一个公共构造函数。 我不知道这对我有什么帮助,我什至尝试过这也不起作用!

请帮忙...

按照要求添加我的整个代码的 sn-ps :

int s_id = session.getAttribute("s_id") != null ? ((Integer) session.getAttribute("s_id")).intValue() : 0 ;
String Pending = "Pending";
<jsp:useBean id="link" scope="application" class = "myPackage.Shop" />
myPackage.Shop s = new myPackage.Shop();
int g_orderQuantity=0;
Statement stmt6 = con.createStatement();
// a shop can select values from Order table
ResultSet rs3 = stmt6.executeQuery("SELECT * FROM ShopSystem.Order where s_id="+s_id+"     AND  status='"+Pending+"'");
//code to declare some Vectors
while(rs3.next())
{   
    status1.add((i),rs3.getString("status"));
    status=status1.elementAt(i);        
    // in the above way am adding ResultSet values to the vector and then copying it to string.
    //i maintains the total count of all my orders
    i++;
}
for(int j=0; j<i; j++)
{
     //checking if the order table is empty
    if((g_quantityRequired == 0)||(Cust_address.equals(null))||(email.equals(null))||((contact.equals(null)))||(total_amount == 0))
    {           %>          <br> There are no pending orders. <br>           
        <%
        %>          <form name="Back" method="post" action="ShopMenu3.jsp">
        <input type="submit" value="Back">
        </form><br><br>         <%
                }        
    // Now that the order table is not empty continue to process the order..
    int g_available=0;
    //check availability of g_id in s_id
    boolean b_availability = s.checkAvailability_perShop(s_id, g_id, g_quantityRequired);
            //if sufficient quantity is not available then place an order
    if(b_availability == false)
    {           // example, if required=50, then order is placed for 100 items
        // but if required is 150, then order is placed for 150 items
        g_orderQuantity = (100>g_quantityRequired)?100 : (g_quantityRequired+100);          
        //placing order and updating the Grocery_perShop table return the amount available of g_id in s_id
        g_available = s.placeOrder_perShop(s_id, g_id, g_orderQuantity);
    }        %> 
        New g_avaliable = <%= g_available %>. The value has been updated in the database.
    <%        //now that sufficient g_id is available with s_id, we execute the order
    //updating the Grocery_perShop table
    g_available = g_available - g_quantityRequired;
    //code to insert updated values in table

sc.close();
%>    <form name="Back" method="post" action="ShopMenu3.jsp">
<input type="submit" value="Back">
</form><br><br> 

【问题讨论】:

  • 注:s_id代表shop id,g_id代表grocery id,所有以“g”开头的变量代表Grocery,“s”代表Shop
  • 尽量避免使用 Scriplet。

标签: java eclipse jsp import


【解决方案1】:

如果您尝试访问的类位于应用程序的mypackage 下,并且您拥有 jsp 文件,则以下语句应该可以工作,

<jsp:useBean id="link" scope="application" class = "mypackage.Shop" />

在完成构建后检查您是否有文件。在使用 eclipse 时检查 build->classes。

【讨论】:

  • 它说“未定义类型:myPackage.Shop”。我已将上述行放在所有其他相关导入语句的下方和 html 标记开始的位置上方
  • 请分享相关代码以使其清晰,尽量避免使用scriplet。
  • 哦,是的,我摆脱了未定义的类型错误,bt 现在得到以下错误:异常 org.apache.jasper.JasperException: /ShopViewOrder.jsp(14,0) 的值useBean 类属性 myPackage.Shop 无效。
  • 检查 bean 类的名称
【解决方案2】:

您必须为 &lt;jsp:useBean&gt; 中的类使用完全限定名称。 import 页面指令不被 &lt;jsp:useBean&gt; 考虑

<jsp:useBean id="link" scope="application" class = "mypackage.Shop" />

直接来自官方Oracle Documentation - Creating and Using a JavaBeans Component

为类属性提供的值必须是fully qualified class name

请注意,bean 不能在未命名的包中。因此值的格式必须是package-name.class-name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 2017-03-24
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多