无法使用: “Bean 无法解析为类型”

发布于 2024-12-18 23:27:36 字数 1576 浏览 3 评论 0原文

我只是在玩JSP。我只是想测试一些 东西,但我不能。每次如果我使用 ,我都会收到错误。即使我只有这个,我也会收到一个错误:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
    <title>Test</title>
  </head>
  <body>
    <jsp:useBean id="mybean" class="Users" scope="session" >
      <jsp:setProperty name="mybean" property="name" value="Hello world" />
    </jsp:useBean>
  </body>
</html>

没有 它运行正常。使用 我收到如下错误:

Servlet.service() for servlet [jsp] in context with path [/JSPTest] threw exception [Unable to compile class for JSP: 

An error occurred at line: 10 in the jsp file: /index.jsp
Users cannot be resolved to a type
7:     <title>Insert title here</title>
8:   </head>
9:   <body>
10:     <jsp:useBean id="mybean" class="Users" scope="session" >
11:       <jsp:setProperty name="mybean" property="name" value="Hello world" />
12:     </jsp:useBean>
13:   </body>

我正在使用 Eclipse、Tomcat 7.0.23 和 Java 1.7.0_01。

有什么想法吗?

PS:我不得不将端口8xxx更改为9xxx,因为oracle DB使用标准8xxx。但这可能不是问题的原因。

I'm just playing around with JSP. I just wanted to test some <jsp:useBean> stuff, but I can't. Every time if I'm using <jsp:useBean>, I get an error. Even if I just have this, I get an error:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
    <title>Test</title>
  </head>
  <body>
    <jsp:useBean id="mybean" class="Users" scope="session" >
      <jsp:setProperty name="mybean" property="name" value="Hello world" />
    </jsp:useBean>
  </body>
</html>

Without the <jsp:useBean> it runs fine. With the <jsp:useBean> I get an error like:

Servlet.service() for servlet [jsp] in context with path [/JSPTest] threw exception [Unable to compile class for JSP: 

An error occurred at line: 10 in the jsp file: /index.jsp
Users cannot be resolved to a type
7:     <title>Insert title here</title>
8:   </head>
9:   <body>
10:     <jsp:useBean id="mybean" class="Users" scope="session" >
11:       <jsp:setProperty name="mybean" property="name" value="Hello world" />
12:     </jsp:useBean>
13:   </body>

I am using Eclipse, Tomcat 7.0.23 and Java 1.7.0_01.

Any ideas?

PS: I had to change the port 8xxx to 9xxx because the oracle DB is using the standard 8xxx. But that's likely not the cause of the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2024-12-25 23:27:36

您需要将类放入包中,以便能够在其他类中使用它们。默认包中的类对于包中的类是不可见的(就像 JSP 最终的结果)。

因此,为 Users 类提供一个 package,如下所示:

package com.example;

public class Users {
    // ...
}

重新编译并将其放入 /WEB-INF/classes/com/example/Users.class

然后您可以按如下方式引用它:

<jsp:useBean id="myBean" class="com.example.Users" />

与具体问题无关,使用复数作为实体的类名通常是一种味道。该类的单个实例真的代表多个用户吗?例如,为什么您没有 List ?或者它实际上代表单个用户?然后应将其命名为 User

You need to put classes in a package in order to be able to use them in another classes. Classes in the default package are invisible to classes which are by itself in a package (like as the JSP would end up).

So, give the Users class a package like so:

package com.example;

public class Users {
    // ...
}

Recompile and put it in /WEB-INF/classes/com/example/Users.class.

Then you can reference it as follows:

<jsp:useBean id="myBean" class="com.example.Users" />

Unrelated to the concrete problem, having a plural as class name of an entity is usually a smell. Does a single instance of that class really represent multiple users? Why would you not have a List<User> for example? Or does it actually represent a single user? It should then be named User instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文