tags:

views:

58

answers:

2

i create a class called "Data" and call it in JSP.. when i comple it, the error says,

Data cannot be resolved to a type
2: pageEncoding="ISO-8859-1"%>
3: <%@ page import= " Data" %>
4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
5: <jsp:useBean id="user" class="Data" scope="session"/> 
6: <jsp:setProperty name="user" property="*"/>
7: <html>
8: <head>

public class Data
{

 String Classname ;
        String Individualname;
        String Link;
        public  void Setclassname(String value)
 {
  Classname = value;

 }
        public String GetClassname()
 { 

  return Classname;

 }
}

jsp file

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

<body>

You entered<BR>
Class Name: <%= user.GetClassname() %><BR>


</body>

i didn't deploy the project....

+4  A: 

First of all, you are not following the beans naming conventions for the getters/setters and the fields naming.

it should be :

public class Data {

String classname ;
String individualName;
String link;

public void setClassname(String value) {classname = value;}
public String getClassname() {return classname;}
}

or maybe className but Classname casing suggest it is a class and not an attribute.

To retrieve the beans property you can try:

<jsp:getProperty name="user" property="classname"/>

Now about your issue, I think your page import is wrong:

The error says it does not recognize find type Data, which suggests your import isn't working. Sun recommends that you do not use default packages. Even IDEs will trigger a warning when you put classes in a default package.

<%@ page import= " Data" %>

try putting the fully qualified name of the Data class, i.e, com.package.Data

<%@ page import= "com.package.Data" %>
Bakkal
thanks for reply.. but still deal with the same error...when i changed the property in to class name , it says that org.apache.jasper.JasperException: Cannot find any information on property 'Data' in a bean of type 'Data'
what package is your `Data` class in?
Bakkal
i didn't create a package.. it's only a class file.. should i need to create a package? it's in the default package
thanks.. I Created a ONT package and put data class in it.. but same error..???<%@ page import= "Ont.Data" %>
`<jsp:useBean id="user" class="Ont.Data" scope="session"/>`
Bakkal
now this is a errorCannot find any information on property 'Data' in a bean of type 'Ont.Data'
+1  A: 

You don't need @page import. This is only linked into the scriptlet scope, not into taglibs. You don't want to use scriptlets here. The following should work:

<jsp:useBean id="user" class="com.example.Data" scope="session"/> 

Further, you should always put classes in a package whenever you'd like to import/reuse it in other classes. Packageless classes are not importable in other classes. Add a package declaration and put the class in the right location in the folder structure.

package com.example;

public class Data {
    // ...
}

When building manually, it should end up in /WEB-INF/classes/com/example/Data.class. When building using an IDE, like Eclipse, put it in src/com/example/Data.java and the IDE will worry about compiling and putting it in the right location.

You should also really fix the naming conventions of the variables and getters/setters. Follow the Java Naming Conventions.


Update as per the comments:

thanks... i did it what you said here.. still with the same issue.. now error says Cannot find any information on property 'Data' in a bean of type 'Ont.Data'

First: please, follow the Java Naming Conventions. Package names should not start with uppercase. Further, this error message means that there's no getter for the named property. You should also really follow the Javabean specifications. Property names should start with lowercase, e.g. propertyname and getter methods should be public and be named like so getPropertyname(), a get, then first letter of propertyname uppercased and the remnant exactly the same as original propertyname. The same applies for setters like so setPropertyname().

Here's a rewrite of your Data class:

package com.example;

public class Data {
    private String classname;
    private String individualname;
    private String link;

    public String getClassname() {
        return classname;
    }

    public String getIndividualname() {
        return individualname;
    }

    public String getLink() {
        return link;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public void setIndividualname(String individualname) {
        this.individualname = individualname;
    }

    public void setLink(String link) {
        this.link = link;
    }
}

Most IDE's, for example Eclipse, can autogenerate Javabeans in this flavor. Take benefit of it.

See also:

BalusC
thanks... i did it what you said here.. still with the same issue.. now error says Cannot find any information on property 'Data' in a bean of type 'Ont.Data'