@用法做回复

来源:互联网 发布:淘宝怎么上传 编辑:程序博客网 时间:2024/05/24 06:33

实现时主要问题在于怎么将所有@对象给找出来,替换成<a href = "/user/id">@user.name</a>的形式。

@Overridepublic void save(Comment comment, long topicId) throws UnsupportedEncodingException {comment.setTopic(topicService.findById(topicId));User user = userService.findByName("sunhuwh");if(user==null){User user2 = new User();user2.setName("sunhuwh");userService.save(user2);comment.setUser(user2);}else{comment.setUser(user);}String content = comment.getContent();LinkedList<Integer> atArr = Lists.newLinkedList();LinkedList<Integer> allMark = Lists.newLinkedList();char[] contents = content.toCharArray();Pattern p = Pattern.compile("[\\+-/*()^~!@#$%&_=|\'\":;<>,.》《?、,:;”’}{】【——)(……¥!·?/ ]+");Pattern p2 = Pattern.compile("@");for(int i = 0;i<contents.length;i++){String contentToString = String.valueOf(contents[i]);Matcher matcher = p.matcher(contentToString);Matcher matcher2 = p2.matcher(contentToString);if(matcher2.find()){atArr.add(i);}else if(matcher.find()){allMark.add(i);}}allMark.addAll(atArr);Collections.sort(allMark);List<String> nameList = Lists.newArrayList();String newContent="";int m = 0;for(int i = 0 ;i<atArr.size();i++){int at = (int) atArr.get(i);for(int j = 0;j<allMark.size();j++){int mark = (int) allMark.get(j);if(at<mark){String name = content.substring(at, mark);nameList.add(name.replace("@",""));if(m==0) newContent=content;newContent = newContent.replaceFirst(name, "af134256427sabear83199876543210041125>"+name.replace("@","")+"</a>");m++;break;}}}for(String name:nameList){String newName = "af134256427sabear83199876543210041125>"+name+"</a>";String newName2="@"+name;User newUser = userService.findByName(name);if(newUser!=null){newName2="<a href ='user/"+newUser.getId()+"' >@"+newUser.getName()+"</a>";}newContent = newContent.replaceFirst(newName, newName2);}if(atArr.size()==0){newContent = content;}comment.setContent(newContent);comment.setTopic(topicService.findById(topicId));save(comment);}

js

function addComment(parentId,commentId,userName){var topicId = $("#topicId").val();if($("#addCommentId"+commentId).css('display')=="none"){$("#addCommentId"+commentId).css('display','block');}else{$("#addCommentId"+commentId).css('display','none'); }$.get("comment/getComments?parentId="+parentId+"&topicId="+topicId,function(data){var appendButton ="";var append = "";if(data!=""){var arr = data.split("$");var allTr="";for(var i = 0;i<arr.length;i++){var arr2 = arr[i].split(',');var name = arr2[3];var content = arr2[0];var time = arr2[1];var id = arr2[2];content = "<table><tr><td>"+content+"</td></tr><tr><td>"+time+"</td></tr></table>";appendButton = appendButton+content+"<button type = 'button' id = 'toAddCommentId' onclick = 'replaceFrom("+id+","+parentId+",\""+name+"\""+")'>回复</button>";}}$("#addCommentId"+commentId).html(appendButton+"<br/>"+"<div id='commentsId'><form id='formTextarea' action='comment?topicId="+topicId+"&parentId="+parentId+"&commentId="+commentId+"' id = 'form' method = 'post'><textarea id='textareaId"+commentId+"' rows='2' cols='77' validate='required' validate-message='不能为空!'  name = 'content' >@"+userName+"...."+commentId+"...."+parentId+":</textarea><br/><button type = 'submit' id = 'commentContentId'>发表</button></form</div>");})}//form替换掉function replaceFrom(commentId2,parentId2,name2){var topicId = $("#topicId").val();alert(topicId+"...."+parentId2+"...."+commentId2);$("#commentsId").html("<form id='formTextarea' action='comment?topicId="+topicId+"&parentId="+parentId2+"&commentId="+commentId2+"' id = 'form' method = 'post'><textarea id='textareaId"+commentId2+"' rows='2' cols='77' validate='required' validate-message='不能为空!'  name = 'content' >@"+name2+"...."+commentId2+":</textarea><br/><button type = 'submit' id = 'commentContentId'>发表</button></form");}

jsp:

<div style = "display:none" id = "addCommentId${comment.id }">                                 </div>
comment.id是第一处位置的comment的id。

上面那处js代码是为了实现下面出现的,要理解好思路,先是所有回复主题的回复显示出来,再然后点击第一处时是将回复此回复的所有数据找出来,并有form。

如果点击了第二处时,就需要换个form。我也试过不换form,但是最后不知道怎么回事,form总是不变。总是原来的。



More than one row with the given identifier was found。

hibernate不是一对一一对多多对一没有配置好。

比如一个user本该有多个课程。而配置时却一对一了。

0 0