博客制作:动态添加文章内容与用户评论
1、将时间戳转换成日期
function add0(m){return m<10?'0'+m:m }
function format(shijianchuo)
{
//shijianchuo是整数,否则要parseInt转换
var time = new Date(shijianchuo);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
这里要注意time.getMonth()系统默认一月为从0开始,故要将其加一才为实际月份
2、过滤html标签
package com.zz.xd.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyUtil {
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //过滤html标签
return htmlStr.trim(); //返回文本字符串
}
}
注意要先过滤在剪切拼接,不然会导致剪切到html代码使得过滤方法无法正常使用!
controller代码:
@RequestMapping("find/{id}")
public Map getDetail(@PathVariable("id") Long id){
Detail d=detailRepository.findOne(id);
Map m=new HashMap();
List<Comment> ls=commentRepository.findByDetailid(id);
m.put("detail", d);
m.put("comments", ls);
return m;
js代码:
$(document).ready(
function() {
var url = decodeURI(location.href);
var result = url.split("?")[1];
$.get(
"/springbootdemo/detail/find/" + result,
function(data) {
console.log(data);
$("#titlecontent").text(data.detail.title);
$("#bodycontent").html(
data.detail.content);
var ccontext = "";
for (i = 0; i < data.comments.length; i++) {
var date=new Date(data.comments[i].createtime);
ccontext = ccontext
+ '<li class="comment even thread-even depth-1" id="li-comment-"> <div id="comment-969" class="comment_body contents"> <div class="profile"> <a href=""><img src="/springbootdemo/demo/index/statics/images/9cc50a9e422fb1c89aebafeb959cef7a.jpg" class="gravatar" alt="小布丁"></a> </div> <div class="main shadow"> <div class="commentinfo"> <section class="commeta"> <div class="shang"> <h4 class="author"><a href="" target="_blank"><img src="/springbootdemo/demo/index/statics/images/9cc50a9e422fb1c89aebafeb959cef7a.jpg" class="gravatarsmall" alt="小布丁">'
+ data.comments[i].authorid;
ccontext = ccontext
+ '</a></h4> </div> </section> </div> <div class="body"> <p>'
+ data.comments[i].content;
ccontext = ccontext
+ '</p> </div> <div class="xia info"> <span><time >'
+ date.getFullYear()+'年'+(date.getMonth()+1)+'月'+date.getDate()+'日';
ccontext = ccontext
+ '</time></span> <span><a rel=“nofollow” class=“comment-reply-link” href="" onclick=“return addComment.moveForm( "comment-969", "969", "respond", "1202" )” aria-label=“回复给小布丁”>回复</a></span> </div> </div> </div> </li>';
}
$("#commentwrap").html(ccontext);
});
});
效果图如下: