org.apache.jasper.JasperException:无法为JSP编译类:

时间:2013-07-08 01:30:36

标签: java jsp tomcat

您好我正在尝试使用tomcat编译一个简单的jsp文件,但我一直有这个错误消息

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 13 in the jsp file: /WebContent/test.jsp
Member cannot be resolved to a type
10: <body>
11: <%  
12:     //MyBatisDao myDao = new MyBatisDao();
13:     List<Member> list; // = myDao.func("test");
14:     //out.println("Result list= " + list);
15: %>
16: </body>

test.jsp(在Webcontent上)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="pageNumber.*, java.util.*, java.io.*" %>
<!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>Insert title here</title>
</head>
<body>
<%  
    List<Member> list;
%>
</body>
</html> 

Member.java

package pageNumber;
public class Member {
 private String id;
 private String pw;
 private int point;
 private int lvl;

 public Member() {}
 public Member(String id, String pw, int point, int lvl) {
  this.id = id;
  this.pw = pw;
  this.point = point;
  this.lvl = lvl;
 }

 public String getId() { return id; }
 public void setId(String id) { this.id = id; }
 public String getPw() { return pw; }
 public void setPw(String pw) { this.pw = pw; }
 public int getPoint() { return point; }
 public void setPoint(int point) { this.point = point; }     
 public int getLvl() { return lvl; }
 public void setLvl(int lvl) { this.lvl = lvl; }
 public String toString() {
  return "ID= " + id + ", PW= " + pw + ", POINT= " + point + ", LVL= " + lvl;
 }

}

据我所知,我遇到了这个问题,因为我的tomcat无法找到类文件, 所以我把我的班级文件放在了

  • build / classes / pageNumber(其中pageNumber是我的包名)
  • WEB-INF /类/ PAGENUMBER
  • 的Tomcat \工作\卡塔利娜\ localhost_ \组织\阿帕奇\ JSP \ PAGENUMBER

但没有一个成功。 有什么帮助吗?


编辑: 从

更改导入声明后
<%@ page import="pageNumber.*, java.util.*, java.io.*" %>

<%@ page import="java.util.List, pageNumber.Member" %>

它实际上给了我一种新的错误:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 15 in the generated java file
Only a type can be imported. pageNumber.Member resolves to a package

An error occurred at line: 12 in the jsp file: /WebContent/test.jsp
Member cannot be resolved to a type
9: </head>
10: <body>
11: <%  
12:     List<Member> list; // = myDao.func("test");
13: %>
14: </body>
15: </html> 

6 个答案:

答案 0 :(得分:11)

这可能是由jar冲突引起的。删除servlet / WEB-INF /目录中的servlet-api.jar,%Tomcat home%/ lib已经有了这个lib。

答案 1 :(得分:1)

问题是由于您需要在JSP中导入pageNumber.Member类而引起的。确保还包括其他包和类,如java.util.List

<%@ page import="pageNumber.*, java.util.*" %>

但是,在JSP中使用scriptlet 会遇到一个主要问题。请参阅How to avoid Java Code in JSP-Files?并开始练习ELJSTL,并将更多精力放在MVC解决方案上。

答案 2 :(得分:0)

将您的会员类别包含在您的jsp中:

<%@ page import="pageNumber.*, java.util.*, java.io.*,yourMemberPackage.Member" %>

答案 3 :(得分:0)

无需手动将类文件放在Tomcat上。只需确保Member的包声明正确定义为

package pageNumber;

因为,这是您在JSP中导入的唯一应用程序包。

<%@ page import="pageNumber.*, java.util.*, java.io.*" %>

答案 4 :(得分:0)

你的这一行:

<%@ page import="pageNumber.*, java.util.*, java.io.*" %>

@之前需要%符号,如下所示:

<%@ page import="pageNumber.*, java.util.*, java.io.*" @%>

答案 5 :(得分:0)

我正面临这个问题,我发现Apache Tomcat目录中存在较旧的ecj库 1)从Apache Tomcat库中删除旧的jar 2)清理项目 3)建立它。 它开始按预期工作。