jsp文件里想要通过useBean tag调用编写好的javaBean实现读取txt文件内容时,在
application.setAttribute("bean",new FileBean(path));
}
FileBean fb=
(FileBean)application.getAttribute("bean");
这几句话报错,都是报编译错误,FileBean cannot be resolved to a type
实验课学习javaBean怎么用,所以还没太明白,希望有大神不吝赐教。
具体代码在下面
jsp文件如下:
<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="fb" class="jikken4.FileBean" scope="application"/>
<%
if(application.getAttribute("bean")==null) {
String path=application.getRealPath("WEB-INF/id_data.txt");
application.setAttribute("bean",new FileBean(path));
}
FileBean fb=
(FileBean)application.getAttribute("bean");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; utf-8">
<title>FILE BEANS</title>
</head>
<body>
※データの表示<br>
<textarea cols="40" rows="10"><%=fb.getData() %>
</textarea>
</body>
</html>
javaBean文件如下:
package jikken4;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileBean {
private String fname = null;
private String data = null;
public FileBean(){ this("Jikken4/WebContent/WEB-INF/id_data.txt"); }
public FileBean(String s){
this.fname = s;
this.loadData(); }
public String getData(){
return this.data; }
public void setData(String s){
this.data = s; }
public void loadData(){
FileReader reader = null;
BufferedReader breader = null;
try {
reader = new FileReader(this.fname);
breader = new BufferedReader(reader);
String tmp = "";
String result = "";
while((tmp = breader.readLine()) != null){
result += tmp + "\n";
}
this.data = result;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
} }}