调试了一个下午,就是找不出原因来
类文件:
package test;
public class TestBean{
private String name = null;
public TestBean(String strName_p){
this.name=strName_p;
}
public void setName(String strName_p){
this.name=strName_p;
}
public String getName(){
return this.name;
}
}
jsp文件:
<html><body>
<jsp:useBean id="HW" scope="request" class="test.TestBean"/>
<center>
<%
HW.setName("Hello World.");
%>
Java bean name is: <%=HW.getName()%>
</center></body></html>
这是我编写的一个调试JAVABEAN的程序,我已经把类文件编译后生成的class文件放到../WEB-INF/test/TestBean.class里面了,环境变量又已经设定正确了,
如果用
<%@ page import="test.TestBean" %>
<html><body><center>
<%
TestBean testBean=new TestBean("This is a test java bean.");
%>
Java bean name is: <%=testBean.getName()%>
</center></body></html>
来调试就能正确显示,但用上面的<jsp:useBean>就不行,唉,不知哪里出了问题,显示的错误是这样:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /TestBean.jsp(3,0) The value for the useBean class attribute test.TestBean is invalid.
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
org.apache.jasper.JasperException: /TestBean.jsp(3,0) The value for the useBean class attribute test.TestBean is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:146)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1174)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3304)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:303)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
到底哪里出错了呢?请各位指点一下,感谢~~~~
----------------解决方案--------------------------------------------------------
谁能指点一下,我又搞了一个下午,头都大了感激啊~~~~~
----------------解决方案--------------------------------------------------------
<%
HW.setName("Hello World.");
%>
这句替换成
<jsp:setProperty name="HW" property="name" value="vlinux"/>
<%=HW.getName()%>
替换成
<jsp:getProperty name="HW" property="name"/>
----------------解决方案--------------------------------------------------------
应该不是替换的问题
而是你的TestBean这个类,是不是定义了默认的构造函数
我看到你初始化的时候是用字符串做为参数来初始化的
其实可以用<jsp:useBean>的地方都可以直接用JAVA语句来实现
----------------解决方案--------------------------------------------------------
哦,是不是TestBean应该有
public TestBean(){}
构造函数?
----------------解决方案--------------------------------------------------------
感谢楼上两位版主,我解决问题了,原来TestBean类真的要有一个不带参数的默认的构造函数,把JAVA程序改成这样就能通过了:
package test;
public class TestBean{
private String name;
public TestBean(){
this.name="";
}
public void setName(String strName_p){
this.name=strName_p;
}
public String getName(){
return this.name;
}
}
谢谢两位热心的帮助,我爱bc-cn~~~~~~~~~~~~~
----------------解决方案--------------------------------------------------------