文章静态化分页
来源:互联网 发布:10档行情软件 编辑:程序博客网 时间:2024/06/05 22:36
此功能是公司之前的一个要求,后台添加的文章需要进行分页静态化,根据我们公司的需求进行的功能实现,下面是我的步骤
一、所用的框架是springMVC+spring+mybatis
二、后台编辑器使用的是ueditor1_4_3_2-utf8-jsp版本,下载地址是http://ueditor.baidu.com/website/download.html
三、在页面导入编辑器的使用
1:下面是页面的代码
<tr bgcolor="#F9FAF3" id="lasttr">
<td height="30" align="right" valign="top">文章内容:</td>
<td><textarea name="content" id="content" rows="10" cols="60" style="width:760px;height:400px;"></textarea></td>
</tr>
2:需要引入的js和css有:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/admin/ueditor/themes/default/css/ueditor.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/admin/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/admin/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/admin/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/admin/ueditor/lang/zh-cn/zh-cn.js"></script>
:3: js代码如下:
$(function(){
var um = UE.getEditor('content',{
lang:'zh-cn',
langPath:"${pageContext.request.contextPath}/admin/ueditor/lang/",
});
});
4:需要更改的编辑器配置有
4-1、config.json文件
4-2、 ueditor.config.js文件
4-3、在toolbars一项里面加入自己需要的工具
我这里主要是需要分页符,所以加入了pagebreak,编辑器效果展示如下:
箭头表示分隔符,红框表示需要分页的地方加入分隔符
分隔符在编辑器显示的是一条粗线,传到后台的时候是和content一起的,是字符“_ueditor_page_break_tag_”
四、后台代码
后台是提前通过jquery写好文章模板的静态html页面,然后通过流获取文件然后替换内容,然后生成对应的静态页面
代码如下:
public void createHtml(Article article, String root, String templatePath, String htmlPath, String prevHref, String prevTitle, String nextHref, String nextTitle) {// 读取模板文件内容String text = "";if(article != null){String tel = "";String name = "";String logo = "";//获取文章所有内容String content = article.getContent();if(content.contains("_ueditor_page_break_tag_")){String[] contentArr = content.split("_ueditor_page_break_tag_");htmlPath = htmlPath.replaceAll("\\\\","/" );//每一页链接地址String linkPage = "";String pageStr = "";//上一页String prePage = "";//下一页String nextPage = "";for(int i = 1;i<= contentArr.length;i++){text = FileIOUtil.readFileFromPath(templatePath);String suffixText = text.substring(0,text.indexOf("mainContent"));String yus = text.substring(text.indexOf("mainContent"));StringBuffer center = new StringBuffer("");for(int j = 1;j<= contentArr.length;j++){if(j == 1){linkPage = article.getId() + ".html";}else{linkPage = article.getId() + "_"+ j + ".html";}pageStr = pageStr + ""+j+"" ;}//判断是否有上一页和下一页if(i == 1){linkPage = htmlPath;prePage = "";//当目前是第一页时没有上一页}else{linkPage = htmlPath.substring(0, htmlPath.lastIndexOf(".")) + "_"+ i + ".html";if(i == 2){prePage = "上一页";}else{prePage = "上一页";}}if(i != contentArr.length){nextPage = "下一页";}else{nextPage ="";}pageStr = prePage + pageStr + nextPage;center.insert(center.indexOf(""), pageStr);text =suffixText + yus.substring(0, yus.indexOf("
")) + center + yus.substring(yus.indexOf("
"));pageStr = "";try {List
configures = adminConfigureMapper.findEntity();if (null != configures && configures.size() > 0) {AdminConfigure configure = configures.get(0);tel = configure.getTel();name = configure.getSiteName();logo = configure.getLogo();}text = text.replace("{tel}", tel);text = text.replace("{name}", name);text = text.replace("{logo}", logo);} catch (Exception e) {}// 使用信息内容替换模板中对应占位符//text = text.replace("{rootPath}", root);text = text.replace("{title}", article.getTitle());if (null != article.getKeyword() && !"".equals(article.getKeyword())) {text = text.replace("{keywords}", article.getKeyword());}else {text = text.replace("{keywords}", "网谱,九亲云谱,姓氏丛书,宗亲文化,宗亲资讯,华人谱库,修谱常识,谱书数字化,宗亲会,宗亲联谊,修谱资料,家规家训");}if (null != article.getSummary() && !"".equals(article.getSummary())) {text = text.replace("{description}", article.getSummary());} else {text = text.replace("{description}", "");}text = text.replace("{articleTitle}", article.getTitle());text = text.replace("{articleDate}", DateUtil.dateToString(article.getTime(), "yyyy-MM-dd HH:mm:ss"));text = text.replace("{praise}", String.valueOf(article.getPraise()));text = text.replace("{comments}", String.valueOf(article.getComments()));if (null != article.getAuthor() && !"".equals(article.getAuthor())) {text = text.replace("{articleAuthor}", "作者:" + article.getAuthor());} else {text = text.replace("{articleAuthor}", "");}text = text.replace("{articleContent}",contentArr[i-1]);text = text.replace("{prevHref}", "/" + prevHref);if (null != prevTitle && !"".equals(prevTitle)) {text = text.replace("{prevTitle}", prevTitle);} else {text = text.replace("{prevTitle}", "已经是第一篇了");}text = text.replace("{nextHref}", "/" + nextHref);if (null != nextTitle && !"".equals(nextTitle)) {text = text.replace("{nextTitle}", nextTitle);} else {text = text.replace("{nextTitle}", "已经是最后一篇了");}text = text.replace("{webSitItems}", ItemsUtil.getItem(article.getCategoryId()));text = text.replace("{articleId}", String.valueOf(article.getId()));text = text.replace("{categoryId}", String.valueOf(article.getCategoryId()));text = text.replace("{webRoot}", root + "/");FileIOUtil.writeFileFromText(linkPage, text);text = "";}}else{text = FileIOUtil.readFileFromPath(templatePath);try {List configures = adminConfigureMapper.findEntity();if (null != configures && configures.size() > 0) {AdminConfigure configure = configures.get(0);tel = configure.getTel();name = configure.getSiteName();logo = configure.getLogo();}text = text.replace("{tel}", tel);text = text.replace("{name}", name);text = text.replace("{logo}", logo);} catch (Exception e) {}// 使用信息内容替换模板中对应占位符//text = text.replace("{rootPath}", root);text = text.replace("{title}", article.getTitle());if (null != article.getKeyword() && !"".equals(article.getKeyword())) {text = text.replace("{keywords}", article.getKeyword());}else {text = text.replace("{keywords}", "网谱,九亲云谱,姓氏丛书,宗亲文化,宗亲资讯,华人谱库,修谱常识,谱书数字化,宗亲会,宗亲联谊,修谱资料,家规家训");}if (null != article.getSummary() && !"".equals(article.getSummary())) {text = text.replace("{description}", article.getSummary());} else {text = text.replace("{description}", "");}text = text.replace("{articleTitle}", article.getTitle());text = text.replace("{articleDate}", DateUtil.dateToString(article.getTime(), "yyyy-MM-dd HH:mm:ss"));text = text.replace("{praise}", String.valueOf(article.getPraise()));text = text.replace("{comments}", String.valueOf(article.getComments()));if (null != article.getAuthor() && !"".equals(article.getAuthor())) {text = text.replace("{articleAuthor}", "作者:" + article.getAuthor());} else {text = text.replace("{articleAuthor}", "");}text = text.replace("{articleContent}",article.getContent());text = text.replace("{prevHref}", "/" + prevHref);if (null != prevTitle && !"".equals(prevTitle)) {text = text.replace("{prevTitle}", prevTitle);} else {text = text.replace("{prevTitle}", "已经是第一篇了");}text = text.replace("{nextHref}", "/" + nextHref);if (null != nextTitle && !"".equals(nextTitle)) {text = text.replace("{nextTitle}", nextTitle);} else {text = text.replace("{nextTitle}", "已经是最后一篇了");}text = text.replace("{webSitItems}", ItemsUtil.getItem(article.getCategoryId()));text = text.replace("{articleId}", String.valueOf(article.getId()));text = text.replace("{categoryId}", String.valueOf(article.getCategoryId()));text = text.replace("{webRoot}", root + "/");FileIOUtil.writeFileFromText(htmlPath, text);}}}