Spring:工具类 WebContextHolder
2021-01-28 01:15
标签:工具类 gets 类型 Servle value ons framework contex 参数 Spring:工具类 WebContextHolder 标签:工具类 gets 类型 Servle value ons framework contex 参数 原文地址:https://www.cnblogs.com/jiangbo44/p/12842326.html环境
源码
package jiangbo.springweb.context;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public final class WebContextHolder {
/**
* Constructor
*/
private WebContextHolder() {
}
/**
* get Request
*
* @return HttpServletRequest
*/
public static HttpServletRequest getRequest() {
return (((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest());
}
/**
* get Response
*
* @return {@link HttpServletResponse}
*/
public static HttpServletResponse getResponse() {
return (((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse());
}
/**
* get Http Session
*
* @return Http Session
*/
public static HttpSession getSession() {
return getRequest().getSession();
}
/**
* get Request Attribute
*
* @param name name
* @return Object
*/
public static Object getRequestAttribute(String name) {
return getRequest().getAttribute(name);
}
/**
* get Request Attribute
*
* @param
*
* @param name name
* @return 从请求中获取的值
*/
public static String getRequestString(String name) {
return getRequestAttribute(name, String.class);
}
/**
* get Session Attribute
*
* @param name name
* @return Object
*/
public static Object getSessionAttribute(String name) {
return getSession().getAttribute(name);
}
/**
* get Session Attribute
*
* @param
*
* @param name session中的key
* @return 从session中获取的值
*/
public static String getSessionString(String name) {
return getSessionAttribute(name, String.class);
}
/**
* get Servlet Context
*
* @return ServletContext
*/
public static ServletContext getServletContext() {
return getSession().getServletContext();
}
/**
* set Request Attribute
*
* @param name name
* @param value value
*/
public static void setRequestAttribute(String name, Object value) {
getRequest().setAttribute(name, value);
}
/**
* set Session Attribute
*
* @param name name
* @param value value
*/
public static void setSessionAttribute(String name, Object value) {
getSession().setAttribute(name, value);
}
}
文章标题:Spring:工具类 WebContextHolder
文章链接:http://soscw.com/index.php/essay/48007.html