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 ...
随机推荐
- Linux内核分析第六次作业
分析system_call中断处理过程 一.先在实验楼的虚拟机中MenuOs增加utsname和utsname-asm指令. 具体实现如下: 1.克隆最新新版本的menu,之后进入menu 2.进入t ...
- Python模块hashlib
Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制 ...
- calibre的注册表残留删除
卸载calibre后,注册表仍有残留 for /f %a in ('reg query HKEY_CLASSES_ROOT /f calibre /k') do reg delete %a /f
- Reachability实时监控网络变化
Reachability是一种实时观察网络发生变化控件,如当你的手机处于WiFi情况下,他就会检测环境,当处于GPS的情况下改变环境,当处于无网络的情况下又是一种环境.下面我们看下关于Reachabi ...
- rabbitmq (四) 路由
上文讲的是广播类型fanout 本章讲 direct和topic. 当使用广播类型fanout的时候: routingKey字段不起作用. direct:精确匹配 routingKey:匹配一个单词, ...
- 消息对话框 MessageBoxButtons
消息对话框MessageBox经常用于向用户显示通知信息.例如,在操作过程中遇到错误或程序异常,经常会使用这种方式给用户以提示,它是特殊类型的对话框. 在C#中,MessageBox消息对话框位于Sy ...
- Java 泛型的作用及其基本概念
一.泛型的基本概念 java与c#一样,都存在泛型的概念,及类型的参数化.java中的泛型是在jdk5.0后出现的,但是java中的泛型与C#中的泛型是有本质区别的,首先从集合类型上来说,java 中 ...
- 深入理解Servlet原理
Servlet介绍: 1. 如何返回一个网页: a. 静态网页 >新闻页.百度百科 >服务器保存一份HTML,直接返回给浏览器即可 b. 动态网页 >淘宝.QQ空间.微博 >服 ...
- Python与设计模式之创建型模式及实战
用Python学习一下设计模式,如果很枯燥的话,就强行能使用的就用一下.设计模式参考Python与设计模式-途索 1. 单例模式 保证一个类仅有一个实例,并提供一个访问它的全局访问点. import ...
- 6. 添加messager.alert()确定按钮的回调函数,即点完确定按钮后触发的事件
添加messager.alert()确定按钮的回调函数,即点完确定按钮后触发的事件: $.messager.alert('提示信息', "请联系管理员处理!", 'info', f ...