所示PO的父类11 | *@see 12 | *@since 13 | */ 14 | public class BaseDomain implements Serializable 15 | { 16 | public String toString() { 17 | return ToStringBuilder.reflectionToString(this); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /WebContent/WEB-INF/jsp/fail.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 |
|7 | *@see 8 | *@since 9 | */ 10 | public class CommonConstant 11 | { 12 | /** 13 | * 用户对象放到Session中的键名称 14 | */ 15 | public static final String USER_CONTEXT = "USER_CONTEXT"; 16 | 17 | /** 18 | * 将登录前的URL放到Session中的键名称 19 | */ 20 | public static final String LOGIN_TO_URL = "toUrl"; 21 | 22 | /** 23 | * 每页的记录数 24 | */ 25 | public static final int PAGE_SIZE = 5; 26 | } 27 | -------------------------------------------------------------------------------- /WebContent/WEB-INF/jsp/success.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 |
23 | * 主题对应的主题帖 24 | *25 | * 26 | * @see 27 | *@since 28 | */ 29 | @Entity 30 | @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 31 | @Inheritance(strategy = InheritanceType.SINGLE_TABLE) 32 | @DiscriminatorColumn(name = "post_type", discriminatorType = DiscriminatorType.STRING) 33 | @DiscriminatorValue("2") 34 | public class MainPost extends Post { 35 | 36 | } 37 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 |
17 | * 所有Controller的基类 18 | *19 | * 20 | * @see 21 | * @since 22 | */ 23 | public class BaseController { 24 | protected static final String ERROR_MSG_KEY = "errorMsg"; 25 | 26 | /** 27 | * 获取保存在Session中的用户对象 28 | * 29 | * @param request 30 | * @return 31 | */ 32 | protected User getSessionUser(HttpServletRequest request) { 33 | return (User) request.getSession().getAttribute( 34 | CommonConstant.USER_CONTEXT); 35 | } 36 | 37 | /** 38 | * 保存用户对象到Session中 39 | * @param request 40 | * @param user 41 | */ 42 | protected void setSessionUser(HttpServletRequest request,User user) { 43 | request.getSession().setAttribute(CommonConstant.USER_CONTEXT, 44 | user); 45 | } 46 | 47 | 48 | /** 49 | * 获取基于应用程序的url绝对路径 50 | * 51 | * @param request 52 | * @param url 53 | * 以"/"打头的URL地址 54 | * @return 基于应用程序的url绝对路径 55 | */ 56 | public final String getAppbaseUrl(HttpServletRequest request, String url) { 57 | Assert.hasLength(url, "url不能为空"); 58 | Assert.isTrue(url.startsWith("/"), "必须以/打头"); 59 | return request.getContextPath() + url; 60 | } 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/com/csdn/web/RegisterController.java: -------------------------------------------------------------------------------- 1 | 2 | package com.csdn.web; 3 | 4 | import javax.servlet.http.HttpServletRequest; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.servlet.ModelAndView; 11 | 12 | import com.csdn.domain.User; 13 | import com.csdn.exception.UserExistException; 14 | import com.csdn.service.UserService; 15 | 16 | 17 | /** 18 | * 19 | *
23 | * 用户注册的Action 24 | *25 | * 26 | * @see 27 | * @since 28 | */ 29 | @Controller 30 | public class RegisterController extends BaseController { 31 | /** 32 | * 自动注入 33 | */ 34 | @Autowired 35 | private UserService userService; 36 | 37 | 38 | 39 | /** 40 | * 用户登录 41 | * @param request 42 | * @param response 43 | * @param user 44 | * @return 45 | */ 46 | @RequestMapping(value = "/register", method = RequestMethod.POST) 47 | public ModelAndView register(HttpServletRequest request,User user){ 48 | ModelAndView view = new ModelAndView(); 49 | view.setViewName("/success"); 50 | try { 51 | userService.register(user); 52 | } catch (UserExistException e) { 53 | view.addObject("errorMsg", "用户名已经存在,请选择其它的名字。"); 54 | view.setViewName("forward:/register.jsp"); 55 | } 56 | setSessionUser(request,user); 57 | return view; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/com/csdn/dao/TopicDao.java: -------------------------------------------------------------------------------- 1 | package com.csdn.dao; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | import com.csdn.domain.Topic; 6 | 7 | 8 | 9 | /** 10 | * topic 的DAO类 11 | * 12 | */ 13 | @Repository 14 | public class TopicDao extends BaseDao
24 | * 论坛管理,这部分功能由论坛管理员操作,包括:创建论坛版块、指定论坛版块管理员、 25 | * 用户锁定/解锁。 26 | *27 | * 28 | * @see 29 | * @since 30 | */ 31 | @Controller 32 | @RequestMapping("/login") 33 | public class LoginController extends BaseController { 34 | /** 35 | * 自动注入 36 | */ 37 | @Autowired 38 | private UserService userService; 39 | 40 | /** 41 | * 用户登陆 42 | * @param request 43 | * @param user 44 | * @return 45 | */ 46 | @RequestMapping("/doLogin") 47 | public ModelAndView login(HttpServletRequest request, User user) { 48 | User dbUser = userService.getUserByUserName(user.getUserName()); 49 | ModelAndView mav = new ModelAndView(); 50 | mav.setViewName("forward:/login.jsp"); 51 | if (dbUser == null) { 52 | mav.addObject("errorMsg", "用户名不存在"); 53 | 54 | } else if (!dbUser.getPassword().equals(user.getPassword())) { 55 | mav.addObject("errorMsg", "用户密码不正确"); 56 | } else if (dbUser.getLocked() == User.USER_LOCK) { 57 | mav.addObject("errorMsg", "用户已经被锁定,不能登录。"); 58 | } else { 59 | dbUser.setLastIp(request.getRemoteAddr()); 60 | dbUser.setLastVisit(new Date()); 61 | userService.loginSuccess(dbUser); 62 | setSessionUser(request,dbUser); 63 | String toUrl = (String)request.getSession().getAttribute(CommonConstant.LOGIN_TO_URL); 64 | request.getSession().removeAttribute(CommonConstant.LOGIN_TO_URL); 65 | //如果当前会话中没有保存登录之前的请求URL,则直接跳转到主页 66 | if(StringUtils.isEmpty(toUrl)){ 67 | toUrl = "/index.html"; 68 | } 69 | mav.setViewName("redirect:"+toUrl); 70 | } 71 | return mav; 72 | } 73 | 74 | /** 75 | * 登录注销 76 | * @param session 77 | * @return 78 | */ 79 | @RequestMapping("/doLogout") 80 | public String logout(HttpSession session) { 81 | session.removeAttribute(CommonConstant.USER_CONTEXT); 82 | return "forward:/index.jsp"; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /WebContent/login.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 | 4 | 5 | 6 |
Log in
56 | 57 | Wanner 59 | to Register ? 60 | 63 | 64 | 67 | 68 || 用户 | 69 |75 | |
| 锁定/解锁 | 78 |锁定 79 | 解锁 | 80 |
| 84 | | |
| 论坛模块 | 71 |77 | |
| 用户 | 80 |86 | |
| 90 | | |
| ${post.postTitle} | 23 ||
| 用户:${post.user.userName} 26 | 积分:${post.user.credit} 时间: |
28 | ${post.postText} | 29 |
| 标题 | 66 |68 | |
| 内容 | 71 |73 | |
| 76 | | 79 ||
Register
35 | 38 | Go back to Login ? 40 | 43 | 44 | 46 | 47 || 用户名 | 84 |85 | |
| 密码 | 88 |89 | |
| 密码确认 | 92 |93 | |
| 97 | | |