我只是在玩玩JSP。我只是想测试一些<jsp:useBean>的东西,但是我做不到,每次我使用<jsp:useBean>的时候,我都会收到一个错误。即使我只有这个,我也会得到一个错误:
<%@ 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>没有<jsp:useBean>,它运行得很好。使用<jsp:useBean>时,我得到如下错误:
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、Tomcat7.0.23和Java 1.7.0_01。
有什么想法吗?
PS:我必须将端口8xxx更改为9xxx,因为oracle DB使用的是标准的8xxx。但这可能不是问题的原因。
发布于 2011-12-01 00:36:06
您需要将类放在包中,以便能够在其他类中使用它们。缺省包中的类对于包中本身的类是不可见的(就像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>呢?或者它实际上代表了单个用户?然后应该将其命名为User。
https://stackoverflow.com/questions/8329451
复制相似问题