首先,要说明的是:文章是用fckeditor编辑成的html代码。网上有很多方法,都只是截取字符,html标签很容易就被分开了。以至于前台显示时出错。
1。创建自己的类(用来处理fckeditor编辑器生成的代码,即文章内容):类的代码如下:
public class OnepageUtil {
private int size = 800;// 每页显示内容量
private int currentPage = 1;//设置第一页为初始页
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
private int pages = 0;
private int length = 0;
String PageInfo = "";
//前台显示的文章内容
public String getSubContent(String content){
ArrayList newContent = new ArrayList();
newContent = acticlePager(content);
String cont = "文章已结束...";
pages = countPages(content);
if (currentPage > pages)
currentPage = pages;
try{
cont = newContent.get(currentPage-1).toString();
}catch(Exception e){
}
return cont;
}
//将文章内容分成数组
public ArrayList acticlePager(String content) {
ArrayList list = new ArrayList();
boolean end = true;
length = content.length();
while (end) {
String temp = "";
/**
* 如果内容已经少于默认数,就直接作为结尾返回
*/
if (size >= length) {
temp = content;
list.add(temp);
break;
}
/**
* temp为本次截取内容段 temp2为余下的内容段
*/
temp = content.substring(0, size);
String temp2 = content.substring(size + 1, length);
String session = temp;
int a = 0;
int b = 0;
/**
* 首先计算 <和>是否相等
*/
while (temp.indexOf(" <") > -1) {
a++;
temp = temp.substring(temp.indexOf(" <") + 1, temp.length());
}
temp = session;
while (temp.indexOf(">") > -1) {
b++;
temp = temp.substring(temp.indexOf(">") + 1, temp.length());
}
if (a != b) {
int p = temp2.indexOf(">");
temp = content.substring(0, size + p + 2);
temp2 = content.substring(size + p + 2, length);
session = temp;
}
/**
* 如果相等就再计算 <P和
* </p>
* 是否吻合
*/
if (a == b) {
a = 0;
b = 0;
temp = session;
while (temp.indexOf(" <P") > -1) {
a++;
temp = temp.substring(temp.indexOf(" <") + 1, temp.length());
}
temp = session;
while (temp.indexOf(" </P") > -1) {
b++;
temp = temp.substring(temp.indexOf(">") + 1, temp.length());
}
if (a == b) {
break;
}
if (a != b) {
int p = temp2.indexOf(" </P>");
temp = content.substring(0, size + p + 5);
try {
if ((size + p + 5) < length)
temp2 = content.substring(size + p + 5, length);
} catch (Exception e) {
temp2 = "";
}
}
}
/**
* 余下内容更新
*/
content = temp2;
length = content.length();
// System.out.println("cut after:"+content);
System.out.println("cut after:" + temp);
list.add(temp);
/**
* 如果不存在余下内容了就结束本次操作
*/
if (temp2.equals("") || temp2.length() < 1)
end = false;
}
return list;
}
//计算总页数
public int countPages(String content){
length = content.length();
if (length % size == 0) {
pages = length / size;
} else {
pages = length / size + 1;
}
return pages;
}
//生成页码导航
public String pageInfo(String content,int pageNo){
pages = countPages(content);
for (int i = 1; i <= pages; i++)
{
if (i == currentPage)
PageInfo += "" + i + "";
else
PageInfo += " <a href=/article/"+pageNo+"_" + i + ".html>[" + i + "] </a> ";
}
if (currentPage > 1)
PageInfo = " <a href=/article/"+pageNo+"_" + (currentPage - 1) + ".html>上一页 </a>" + PageInfo;
if (currentPage < pages)
PageInfo += " <a href=/article/"+pageNo+"_" + (currentPage + 1) + ".html>下一页 </a>";
return PageInfo;
}
}
2。页面用以下方式直接调用:
<jsp:useBean id="htmlUtil" class="com.yzfc.util.OnepageUtil"></jsp:useBean>
用此方法获取当前页码并设置为 htmlUtil 的属性:
<%
if (request.getAttribute("ps") != null){
int currentPage = Integer.parseInt((String)request.getAttribute("ps"));
htmlUtil.setCurrentPage(currentPage);
}
%>
用此方法显示内容:
<%=htmlUtil.getSubContent(article.getContent().toString()) %>
3. 编辑调用的action :加入如下代码:
private String ps; //用来传递参数
public String getPs() {
return ps;
}
public void setPs(String ps) {
this.ps = ps;
}4。 利用apache的 url 静态化转化为真正的请求url: 此规则在.htaccess文件中
RewriteRule ^article/([0-9].*)_([0-9].*).html$ findArticleById.action?article.articleid=$1&ps=$2
5。