JavaBean 'value for the useBean class attribut

2019-02-19 15:25发布

问题:

This question already has an answer here:

  • JasperException: The value for the useBean class attribute is invalid 6 answers

I have two JSPs and a JavaBean that aren't working. I'm using Tomcat 6.0. The first JSP is GetName.jsp, located at C:\Tomcat\webapps\app1\GetName.jsp:

<HTML>
<BODY>
<FORM METHOD=POST ACTION="NextPage.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

The second JSP is NextPage.jsp, located at C:\Tomcat\webapps\app1\NextPage.jsp:

<jsp:useBean id="user" class="classes.UserData" scope="session"/> 
<HTML>
<BODY>
You entered<BR>
Name: <jsp:getProperty name="user" property="username" /><BR>
Email: <jsp:getProperty name="user" property="email" /><BR>
Age: <jsp:getProperty name="user" property="age" /><BR>
</BODY>
</HTML>

My JavaBean, UserData, compiles correcty, and the class file is located at C:\Tomcat\webapps\app1\WEB-INF\classes:

package classes;

import java.io.Serializable;

public class UserData implements Serializable {
    String username;
    String email;
    int age;

    public UserData() {
    }

    public void setUsername( String value )
    {
        username = value;
    }

    public void setEmail( String value )
    {
        email = value;
    }

    public void setAge( int value )
    {
        age = value;
    }

    public String getUsername() { return username; }

    public String getEmail() { return email; }

    public int getAge() { return age; }
}

I also have the following in my web.xml file, located at C:\Tomcat\webapps\app1\WEB-INF:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
</web-app>

My Google searches have suggested something to do with the classpath. My classpath is currently .;C:\Tomcat\lib\servlet-api.jar.

When I enter information in GetName.jsp and click on the button, Tomcat gives me the following for NextPage.jsp:

org.apache.jasper.JasperException: /NextPage.jsp(1,1) The value for the useBean class attribute classes.UserData is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1203)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1160)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2399)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3365)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:315)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I could swear I'm doing everything right, but apparently I'm not. Could someone please tell me what's wrong before I tear all my hair out? Thanks in advance.

回答1:

You need to set the bean properties in your NextPage.jsp file.

Add the following line after your useBean statement like this.

<jsp:useBean id="user" class="UserData" scope="session"/>
<jsp:setProperty name="user" property="*" /> 


回答2:

The problem is that your UserData.class file is in wrong directory, which means UserData.class should actually be in webapps/app1/WEB-INF/classes/classes/UserData.class.



回答3:

After compiling the java file "UserData.java",you will get one class file="UserData.class".Make sure that the UserData.class is located in User under class. ie the location of the file UserData.java and UserData.class must be-

..\Tomcat6.0\webapps\ROOT\WEB-INF\classes\UserData.java and ..\Tomcat6.0\webapps\ROOT\WEB-INF\classes\UserData.class

If "classes" is not present in WEB-INF,you can create a new folder named "classes" to store these files.Restart the tomcat to view changes.



回答4:

The problem appears to be your referencing classes as a package. It is not, it is the root directory of the classes in the war.

remove the package line from your class, and refer to your bean as class="UserData" and see what happens.