1.TestBean.java源代码如下:
<%@ page import="test.TestBean" %>
<html>
<body>
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;
}
}
将编译好的class文件放在%CATALINA_HOME%\webapps\myapp\WEB-INF\classes\test下,也就是
%CATALINA_HOME%\webapps\myapp\WEB-INF\classes\test\TestBean.class
2.web.xml文件如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>My Web Application</display-name>
<description>
A application for test.
</description>
<servlet>
<servlet-name>Test</servlet-name>
<display-name>Test</display-name>
<description>A test Servlet</description>
<servlet-class>test.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
3.调用的JSP文件:
<%@ page import="test.TestBean" %>
<html>
<body>
<%
TestBean testBean=new TestBean("This is my test java bean.");
%>
Java bean name is: <%=testBean.getName()%>
</body>
</html>
成功,可为什么下面的不成功呢?<%@ page import="test.TestBean" %>
<html>
<body>
<jsp:useBean id="testBean" class="test.TestBean" />
<%=testBean.getName()%>
</body>
</html>
提示:org.apache.jasper.JasperException: /TestBean.jsp(6,0) The value for the useBean class attribute test.TestBean is invalid.分不够可以再加!
<%@ page import="test.TestBean" %>
<html>
<body>
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;
}
}
将编译好的class文件放在%CATALINA_HOME%\webapps\myapp\WEB-INF\classes\test下,也就是
%CATALINA_HOME%\webapps\myapp\WEB-INF\classes\test\TestBean.class
2.web.xml文件如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>My Web Application</display-name>
<description>
A application for test.
</description>
<servlet>
<servlet-name>Test</servlet-name>
<display-name>Test</display-name>
<description>A test Servlet</description>
<servlet-class>test.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
3.调用的JSP文件:
<%@ page import="test.TestBean" %>
<html>
<body>
<%
TestBean testBean=new TestBean("This is my test java bean.");
%>
Java bean name is: <%=testBean.getName()%>
</body>
</html>
成功,可为什么下面的不成功呢?<%@ page import="test.TestBean" %>
<html>
<body>
<jsp:useBean id="testBean" class="test.TestBean" />
<%=testBean.getName()%>
</body>
</html>
提示:org.apache.jasper.JasperException: /TestBean.jsp(6,0) The value for the useBean class attribute test.TestBean is invalid.分不够可以再加!
public class TestBean{
private String name = null;
public TestBean() {
}
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;
}}javabean要定义一个无参数的构造函数
have a try!!
加上
<jsp:setProperty name="testBean" property="*"/>
--
该错误是指 JSP 中给定的 useBean 标签的 class 属性的值无效
错误可能:
1. 在编译 JSP 时(不是运行时),指定的 Bean 类没找到
2. Bean 虽然找到了,但是它不是 public 的,或者找到的 class 文件是 interface 或抽象类
3. Bean 类中没有 public 的默认构建函数
第三点中需要注意的是,如果你的 Bean 类没有提供任何构造函数,将自动生成一个默认构建函数,。但是,如果你有构造函数,则不会自动生成该默认构造函数,
而<jsp:useBean>是不接受带参数的构造函数的,你可以通过setProperty 来传递参数值