对一篇新闻内容如何分页
我做显示新闻详细的时候,一篇新闻内容太长了,我想到把新闻内容进行分页,但是出现了一些问题,出现不了,以下我写的主要代码:
if (id != 0) {
message = messageManager.get(id); //根据id得到的新闻
if (content != null || content != "") {
int wordCount = 1000; // 1000字为一页
String tempContent = content; // 文章的全部内容
int t = tempContent.length();
if (t <= wordCount) { // 如果文章小于1000字,不用分页
cur_page = 1;
return tempContent;
}
String pageContent = tempContent.substring(0, wordCount); // 获得前1000字的内容
tempContent = tempContent.substring(wordCount); // 把tempContent改为1000字以后的内容
int index = tempContent.indexOf("\n"); // 换行符的位置
if (index == -1)
pageContent += tempContent; // 如果1000字后没有分段了,也不分了,当然也可以强制分页
String tempB = tempContent.substring(0, index); // 1000字后的第一个换行符前的内容
int totalPages = t % wordCount == 0 ? (t / wordCount)
: (t / wordCount + 1);
if(cur_page>totalPages){
cur_page=totalPages;
}
content+=tempB;
}
}
页面显示分页:新闻内容分页,恩:
<div class="STYLE4">
<input type="button" value="首页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page= <%=1%>"/>
<input type="button" value="上一页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page=${cur_page-1}'"/>
<input type="button" value="下一页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page=${cur_page+1}'" />
<input type="button" value="末页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page=${cur_page-1}'"/>
</div>
但是运行时候:java.lang.NullPointerException 说:int t = tempContent.length();有问题,我该怎么改,希望各位帮我提供思路,改改,谢谢啦!
if (id != 0) {
message = messageManager.get(id); //根据id得到的新闻
if (content != null || content != "") {
int wordCount = 1000; // 1000字为一页
String tempContent = content; // 文章的全部内容
int t = tempContent.length();
if (t <= wordCount) { // 如果文章小于1000字,不用分页
cur_page = 1;
return tempContent;
}
String pageContent = tempContent.substring(0, wordCount); // 获得前1000字的内容
tempContent = tempContent.substring(wordCount); // 把tempContent改为1000字以后的内容
int index = tempContent.indexOf("\n"); // 换行符的位置
if (index == -1)
pageContent += tempContent; // 如果1000字后没有分段了,也不分了,当然也可以强制分页
String tempB = tempContent.substring(0, index); // 1000字后的第一个换行符前的内容
int totalPages = t % wordCount == 0 ? (t / wordCount)
: (t / wordCount + 1);
if(cur_page>totalPages){
cur_page=totalPages;
}
content+=tempB;
}
}
页面显示分页:新闻内容分页,恩:
<div class="STYLE4">
<input type="button" value="首页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page= <%=1%>"/>
<input type="button" value="上一页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page=${cur_page-1}'"/>
<input type="button" value="下一页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page=${cur_page+1}'" />
<input type="button" value="末页" onclick="window.location.href='${ctx}/message/message!look.action?id=${id}&cur_page=${cur_page-1}'"/>
</div>
但是运行时候:java.lang.NullPointerException 说:int t = tempContent.length();有问题,我该怎么改,希望各位帮我提供思路,改改,谢谢啦!
...全文
相关推荐
JSP新闻上一篇下一篇及分页显示
分页不仅可以减少页面加载时间,还可以帮助用户更有序地浏览大量内容。在JSP中,可以通过传递当前页码作为请求参数,然后在Servlet或控制器中根据参数调整SQL查询,获取相应页码的新闻数据。2.
本科毕业论文---网站新闻管理系统-新闻管理系统.doc
本文档详细阐述了一篇关于本科毕业论文的网站新闻管理系统的设计与实现。
诚优V1.0asp长文章分页源码(根据字符分页)
在ASP中,处理长篇文章的分页是一个常见的需求,特别是在内容丰富的网站中,如博客、论坛或者新闻站点。"
分节符分页符分栏符的区别
这种布局方式通常用于新闻稿、杂志文章、报纸版面等,能够增加文档的视觉层次感和阅读舒适度。
网页设计中的交互:关于分页问题的浅谈
#### 19楼论坛分页设计**特点:**- **双位置分页:** 论坛通常有大量的帖子,用户可能会阅读一篇帖子后直接翻页或返回帖子列表,因此,在顶部和底部都设置分页导航,便于用户快速操作。


