Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.tjport.common.spring; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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; import org.springframework.web.context.request.ServletWebRequest; import com.tjport.common.utils.IpUtils; /** * spring mvc ? * * @author : * @date : */ @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); } /** * ?HttpServletResponse * * @return {@link HttpServletResponse} */ public static HttpServletResponse getResponse() { return getServletWebRequest().getResponse(); } /** * ?spring mvcServletWebRequest * * @return {@link ServletWebRequest} */ public static ServletWebRequest getServletWebRequest() { return ((ServletWebRequest) RequestContextHolder.getRequestAttributes()); } /** * ?HttpRequestParameter. */ public static <T> T getParameter(String name) { return (T) getRequest().getParameter(name); } /** * ?IP. */ public static String getIp() { HttpServletRequest request = getRequest(); return IpUtils.getIpAddr(request); } }