困扰一天的购物车问题!
我先写了一个购车车的类,然后在JSP中导入调用它,但是它竟然说无法识别那个Shopcart类,但我明明已经导入了啊!望兄弟们赐教,在此先谢过啦!
代码如下:
Shopcart.java :
package com.bookshop.bean;
public class Shopcart {
public int bookID;
public String bookName;
public String ISBN;
public float price;
public int number;
}
cart_add.jsp :
<%@ page contentType= "text/html;charset=GBK "language= "java " import= "java.util.*,java.sql.*,com.bookshop.bean.* " pageEncoding= "GBK "%>
<jsp:useBean id= "con " scope= "page " class= "com.bookshop.bean.connDB "/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<title> cart_add </title>
<%
request.setCharacterEncoding( "GBK ");
int bookID = Integer.parseInt(request.getParameter( "bookID "));
String sql = "select * from books where bookID= ' "+bookID+ " ' ";
ResultSet rs = con.executeQuery(sql);
float price = 0;
if(rs.next()){
price = rs.getInt( "price ");
}
Shopcart bookelement = new Shopcart();
bookelement.bookID = bookID;
bookelement.price = price;
bookelement.number = 1;
boolean flag = true;
Vector cart = (Vector)session.getAttribute( "cart ");
if(cart==null){
cart = new Vector();
}else{
for(int i=1;i <cart.size();i++){
Shopcart bookitem = (Shopcart)cart.elementAt(i);
if(bookitem.bookID==bookelement.bookID){
bookitem.number++;
cart.setElementAt(bookitem,i);
flag = false;
}
}
}
if(flag){
cart.addElement(bookelement);
session.setAttribute( "cart ",cart);
con.close();
out.println( " <script language= 'javascript '> alert( '已经添加至购物车! ');window.location.href= 'cart_see.jsp '; </script> ");
}
%>
</head>
<body>
</body>
</html>
出现的错误:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 9 in the jsp file: /bookshop/WebRoot/cart_add.jsp
Generated servlet error:
D:\Tomcat5.0\work\Catalina\localhost\test\org\apache\jsp\bookshop\WebRoot\cart_005fadd_jsp.java:73: 找不到符号
符号: 类 Shopcart
位置: 类 org.apache.jsp.bookshop.WebRoot.cart_005fadd_jsp
Shopcart bookelement = new Shopcart();
^
An error occurred at line: 9 in the jsp file: /bookshop/WebRoot/cart_add.jsp
Generated servlet error:
D:\Tomcat5.0\work\Catalina\localhost\test\org\apache\jsp\bookshop\WebRoot\cart_005fadd_jsp.java:73: 找不到符号
符号: 类 Shopcart
位置: 类 org.apache.jsp.bookshop.WebRoot.cart_005fadd_jsp
Shopcart bookelement = new Shopcart();
^
An error occurred at line: 9 in the jsp file: /bookshop/WebRoot/cart_add.jsp
Generated servlet error:
D:\Tomcat5.0\work\Catalina\localhost\test\org\apache\jsp\bookshop\WebRoot\cart_005fadd_jsp.java:83: 找不到符号
符号: 类 Shopcart
位置: 类 org.apache.jsp.bookshop.WebRoot.cart_005fadd_jsp
Shopcart bookitem = (Shopcart)cart.elementAt(i);
^
An error occurred at line: 9 in the jsp file: /bookshop/WebRoot/cart_add.jsp
Generated servlet error:
D:\Tomcat5.0\work\Catalina\localhost\test\org\apache\jsp\bookshop\WebRoot\cart_005fadd_jsp.java:83: 找不到符号
符号: 类 Shopcart
位置: 类 org.apache.jsp.bookshop.WebRoot.cart_005fadd_jsp
Shopcart bookitem = (Shopcart)cart.elementAt(i);
^
4 错误
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802
[解决办法]
Shopcart类加个构造函数试试。
[解决办法]
bean是有一系列规范的 譬如要有一个空的构造函数
bean的属性都设成private 然后每个属性都有对应的getXXX,setXXX方法。建议你去看下bean的规范
[解决办法]
<jsp:useBean id= "con " scope= "page " class= "com.bookshop.bean.Shopcart "/>
[解决办法]
compile错误,程序正常,问题出在import,用一个独立的import导入
[解决办法]
<%@ page contentType= "text/html;charset=GBK "language= "java " import= "java.util.*,java.sql.*,com.bookshop.bean.* " pageEncoding= "GBK "%>
分成
<%@ page contentType= "text/html;charset=GBK "language= "java " pageEncoding= "GBK%>
<%@ page import= "java.util.*,java.sql.*,com.bookshop.bean.* "%>
试试!
[解决办法]
Shopcart bookelement = new Shopcart();
改成com.bookshop.bean.Shopcart bookelement = new com.bookshop.bean.Shopcart();
[解决办法]
检查你的web服务器比如tomcat,weblogic等等下面是否有编译好的Shopcart.class文件.
[解决办法]
确认你的Shopcart.class 是在WEB-INF 目录下的classes 里面。
[解决办法]
首先你确认你的class里面有这个class文件,否则肯定是找不到,其次,你的数据库操作没有进行Exception捕获,如果你不能确认的话,可以重新便宜,至于怎么引用是没有关系的
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html