一个index.jsp一个bean包里的pagingbean.java。
PS:求别吐槽这个分页做法。。。。。或者帮我改完,在吐槽也成啊
测试pagingbean.java完全没问题。。。
package bean;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class pagingbean {
private final int size=4;//控制每页显示几条数据;
private int countPage;//计算总页数
private ArrayList<String> list;//存储读取的内容
private int index;//根据当前第几页,获取list中的索引 index开始到index+size-1结束
//将每一行的内容存入list中,并获取总页数。
public void saveContentInList()
{
FileReader file;
try {
file = new FileReader("WebRoot/File/book.txt");//FileNotFoundException e
//file = new FileReader("File/book.txt");
BufferedReader buff=new BufferedReader(file);//主要是利用它的readline();
String buffer=null;
list=new ArrayList<String>();
while((buffer=buff.readLine())!=null)
{
//将buffer加入集合当中
//排除空行!排除只有空格!isEmpty从1.6版本开始。
/*if(!buffer.trim().isEmpty())
{
count++;
}*/
if(!buffer.trim().equals(""))
{
list.add(buffer);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
//获取总页数
public int getAllPages()
{
countPage=list.size()/size;
if(list.size()%size>0)
{
countPage=list.size()/size+1;
}
return countPage;
}
//根据当前第几页,获取list中的索引 index开始到index+size-1结束
public void getIndextInList(int nowPage)
{
index=(nowPage-1)*size;
}
//测试
public static void main(String[] args) {
pagingbean pgb=new pagingbean();
pgb.saveContentInList();
int pages=pgb.getAllPages();
System.out.println("ppppp"+pages);
//pgb.list;
System.out.println(pgb.list);
System.out.println(pgb.countPage);
pgb.getIndextInList(2);
System.out.println(pgb.index);
}
}
在jsp里却报错,提示 找不到文件 以及空指针
<body> <jsp:useBean id="pagetool" scope="page" class="bean.pagingbean"></jsp:useBean> This is my JSP page. <br> <% pagetool.saveContentInList(); //获取总页数 int pages=pagetool.getAllPages(); %> 当前共有<%=pages%>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
你在web容器里调用<spanstyle="color:#0000FF;font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:14.65625px;background-color:#F8F8F8;">WebRoot/File/book.txt这个路径好像不对<divclass="ref">
<spanstyle="color:#0000FF;font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;font-size:12px;line-height:14px;background-color:#F8F8F8;">这个路径Main方法能用不代表JSP能用,
JSP有自Context上下文,路径是在这里解析的,这个肯定是找不到文件
<spanstyle="color:#0000FF;font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;font-size:12px;line-height:14px;background-color:#F8F8F8;">这个路径Main方法能用不代表JSP能用,
JSP有自Context上下文,路径是在这里解析的,这个肯定是找不到文件
包名?
路径很有可能是/nameofpakage/<spanstyle="font-family:Verdana,sans-serif,宋体;font-size:14px;line-height:normal;background-color:#FFFFFF;">File/book.txt
<divclass="ref">包名?
路径很有可能是/nameofpakage/<spanstyle="font-family:Verdana,sans-serif,宋体;font-size:14px;line-height:normal;background-color:#FFFFFF;">File/book.txt
<divclass="ref">