com.github.dactiv.common.spring.mvc.SpringMvcHolder.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.common.spring.mvc.SpringMvcHolder.java

Source

/*
 * Copyright 2013-2014 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.github.dactiv.common.spring.mvc;

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

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * spring mvc ?Struts2ServletActionContext,
 * 
 * @author maurice
 *
 */
@SuppressWarnings("unchecked")
public abstract class SpringMvcHolder {

    /**
     * ?request attribute
     * 
     * @param name ??
     * 
     * @return Object
     */
    public static <T> T getRequestAttribute(String name) {
        return (T) getAttribute(name, RequestAttributes.SCOPE_REQUEST);
    }

    /**
     * request attribute
     * 
     * @param name ??
     * @param value 
     */
    public static void addRequestAttribute(String name, Object value) {
        addAttribute(name, value, RequestAttributes.SCOPE_REQUEST);
    }

    /**
     * request attribute
     * 
     * @param name ??
     */
    public void removeRequestAttribute(String name) {
        removeAttribute(name, RequestAttributes.SCOPE_REQUEST);
    }

    /**
     * ?sessiont attribute
     * 
     * @param name ??
     * 
     * @return Object
     */
    public static <T> T getSessionAttribute(String name) {
        return (T) getAttribute(name, RequestAttributes.SCOPE_SESSION);
    }

    /**
     * session attribute
     * 
     * @param name ??
     * @param value 
     */
    public static void addSessionAttribute(String name, Object value) {
        addAttribute(name, value, RequestAttributes.SCOPE_SESSION);
    }

    /**
     * session attribute
     * 
     * @param name ??
     */
    public void removeSessionAttribute(String name) {
        removeAttribute(name, RequestAttributes.SCOPE_SESSION);
    }

    /**
     * ?,?Attribute
     * 
     * @param name attribute??
     * @param scope ,?{@link RequestAttributes}
     * 
     * @return Object
     */
    public static <T> T getAttribute(String name, int scope) {
        return (T) getServletRequestAttributes().getAttribute(name, scope);
    }

    /**
     * ?,Attribute
     * 
     * @param name attribute??
     * @param value 
     * @param scope ,?{@link RequestAttributes}
     */
    public static void addAttribute(String name, Object value, int scope) {
        getServletRequestAttributes().setAttribute(name, value, scope);
    }

    /**
     * ???Attribute
     * 
     * @param name attribute??
     * @param scope ,?{@link RequestAttributes}
     */
    public static void removeAttribute(String name, int scope) {
        getServletRequestAttributes().removeAttribute(name, scope);
    }

    /**
     * ?HttpServletRequest
     * 
     * 
     * @return {@link HttpServletRequest}
     */
    public static HttpServletRequest getRequest() {

        return getServletRequestAttributes().getRequest();
    }

    /**
     * ? http session
     * 
     * @return {@link HttpSession}
     */
    public static HttpSession getSession() {
        return getSession(false);
    }

    /**
     * ? http session
     * 
     * @param create true to create a new session for this request if necessary; false to return null if there's no current session
     * 
     * @return {@link HttpSession}
     */
    public static HttpSession getSession(boolean create) {
        return getRequest().getSession(create);
    }

    /**
     * ?spring mvcServletRequestAttributes
     * 
     * @return {@link ServletRequestAttributes}
     */
    public static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    }

    /**
     * ?web
     * 
     * @param path 
     * 
     * @return String
     */
    public static String getRealPath(String path) {
        return getRequest().getSession().getServletContext().getRealPath(path);
    }
}