Springboot-web篇---part03-登录,拦截

登录

开发期间模板引擎页面修改好以后要实时生效
1.禁用模板引擎的缓存

spring.thymeleaf.cache= false

2.页面修改完ctrl+f9:重新编译

拦截器

public class LoginHandlerInterceptor implements HandlerInterceptor{
     
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     
      Object user=request.getSession().getAttribute("loginUser");
      if(user==null){
     
          //未登录,返回登录页面
          request.setAttribute("msg","没有权限请先登录");
          request.getRequestDispatcher("/index.html").forward(request,response);
          return  false;
      }else{
     
          //已登录,放行请求
          return  true;
      }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
     

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
     

    }
}

公共元素抽取

1.抽取公告片段

<div th:fragment="copy">
被抽取部分
</div>

2.公共片段引入

<div th:insert="~{footer::copy}"></div>

你可能感兴趣的:(web,ssm框架)