Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package ru.itmo.wp.web.page;
  2.  
  3. import ru.itmo.wp.model.domain.User;
  4. import ru.itmo.wp.model.service.ArticleService;
  5. import ru.itmo.wp.model.service.UserService;
  6.  
  7. import javax.servlet.http.HttpServletRequest;
  8. import java.util.Map;
  9.  
  10. public class Page {
  11.     static final String USER_ID_SESSION_KEY = "userId";
  12.  
  13.     private UserService userService = new UserService();
  14.     private ArticleService articleService = new ArticleService();
  15.  
  16.     private User user;
  17.  
  18.     protected UserService getUserService() {
  19.         return userService;
  20.     }
  21.  
  22.     public ArticleService getArticleService() {
  23.         return articleService;
  24.     }
  25.  
  26.     public User getUser() {
  27.         return user;
  28.     }
  29.  
  30.     protected boolean isEmptyUser() {
  31.         return user == null;
  32.     }
  33.  
  34.     protected void login(HttpServletRequest request, User user) {
  35.         request.getSession(true).setAttribute(USER_ID_SESSION_KEY, user.getId());
  36.     }
  37.  
  38.     protected void logout(HttpServletRequest request) {
  39.         request.getSession().removeAttribute(USER_ID_SESSION_KEY);
  40.     }
  41.  
  42.     public void before(HttpServletRequest request, Map<String, Object> view) {
  43.         Long userId = (Long) request.getSession().getAttribute(USER_ID_SESSION_KEY);
  44.         if (userId != null) {
  45.             user = userService.find(userId);
  46.             view.put("user", user);
  47.         }
  48.     }
  49.  
  50.     public void after(HttpServletRequest request, Map<String, Object> view) {
  51.         // No operations.
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement