called class in jsp
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">
5: <jsp:useBean id="user" class="Data" scope="session"/>
6: <jsp:setProperty name="user" property="*"/>
7: <html>
8: <head>
public class Data
{
Stri开发者_StackOverflowng 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....
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" %>
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:
- Beginning and intermediate JSP/Servlet tutorials.
精彩评论