我试图了解 Javabeans 是如何工作的,所以我创建了一个简单的 JSP 项目,其中包含InfoRet.java
(Javabean)和一个用于检索值的JSP文件来自豆子。
这是代码:
public class InfoRet implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public InfoRet() {
}
public String getname() {
return this.name;
}
public int getage() {
return this.age;
}
public void setname(String name) {
this.name = name;
}
public void setage(int age) {
this.age = age;
}
}
这是JSP文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<jsp:useBean id="obj" class="InfoRet" scope="session"></jsp:useBean>
</head>
<body>
<jsp:setProperty name="obj" property="name" value="harshal" />
<jsp:setProperty name="obj" property="age" value="323" />
<jsp:getProperty name="obj" property="name" />
<br/>
<jsp:getProperty name="obj" property="age" />
</body>
</html>
错误是:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 8 in the jsp file: /indexs.jsp
InfoRet cannot be resolved to a type
5: <head>
6: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7: <title>Insert title here</title>
8: <jsp:useBean id="obj" class="InfoRet" scope="session" ></jsp:useBean>
9: </head>
10: <body>
11: <jsp:setProperty name="obj" property="name" value="harshal"/>
An error occurred at line: 8 in the jsp file: /indexs.jsp
InfoRet cannot be resolved to a type
5: <head>
6: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7: <title>Insert title here</title>
8: <jsp:useBean id="obj" class="InfoRet" scope="session" ></jsp:useBean>
9: </head>
10: <body>
11: <jsp:setProperty name="obj" property="name" value="harshal"/>
An error occurred at line: 8 in the jsp file: /indexs.jsp
InfoRet cannot be resolved to a type
5: <head>
6: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7: <title>Insert title here</title>
8: <jsp:useBean id="obj" class="InfoRet" scope="session" ></jsp:useBean>
9: </head>
10: <body>
11: <jsp:setProperty name="obj" property="name" value="harshal"/>
An error occurred at line: 14 in the jsp file: /indexs.jsp
InfoRet cannot be resolved to a type
11: <jsp:setProperty name="obj" property="name" value="harshal"/>
12: <jsp:setProperty name="obj" property="age" value="323" />
13:
14: <jsp:getProperty name="obj" property="name"/>
15: <br/>
16: <jsp:getProperty name="obj" property="age"/>
17: </body>
An error occurred at line: 14 in the jsp file: /indexs.jsp
InfoRet cannot be resolved to a type
11: <jsp:setProperty name="obj" property="name" value="harshal"/>
12: <jsp:setProperty name="obj" property="age" value="323" />
13:
14: <jsp:getProperty name="obj" property="name"/>
15: <br/>
16: <jsp:getProperty name="obj" property="age"/>
17: </body>
An error occurred at line: 16 in the jsp file: /indexs.jsp
InfoRet cannot be resolved to a type
13:
14: <jsp:getProperty name="obj" property="name"/>
15: <br/>
16: <jsp:getProperty name="obj" property="age"/>
17: </body>
18: </html>
An error occurred at line: 16 in the jsp file: /indexs.jsp
InfoRet cannot be resolved to a type
13:
14: <jsp:getProperty name="obj" property="name"/>
15: <br/>
16: <jsp:getProperty name="obj" property="age"/>
17: </body>
18: </html>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
所以我想知道上面代码的问题是什么。
答案 0 :(得分:3)
我想您的问题是您的班级InfoRet
位于默认包中(也就是说,它不在您创建的命名包中)。默认包中的类对于命名的包中的其他类是不可见的(就像您的JSP一样)。
所以你必须将你的课程安排在一个包中:
package com.your.package;
public class InfoRet implements java.io.Serializable {
//...
}
然后在JSP中使用它:
<jsp:useBean id="obj" class="com.your.package.InfoRet" scope="session" />
答案 1 :(得分:1)
是的,你必须创建一个单独的java包,然后在该包中创建一个新的java类。
之后只需在<jsp:useBean class="yournewpackage.yourclassname">