对一篇新闻内容如何分页

Magiccrystal 2010-03-04 03:14:45
我做显示新闻详细的时候,一篇新闻内容太长了,我想到把新闻内容进行分页,但是出现了一些问题,出现不了,以下我写的主要代码:
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();有问题,我该怎么改,希望各位帮我提供思路,改改,谢谢啦!

...全文
129 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magiccrystal 2010-04-22
  • 打赏
  • 举报
回复
不知道 .net php有好的分页没,但是以上思想页面不好看,建议大家以后有此情况,加滚动条,呵呵
NOKIA5320XM 2010-03-05
  • 打赏
  • 举报
回复
如果文中有图片,那又怎么分页呢? 等待高手的出现
cgcavalier518 2010-03-05
  • 打赏
  • 举报
回复
if (content != null && "".equals(content)) {
>>>>>>>
if (content != null && !"".equals(content)) {
Magiccrystal 2010-03-05
  • 打赏
  • 举报
回复
int wordCount = 1000; // 1000字为一页
cur_page = 1;
if (id != 0) {
message = messageManager.get(id);
// System.out.println(id+" "+message.getContent()+" "+cur_page );
if (content != null && "".equals(content)) {
String tempContent = message.getContent(); // 文章的全部内容
int t = tempContent.length();

if (t <= wordCount) { // 如果文章小于1000字,不用分页
cur_page = 1;
if(cur_page<=0){
cur_page=1;
}
tempContent = content;
} else {
String pageContent = tempContent.substring(0, wordCount); // 获得前1000字的内容
System.out.println(pageContent);
tempContent = tempContent.substring(wordCount); // 把tempContent改为1000字以后的内容
int index = tempContent.indexOf("\n"); // 换行符的位置
if (index == -1)
pageContent += tempContent; // 如果1000字后没有分段了,也不分了,当然也可以强制分页
String tempB = tempContent.substring(0, index); // 1000字后的第一个换行符前的内容

content += tempB;
int totalPages = t % wordCount == 0 ? (t / wordCount) : (t
/ wordCount + 1);

if (cur_page > totalPages) {
cur_page = totalPages;
}

}
}
}
还是不行啊,
leavin521 2010-03-05
  • 打赏
  • 举报
回复
if(content!=null && !"".equals(content)){
..
}
Magiccrystal 2010-03-05
  • 打赏
  • 举报
回复
但是还是没有分页成功啊,上面的我该怎么改??
Magiccrystal 2010-03-05
  • 打赏
  • 举报
回复
哦,掉了“!”,恩,但是还是不行,分页是不是传得有问题??还是在action中有问题,高手帮帮我吧,谢谢
tup2005 2010-03-04
  • 打赏
  • 举报
回复
LZ的判断错误,等于空也不是那么判断
if(content!=null && "".equals(content)){
..
}
fantasy2436 2010-03-04
  • 打赏
  • 举报
回复
content != null || content != "" //这个判断错了 把||改成&&

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧