JSP中调用Bean出现的问题。100分,不成敬意,望有识之士笑纳 (100分)

  • 主题发起人 主题发起人 黑衣人
  • 开始时间 开始时间

黑衣人

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟使用Tomcat 4.1.24,编辑器用NotePad,写了个BEAN,编译好的类文件放在WEB-INF/classes目录下,bean内容如下:
//------------------bean-------------------//
public class FirstBean
{ private String UserName = "";
private String UserPwd = "";
public void FirstBean()
{}
public void setUserName(String name)
{ this.UserName = name;
}
public void setUserPwd(String pwd)
{ this.UserPwd = pwd;
}
public String getUserName()
{ return UserName;
}
public String getUserPwd()
{ return UserPwd;
}
}
//--------------bean----------------//
然后在Jsp文件中调用,JSP文件内容如下:
//--------------JSP----------------//
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head><title>调用JAVABEAN</title>
</head>
<body>
<jsp:useBean id="fb" scope="session" class="FirstBean" />
<jsp:setProperty name="fb" property="userName" value="111" />
<jsp:setProperty name="fb" property="userPwd" value="111" />
<hr>
<jsp:getProperty name="fb" property="userName" />
<jsp:getProperty name="fb" property="userPwd" />
</body>
</html>
//--------------JSP----------------//
运行时报错,错误如下:
//--------------error---------------//
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] C:/Program Files/Tomcat/work/Standalone/localhost/_/UseBean0_jsp.java:49: cannot resolve symbol
[javac] symbol : class FirstBean
[javac] location: class org.apache.jsp.UseBean0_jsp
[javac] FirstBean fb = null;
[javac] ^
[javac] C:/Program Files/Tomcat/work/Standalone/localhost/_/UseBean0_jsp.java:51: cannot resolve symbol
[javac] symbol : class FirstBean
[javac] location: class org.apache.jsp.UseBean0_jsp
[javac] fb = (FirstBean) pageContext.getAttribute("fb", PageContext.SESSION_SCOPE);
[javac] ^
[javac] C:/Program Files/Tomcat/work/Standalone/localhost/_/UseBean0_jsp.java:54: cannot resolve symbol
[javac] symbol : class FirstBean
[javac] location: class org.apache.jsp.UseBean0_jsp
[javac] fb = (FirstBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "FirstBean");
[javac] ^
[javac] C:/Program Files/Tomcat/work/Standalone/localhost/_/UseBean0_jsp.java:71: cannot resolve symbol
[javac] symbol : class FirstBean
[javac] location: class org.apache.jsp.UseBean0_jsp
[javac] out.print(JspRuntimeLibrary.toString((((FirstBean)pageContext.findAttribute("fb")).getUserName())));
[javac] ^
[javac] C:/Program Files/Tomcat/work/Standalone/localhost/_/UseBean0_jsp.java:73: cannot resolve symbol
[javac] symbol : class FirstBean
[javac] location: class org.apache.jsp.UseBean0_jsp
[javac] out.print(JspRuntimeLibrary.toString((((FirstBean)pageContext.findAttribute("fb")).getUserPwd())));
[javac] ^
[javac] 5 errors
//--------------error---------------//
这是怎么回事?在bean中一顶要用package吗?
 
>>public class FirstBean
>>{ private String UserName = "";
>> private String UserPwd = "";
>> public void FirstBean()
>> {}
构造函数没有返回值。
public class FirstBean
{ private String UserName;
private String UserPwd;
//默认构造器
public FirstBean()
{
this.UserName="";
this.UserPwd="";
}
 
还是报一样的错误,,,郁闷啊~~~~~
 
是否已经将FirstBean.java编译成FirstBean.class?
哦,sorry,没看清楚 :)
 
没错,Bean必须包含在某个package内,改一下
WEB-INF/classes下建一个test目录
bean:
package test;
public class FirstBean
{ private String UserName = "";
...
}
<%@ page language="java" import="test.*"%>
<%@ page contentType="text/html;charset=gb2312" %>
 
ft,,,编译了,错误一样,我看错误中提示resolve symbol:class FirstBean,是不是我的类没有用package包起来的原因?
 
在jsp文件开头
<%@ page import = "FirstBean" %>
 
to 黑衣人:JavaBean在jsp页面调用必须包含在某个package内,改改就行了
 
嚯嚯,,,搞定了,按delphifaq方法。
to:csz,按你说的方法结果报一大堆错误,可能是我使用的方法不对,现在再试试。
to:ZRWeng,我在书上看了好像没有说构造函数一定要有返回值的啊,我去掉返回值,按delphifaq的方法试试
马上回来给分
 
构造函数是没有返回值的
另,一个java原程序包含在一个包(package)中,在物理路径上应该有这个包名的文件夹
并且,java原程序必须在这个包名文件夹下.
 
各位同好,再问一个相关的的问题:
如果使用package的话,在jsp页面开头是不是这样写:
<%@ page import="MyBean.FirstBean" %>或者是这样写<%@ page import="MyBean.*" %>
然后在useBean时这样写:
<jsp:useBean id="fb" scope="session" class="MyBean.FirstBean" />
我发现引入package后出现的错误还多些了,,,,到底要怎么写呢?
 
bean的存放路径?
mybean/firstbean.class
 
对的,不过可能你别的地方错了
给你个例子吧,你照着做得了:
package user;
public class UserData
{
String logName,userName;
//构造函数
public UserData(){
}
//设置参数
public void setLogName(String value){
logName = value;
}
public void setUserName(String value){
userName = value;
}
public String getLogName(){
if (logName==null)
{
logName="";
}
return logName;
}
public String getUserName(){
if (userName==null)
{
userName="";
}
return userName;
}
}
}
JSP引用:
<%@page contentType="text/html;
charset=gb2312" language="java" errorPage="" %>
<%@page import="user.*"%>
<%request.setCharacterEncoding("gb2312");%>
<jsp:useBean scope="page" id="userdata" class="user.UserData" />
<jsp:setProperty name="userdata" property="*" />
提示:package名一般为小写,你必须把bean放在package内,package名就是.class文件所在的目录WEB-INF/classes目录下的user/UserData.class
 
就这样吧,多谢各位帮忙,小弟感激不尽,小小意思,了表心意,呵呵
 
望各位笑纳
 
后退
顶部