继续昨天登录注册
<!--错误信息-->
<div class="msg" th:text="${msg}"></div>
用这个div来显示错误提示,先把功能做出来,以后再去用ajax来重新做。
@RequestMapping(value = "/logincheck",method = RequestMethod.POST)
public String loginCheck(HttpServletRequest request,ModelMap map){
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userService.getUserByUP(username,password);
if(user != null){
request.getSession().setAttribute("user",user);
return "redirect:/";
}
map.addAttribute("msg","账号密码错误");
return "login";
}
@RequestMapping(value = "/signupcheck",method = RequestMethod.POST)
public String signupCheck(HttpServletRequest request,ModelMap map){
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userService.getUserByUsername(username);
if(user == null){
User u = new User();
u.setUsername(username);
u.setPassword(password);
userService.saveUser(u);
map.addAttribute("msg","注册成功,请登录");
return "login";
}
map.addAttribute("msg","用户已被注册");
return " signup";
}
简单做做
下午做评论。
<form class="com_form" action="/comment/add" accept-charset="utf-8" method="post">
<!--这条新闻id-->
<input type="hidden" name="nid" th:value="${News.id}">
<textarea class="com_area" name="context"></textarea>
<div class="com_btn" th:if=${session.user!=null}>
<input class="button" type="submit" value="评论">
</div>
<div class="com_btn" th:if=${session.user==null}>
<a href="/user/login">登录评论</a>
</div>
</form>
评论表,用th:if判断session中有没有用户,显示评论还是登录。
<div class="com_row" th:each="cs:${Comments}">
<!--假装是头像-->
<div class="uphoto" style="float: left;font-size: 60px">○</div>
<div class="uid" th:utext="${cs.uid}">用户名</div>
<div class="comtime" th:text="${#dates.format(cs.comtime,'yyyy-MM-dd HH:mm:ss')}">时间</div>
<div class="comment" th:utext="${cs.tid}+${cs.context}">评论</div>
<div class="recomment" th:if=${session.user!=null}>
<input class="review" type="button" value="回复">
</div>
</div>
回复的功能我现在也做不出楼中楼,就用@形式。
为了能点击回复按钮把“@XXX:”这样的内容设置到评论框里,去学习了下jQuery。在thymeleaf中要这么写
</script>
<script type="text/javascript">
/*<![CDATA[*/
$(document).ready(function(){
$(".review").click(function(){
var tname = $(this).parent().prev().prev().prev().text();
$(".com_area").text('@'+tname+":");
});
});
/*]]>*/
</script>
很神秘。
接下来处理”@ABC:XXXX”这样的回复内容,不是回复的话很简单,直接全部存进去。
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String addComment(HttpServletRequest request){
Comment comment = new Comment();
User user = (User) request.getSession().getAttribute("user");
String context = request.getParameter("context");
if(context.startsWith("@")){
//下两句截取@到:
String tname = context.substring(1);
tname = StringUtils.substringBefore(tname,":");//*1
User tUser = userService.getUserByUsername(tname);
comment.setTid(Integer.toString(tUser.getId()));
//去掉回复中的@XXX:,留下内容
context = context.replaceFirst("@"+tname+":","");//*2
}
comment.setContext(context);
comment.setUid(Integer.toString(user.getId()));
comment.setNid(request.getParameter("nid"));
commentService.saveComment(comment);
return "redirect:/"+request.getParameter("nid");
}
在写代码时真的要细心,在*1处,我原来写成了tname = StringUtils.substringBefore(context,":");
导致前面的@没去掉,查不到用户报错。在*2处写成了context = context.replaceFirst("@"+tname+";","");
分号写成了冒号,替换不掉。emmmmm