SpringMVC Http请求工具代码类
在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请求工具代码类的更多相关文章
- Android开发之强大的网络判断工具,判断是否联网,判断是wifi还是3g网络等java工具代码类
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985, 转载请说明出处. 给大家分享一个Android开发者常用的工具类.主要针对网络判断的 功能强大.下面 ...
- WebUtils-网络请求工具类
网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...
- 微信https请求工具类
工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...
- HTTP请求工具类
HTTP请求工具类,适用于微信服务器请求,可以自测 代码; /// <summary> /// HTTP请求工具类 /// </summary> public class Ht ...
- springMVC的一些工具类
springMVC的一些工具类,主要有转换器,读取器 读取文件: package cn.edu.hbcf.common.springmvc; import java.util.HashMap; imp ...
- C#实现的UDP收发请求工具类实例
本文实例讲述了C#实现的UDP收发请求工具类.分享给大家供大家参考,具体如下: 初始化: ListeningPort = int.Parse(ConfigurationManager.AppSetti ...
- 我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类
下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确,也不需要验证服务器证书中的域名是否有效. (PS:建议下 ...
- Java开发微信公众号(三)---微信服务器请求消息,响应消息,事件消息以及工具处理类的封装
在前面几篇文章我们讲了微信公众号环境的配置 和微信公众号服务的接入,接下来我们来说一下微信服务器请求消息,响应消息以及事件消息的相关内容,首先我们来分析一下消息类型和返回xml格式及实体类的封装. ( ...
- java模板模式项目中使用--封装一个http请求工具类
需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...
随机推荐
- 阿里云centos5升级yum源为6
升级后出现Errno -3] Error performing checksum 需要安装 python-hashlib Python 2.4 安装 hashlib 2012年11月13日 14:29 ...
- 关于各种BUF源语的研究
关于各种BUF源语的研究 资料来源: 单端信号需要用到的BUF 关于这些源语的约束: 增大驱动电流 关于管脚的上拉与下拉约束: ODDR的两种操作模式 关于ODDR输出时钟的应用 为什么ODDR需要这 ...
- C# Interview Question 1
What is C#? What's the feature of C# language? Answer:
- ubuntu命令行打开网页
在Ubuntu下,当需要打开其他格式文件时,比如pdf.jpg.mp3等格式文件,通常做法是进入到文件所在的目录,双击打开,很影响效率.事实上,可以通过命令xdg-open打开这些格式文件,甚至是网页 ...
- Idea中重建maven模块,dependencies引入为空的解决办法
使用idea开发Maven项目时, Maven项目中有些模块被删除了,重新加入相同名字的模块时,dependencies为空 如下图 正常引进的项目,dependencies应如下 解决办法: ide ...
- python2的reload模块
在刚开始写python程序的时候,都会遇到一个很头疼的问题——编码错误,在之前的文章中也做了介绍: 由__future__中unicode_literals引起的错误来研究python中的编码问题 . ...
- 2017-2018-2 20165312 实验四《Android程序设计》实验报告
2017-2018-2 20165312 实验四<Android程序设计>实验报告 一.安装Android Studio并进行Hello world测试和调试程序 安装Android St ...
- 采用link方式解决zabbix对于备份监控和ORACLE日志监控由于路径不统一的问题
#对于备份监控和ORACLE日志监控由于路径不统一,我们可以采用link的方式如:#ln -s 原路径 新路径(/zabbix/logs)#新路径统一放在/zabbix/logs下具体看模板指定. # ...
- Hibernate工作原理及为什么要用?
1.原理: 1.读取并解析配置文件 new Configuration().configure()2.读取并解析映射信息,创建SessionFactory sf=buildSessionF ...
- java面试题收集
http://www.cnblogs.com/yhason/archive/2012/06/07/2540743.html 2,java常见面试题 http://www.cnblogs.com/yha ...