Cle?fai 2020-12-11 18:19 采纳率: 100%
浏览 71
已采纳

(jsp)在jsp文件里调用编写好的javaBean报错

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();

            }

  } }}

  • 写回答

1条回答 默认 最新

  • IT•軒轅 企业官方账号 2020-12-11 18:28
    关注

    需要import吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作