com.zuoxiaolong.blog.common.web.AbstractController.java Source code

Java tutorial

Introduction

Here is the source code for com.zuoxiaolong.blog.common.web.AbstractController.java

Source

/*
 * Copyright 2002-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 com.zuoxiaolong.blog.common.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * controller,?webcontroller.
 *
 * @author Boren You
 * @date 2016/5/12 21:28
 * @since 1.0.0
 */
public abstract class AbstractController {

    /**
     * 
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * ?HttpServletRequest
     */
    private static ThreadLocal<HttpServletRequest> httpServletRequestThreadLocal = new ThreadLocal<>();

    /**
     * ?Model
     */
    private static ThreadLocal<Model> modelThreadLocal = new ThreadLocal<>();

    /**
     * @ModelAttribute??
     *
     * @param request
     * @param model
     */
    @ModelAttribute
    protected void setThreadLocal(HttpServletRequest request, Model model) {
        httpServletRequestThreadLocal.set(request);
        modelThreadLocal.set(model);
    }

    /**
     * ??HttpServletRequest
     *
     * @return ?HttpServletRequest
     */
    protected HttpServletRequest getRequest() {
        return httpServletRequestThreadLocal.get();
    }

    /**
     * ??HttpSession
     * @return ?HttpSession
     */
    protected HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * ??Model
     * @return ?Model
     */
    protected Model getModel() {
        return modelThreadLocal.get();
    }

    /**
     * ??ServletContext
     * @return ?ServletContext
     */
    protected ServletContext getContext() {
        return getRequest().getServletContext();
    }

    /**
     * ? Model 
     * @param name ??
     * @param value 
     */
    protected void setModelAttribute(String name, Object value) {
        getModel().addAttribute(name, value);
    }

    /**
     * ? HttpServletRequest 
     * @param name ??
     * @param value 
     */
    protected void setRequestAttribute(String name, Object value) {
        HttpServletRequest request = this.getRequest();
        request.setAttribute(name, value);
    }

    /**
     * ? HttpSession 
     * @param name ??
     * @param value 
     */
    public void setSessionAttribute(String name, Object value) {
        HttpSession session = this.getSession();
        session.setAttribute(name, value);
    }

    /**
     *  HttpSession ?
     * @param name ??
     * @return 
     */
    protected Object getSessionAttribute(String name) {
        HttpSession session = this.getSession();
        Object value = session.getAttribute(name);
        return value;
    }

    /**
     *  HttpServletRequest ?
     * @param name ??
     * @return 
     */
    protected Object getRequestAttribute(String name) {
        HttpServletRequest request = this.getRequest();
        Object value = request.getAttribute(name);
        return value;
    }

}