Java tutorial
package com.kodgemisi.common.thymeleaf;/* * Copyright (c) June 2016, Kod Gemisi Ltd. dev@kodgemisi.com * * This file is a part of "suite-ui" project and this license applies to * the whole project unless explicitly stated otherwise. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import org.springframework.util.Assert; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ThymeleafLayoutInterceptor extends HandlerInterceptorAdapter { private static final String DEFAULT_LAYOUT = "layouts/default"; private static final String DEFAULT_VIEW_ATTRIBUTE_NAME = "view"; private String defaultLayout = DEFAULT_LAYOUT; private String viewAttributeName = DEFAULT_VIEW_ATTRIBUTE_NAME; public void setDefaultLayout(String defaultLayout) { Assert.hasLength(defaultLayout); this.defaultLayout = defaultLayout; } public void setViewAttributeName(String viewAttributeName) { Assert.hasLength(defaultLayout); this.viewAttributeName = viewAttributeName; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (modelAndView == null || !modelAndView.hasView()) { return; } String originalViewName = modelAndView.getViewName(); if (shouldDecorationAvoided(originalViewName)) { return; } // When using custom login page with Spring-Security, login page's controller is of type ParameterizableViewController // We'd better render it without any layout as it's just the login page. // Note that this behavior is project dependent. if (!(handler instanceof HandlerMethod)) { return; } String layoutName = getLayoutName(handler); modelAndView.setViewName(layoutName); modelAndView.addObject(this.viewAttributeName, originalViewName); } /** * @param viewName * @return true if this view shouldn't be decorated */ private boolean shouldDecorationAvoided(String viewName) { return viewName.startsWith("redirect:") || viewName.startsWith("forward:") || viewName.equals("error"); } private String getLayoutName(Object handler) { HandlerMethod handlerMethod = (HandlerMethod) handler; Layout layout = getMethodOrTypeAnnotation(handlerMethod); if (layout == null) { return this.defaultLayout; } else { return layout.value(); } } private Layout getMethodOrTypeAnnotation(HandlerMethod handlerMethod) { Layout layout = handlerMethod.getMethodAnnotation(Layout.class); if (layout == null) { return handlerMethod.getBeanType().getAnnotation(Layout.class); } return layout; } }