Java tutorial
/* * Copyright 2016-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.chenlichao.web.ssm.controller; import cn.chenlichao.web.ssm.utils.LinkedReadOnceQueue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Queue; /** * Controller * * <br>author: ? * <br>date: 16/6/12 ?7:59 * <br>version: V1.0.0 * <br>CopyrightCopyright 2016 Chen Lichao. All rights reserved. */ public abstract class BaseController { /** ?? */ public static final String SUCCESS_MESSAGE_KEY = "SSM.ACTION.MESSAGE.SUCCESS"; /** ??? */ public static final String INFO_MESSAGE_KEY = "SSM.ACTION.MESSAGE.INFO"; /** ? */ public static final String WARNING_MESSAGE_KEY = "SSM.ACTION.MESSAGE.WARNING"; /** ? */ public static final String ERROR_MESSAGE_KEY = "SSM.ACTION.MESSAGE.ERROR"; protected Logger LOGGER = LoggerFactory.getLogger(getClass()); private ThreadLocal<HttpServletRequest> localRequest = new ThreadLocal<>(); private ThreadLocal<HttpServletResponse> localResponse = new ThreadLocal<>(); //private ThreadLocal<RedirectAttributes> localRedirectAttributes = new ThreadLocal<>(); @ModelAttribute public void populateAttributes(HttpServletRequest request, HttpServletResponse response) { localRequest.set(request); localResponse.set(response); // localRedirectAttributes.set(redirectAttributes); } /** * ?? * * @param message ? */ public void addActionSuccess(String message) { addActionMessage(SUCCESS_MESSAGE_KEY, message); } /** * ??? * * @param message ? */ public void addActionInfo(String message) { addActionMessage(INFO_MESSAGE_KEY, message); } /** * ? * * @param message */ public void addActionWarning(String message) { addActionMessage(WARNING_MESSAGE_KEY, message); } /** * ? * * @param message */ public void addActionError(String message) { addActionMessage(ERROR_MESSAGE_KEY, message); } @SuppressWarnings("unchecked") private void addActionMessage(String key, String message) { Queue<String> queue = (Queue<String>) localRequest.get().getSession().getAttribute(key); if (queue == null) { queue = new LinkedReadOnceQueue<>(); localRequest.get().getSession().setAttribute(key, queue); } if (message != null) { message = message.replaceAll("\n", "\\n"); } queue.add(message); } /** * ?? * * @return ? */ protected HttpServletRequest getRequest() { return localRequest.get(); } /** * ??? * * @return ?? */ protected HttpServletResponse getResponse() { return localResponse.get(); } // protected RedirectAttributes getRedirectAttributes() { // return localRedirectAttributes.get(); // } }