我做显示新闻详细的时候,一篇新闻内容太长了,我想到把新闻内容进行分页,但是出现了一些问题,出现不了,以下我写的主要代码:
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();有问题,我该怎么改,希望各位帮我提供思路,改改,谢谢啦!
if(content!=null && "".equals(content)){
..
}
..
}
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;
}
}
}
}
还是不行啊,
>>>>>>>
if (content != null && !"".equals(content)) {