JSP导入的类实现不解析类型

时间:2010-11-03 05:31:26

标签: java jsp tomcat

我在Tomcat中遇到一个异常,说明如下:

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

An error occurred at line: 6 in the generated java file
Only a type can be imported. business.User resolves to a package

An error occurred at line: 12 in the jsp file: /join_email_list.jsp
User cannot be resolved to a type
9:     <body>
10:         <%@ page import="business.User" %>
11:         <%
12:             User user = (User) request.getAttribute("user");
13:             String message = (String) request.getAttribute("message");
14: 
15:             if (user == null)


An error occurred at line: 12 in the jsp file: /join_email_list.jsp
User cannot be resolved to a type
9:     <body>
10:         <%@ page import="business.User" %>
11:         <%
12:             User user = (User) request.getAttribute("user");
13:             String message = (String) request.getAttribute("message");
14: 
15:             if (user == null)


An error occurred at line: 17 in the jsp file: /join_email_list.jsp
User cannot be resolved to a type
14: 
15:             if (user == null)
16:             {
17:                 user = new User();
18:             }
19:             if (message == null)
20:             {


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

要指出的一些事项:

第一:

An error occurred at line: 6 in the generated java file
Only a type can be imported. business.User resolves to a package

这是不准确的用户是业务包中的一个类。

第二

User user = (User) request.getAttribute("user");

用户显然是一种类型,在NetBeans ctrl + space中会显示所有类属性和方法。

这是User类:

package business;

public class User
{
    private String firstName;
    private String lastName;
    private String emailAddress;

    public User()
    {
        firstName = "";
        lastName = "";
        emailAddress = "";
    }

    public User(String first, String last, String email)
    {
        firstName = first;
        lastName = last;
        emailAddress = email;
    }

    public void setFirstName(String f)
    {
        firstName = f;
    }

    public String getFirstName()
    { 
        return firstName; 
    }

    public void setLastName(String l)
    {
        lastName = l;
    }

    public String getLastName()
    { 
        return lastName; 
    }

    public void setEmailAddress(String e)
    {
        emailAddress = e;
    }

    public String getEmailAddress()
    { 
        return emailAddress; 
    }
}

我该如何解决这个问题?似乎没有任何错误。感谢。

2 个答案:

答案 0 :(得分:2)

尝试清理项目(重建),好像你有一个不同于你想象的.class文件。

另一个机会是服务器有脏或过时的编译文件,也清理服务器并重新部署。

或许该课程在Netbeans的另一个项目中。本指南可以帮助您解决另一个相关问题:

http://www.lady4j.com/webClient.jsp?q=classnotfoundexception

答案 1 :(得分:2)

这种JSP错误具有误导性。这实际上是一个NoClassDefFoundError

换句话说,/WEB-INF/classes/business/User.class缺失。