gpt4 book ai didi

java - error: Bean cannot be resolved to a type

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:52 24 4
gpt4 key购买 nike

我试图了解 Javabeans 是如何工作的,所以我创建了一个简单的 JSP 项目,其中包含 InfoRet.java (Javabean) 和一个JSP 文件以从 bean 中检索值。

这是代码:

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)

所以我想知道上面的代码有什么问题。

最佳答案

我猜你的问题是你的类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" />

关于java - <jsp :useBean> error: Bean cannot be resolved to a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17004913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com