在SpringMVC的源代码中也提供了一个封装过的ThreadLocal,其中保存了每次请求的HttpServletRequest对象,(详细请看org.springframework.web.context.request.ServletRequestAttributes的源代码) 。

这样我们就可以进行简单封装一下写一个工具进行使用:

*当然必不可少的一点一定记得,在web.xml里必须要有相应的配置来支持 

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener> 工具类代码: package samples.utils; import java.util.Arrays;
import java.util.Collection;
import java.util.Locale; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.MessageSource;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.ui.context.Theme;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.support.RequestContextUtils; /**
* SpringMVC通用工具
* @author
*/
public final class WebContextHolder { private static final Logger LOGGER = LoggerFactory.getLogger(WebContextHolder.class);
private static final WebContextHolder INSTANCE = new WebContextHolder(); public WebContextHolder get() {
return INSTANCE;
} private WebContextHolder() {
super();
} // -------------------------------------------------------------------------------------------------------------- public HttpServletRequest getRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return attributes.getRequest();
} public HttpSession getSession() {
return getSession(true);
} public HttpSession getSession(boolean create) {
return getRequest().getSession(create);
} public String getSessionId() {
return getSession().getId();
} public ServletContext getServletContext() {
return getSession().getServletContext(); // servlet2.3
} public Locale getLocale() {
return RequestContextUtils.getLocale(getRequest());
} public Theme getTheme() {
return RequestContextUtils.getTheme(getRequest());
} public ApplicationContext getApplicationContext() {
return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
} public ApplicationEventPublisher getApplicationEventPublisher() {
return (ApplicationEventPublisher) getApplicationContext();
} public LocaleResolver getLocaleResolver() {
return RequestContextUtils.getLocaleResolver(getRequest());
} public ThemeResolver getThemeResolver() {
return RequestContextUtils.getThemeResolver(getRequest());
} public ResourceLoader getResourceLoader() {
return (ResourceLoader) getApplicationContext();
} public ResourcePatternResolver getResourcePatternResolver() {
return (ResourcePatternResolver) getApplicationContext();
} public MessageSource getMessageSource() {
return (MessageSource) getApplicationContext();
} public ConversionService getConversionService() {
return getBeanFromApplicationContext(ConversionService.class);
} public DataSource getDataSource() {
return getBeanFromApplicationContext(DataSource.class);
} public Collection<String> getActiveProfiles() {
return Arrays.asList(getApplicationContext().getEnvironment().getActiveProfiles());
} public ClassLoader getBeanClassLoader() {
return ClassUtils.getDefaultClassLoader();
} private <T> T getBeanFromApplicationContext(Class<T> requiredType) {
try {
return getApplicationContext().getBean(requiredType);
} catch (NoUniqueBeanDefinitionException e) {
LOGGER.error(e.getMessage(), e);
throw e;
} catch (NoSuchBeanDefinitionException e) {
LOGGER.warn(e.getMessage());
return null;
}
} }

SpringMVC Http请求工具代码类的更多相关文章

  1. Android开发之强大的网络判断工具,判断是否联网,判断是wifi还是3g网络等java工具代码类

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985, 转载请说明出处. 给大家分享一个Android开发者常用的工具类.主要针对网络判断的 功能强大.下面 ...

  2. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

  3. 微信https请求工具类

    工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...

  4. HTTP请求工具类

    HTTP请求工具类,适用于微信服务器请求,可以自测 代码; /// <summary> /// HTTP请求工具类 /// </summary> public class Ht ...

  5. springMVC的一些工具类

    springMVC的一些工具类,主要有转换器,读取器 读取文件: package cn.edu.hbcf.common.springmvc; import java.util.HashMap; imp ...

  6. C#实现的UDP收发请求工具类实例

    本文实例讲述了C#实现的UDP收发请求工具类.分享给大家供大家参考,具体如下: 初始化: ListeningPort = int.Parse(ConfigurationManager.AppSetti ...

  7. 我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类

    下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确,也不需要验证服务器证书中的域名是否有效. (PS:建议下 ...

  8. Java开发微信公众号(三)---微信服务器请求消息,响应消息,事件消息以及工具处理类的封装

    在前面几篇文章我们讲了微信公众号环境的配置 和微信公众号服务的接入,接下来我们来说一下微信服务器请求消息,响应消息以及事件消息的相关内容,首先我们来分析一下消息类型和返回xml格式及实体类的封装. ( ...

  9. java模板模式项目中使用--封装一个http请求工具类

    需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...

随机推荐

  1. laravel路由别名

    在定义路由时使用数组键 as 指定路由名称: Route::get('user/profile', ['as' => 'profile', function () { // }]); 另外,还可 ...

  2. Python——列表、元祖、字典、集合的基本操作

    列表 1. 列表——增 (1)append li = ['xcsd', 'cdc', [1, 5, 2], 'eht', '辛辰'] li.append('nihao') print(li) #['x ...

  3. mnist全连接层网络权值可视化

    一.数据准备 网络结构:lenet_lr.prototxt 训练好的模型:lenet_lr_iter_10000.caffemodel 下载地址:链接:https://pan.baidu.com/s/ ...

  4. vagrant The specified host network collides with a non-hostonly network!

    换个ip scripts\homestead.rb config.vm.network :private_network, ip: settings["ip"] ||= " ...

  5. 二进制 转换成十进制 BCD码(加3移位法)

    "原来的二进制数十几位,则左移时就要左移几位" "二进制数调整BCD码的方法是将二进制码左移8次,每次移位后都检查低四位LSD+3是否大于7,如是则加3,否则不加,高4位 ...

  6. nginx http转 https

    场景 项目前期使用http,后期为了安全方面的考虑,启用了https.项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡.实际上只需要在nginx上启用https即可,使客户端与 ...

  7. java中int算法的有趣现象

    今天无意中发现一个怪事,当时没理解,后来跟网友讨论了才知道原理,是关于int值的加法算法,两段代码如下: 代码1: @Test public void test1() { ; ; try { whil ...

  8. mysql Mac篇

    默认为mysql下载和安装完毕,安装为默认安装 下载地址:https://dev.mysql.com/downloads/file/?id=473576 1.启动mysql sudo /usr/loc ...

  9. python3:实现字符串的全排列(无重复字符)

    最近在学一些基础的算法,发现我的数学功底太差劲了,特别是大学的这一部分,概率论.线性代数.高数等等,这些大学学的我是忘得一干二净(我当时学的时候也不见得真的懂),导致现在学习算法,非常的吃力.唉!不说 ...

  10. jquery中live is not a function的问题

    jquery中的live()方法在jquery1.9及以上的版本中已被废弃了,如果使用,会抛出TypeError: $(...).live is not a function错误. 解决方法: 之前的 ...