在class中获取web资源
背景介绍
项目中用jasperreport做报表,模板文件为web资源,不在classpath之中。class又需要获取模板文件,结合数据源,生成pdf格式的报表。
之前的做法是定义一个public static byte[] getPdfBytes(HttpServletRequest request)
的接口,以request.getServletContext().getRealPath(String relativePath)
的方式获取web资源的绝对路径,再根据绝对路径获取资源。
这样一来,这个方法的调用者就直接或者间接的受限于Servlet,或者说这个方法就是专门为Servlet而准备的。
我们理想中的接口是这样的public InputStream getResource(String relativePath),只需要正确的相对路径,就可以获取对应资源的内容。
基于Servlet的方案
1、定义ServletContextListener,缓存ServletContext
package cn.ljl.javaweb.demo.util; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* 用于缓存并提供当前{@link ServletContext}.
* @author lijinlong
*
*/
@WebListener
public class ServletContextHolder implements ServletContextListener { private static ServletContext context; /**
* @return the context
*/
public static ServletContext getContext() {
return context;
} @Override
public void contextInitialized(ServletContextEvent sce) {
context = sce.getServletContext();
} @Override
public void contextDestroyed(ServletContextEvent sce) {
context = null;
} }
批注:
在警综新框架中,监听器和上下文持有工具类是分开的:监听器为cn.sinobest.jzpt.framework.exception.web.WebContextListener,持有工具类为cn.sinobest.jzpt.framework.exception.web.WebContextHolderUtil。同事们只需直接使用,不用再另行定义了。
2、通过ServletContext获取web资源
package cn.ljl.javaweb.demo.util; import java.io.InputStream; import javax.servlet.ServletContext; /**
* web资源获取工具类.
* @author lijinlong
*
*/
public class WebResourceUtil {
/**
* 根据相对路径,获取web资源输入流.
* @param realPath 资源的相对路径.
* @return
*/
public static InputStream getResource(String realPath) {
ServletContext context = ServletContextHolder.getContext();
InputStream is = context.getResourceAsStream(realPath);
return is;
}
}
基于Spring的方案
1、定义ApplicationContextAware的实现类
package cn.sinobest.jzpt.framework.utils; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class ApplicationContextManagement implements ApplicationContextAware{
private static ApplicationContext appContext; public static void init(ApplicationContext appContext){
ApplicationContextManagement.appContext = appContext;
} public static ApplicationContext getApplicationContext(){
return appContext;
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
appContext = applicationContext;
} }
需要基于上述类,定义spring的bean。
批注:
上述类是警综新框架中的真实工具类,同事们可以直接使用。
2、基于spring ServletContextResource获取资源
org.springframework.web.context.WebApplicationContext wac = (org.springframework.web.context.WebApplicationContext)
cn.sinobest.jzpt.framework.utils.ApplicationContextManagement.getApplicationContext();
resource = new org.springframework.web.context.support.ServletContextResource(
wac.getServletContext(), path);
java.io.InputStream is = resource.getInputStream();
批注:
第1步的作用依然是获取ServletContext,第2步根据ServletContext和相对路径获取资源。
总结
两种方式,分两步,可得四种组合。
在class中获取web资源的更多相关文章
- JSP中的内置对象和Struts中的Web资源的详解
JSP中的内置对象有如下几种: request :继承于HttpServletRequest, HttpServletRequest继承ServletRequest, 获得的Request对象的方法: ...
- PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次?【坑】
PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次? 因为我这样做,结果后面的查询结果就无法显示了,目前尚不 ...
- 在Action 中访问web资源
1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...
- struts2基础——请求与响应、获取web资源
一.请求与响应 Action1.含义:(1) struts.xml 中的 action 元素,也指 from 表单的 action 属性,总之代表一个 struts2 请求.(2) 用于处理 Stru ...
- Struts2在Action中访问WEB资源
什么是WEB资源? 这里所说的WEB资源是指:HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API. 为什么需要访问WEB资 ...
- struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)
最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...
- Spring中获取web项目的根目录
spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能; WebAppRootListe ...
- Struts2中获取Web元素request、session、application对象的四种方式
我们在学习web编程的时候,一般都是通过requet.session.application(servletcontext)进行一系列相关的操作,request.session.和applicatio ...
- Struts2学习第四课 通过Aware接口获取WEB资源
使用XxxAware接口 看代码: package logan.struts2.study; import java.util.Map; import org.apache.struts2.inter ...
随机推荐
- Mybatis 参考
1:Mybatis最入门---ResultMaps基本用法 2:Mybatis最入门---ResultMaps高级用法(上) 3:Mybatis最入门---ResultMaps高级用法(下) 4:My ...
- LightOJ 1306 - Solutions to an Equation 裸EXGCD
本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...
- [Luogu 1963] NOI2009 变换序列
[Luogu 1963] NOI2009 变换序列 先%Dalao's Blog 什么?二分图匹配?这个确定可以建图? 「没有建不成图的图论题,只有你想不出的建模方法.」 建图相当玄学,不过理解大约也 ...
- 使用Apache Curator监控Zookeeper的Node和Path的状态
1.Zookeeper经常被我们用来做配置管理,配置的管理在分布式应用环境中很常见,例如同一个应用系统需要多台 PC Server 运行,但是它们运行的应用系统的某些配置项是相同的,如果要修改这些相同 ...
- IIS7绑定多个HTTPS网站并应用自签名证书
本文主要介绍如何在IIS中添加多个网站并使用同一个数字签名证书(win7+IIS7.5) IIS中添加站点site1,端口号为80,主机名为空.如下图: 创建证书 IIS->Server Cer ...
- IIS 搭建
1. 在打开程序功能里面,点击IIS安装.注意要选择适当的各种有用的服务.例如默认文档就需要安装非IIS下面的选项. 2. IIS部署网站可以参考网上的步骤.会遇到500处理程序“Extensionl ...
- bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛——差分
Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. E ...
- CSS3 动画实现方法大全
常用效果总结(需要引用animate.css) <!doctype html> <html lang="en"> <head> <meta ...
- Coursera在线学习---第九节(2).推荐系统
一.基于内容的推荐系统(Content Based Recommendations) 所谓基于内容的推荐,就是知道待推荐产品的一些特征情况,将产品的这些特征作为特征变量构建模型来预测.比如,下面的电影 ...
- Codeforces Round #483 (Div. 1) 简要题解
来自FallDream的博客,未经允许,请勿转载,谢谢. 为了证明一下我又来更新了,写一篇简要的题解吧. 这场比赛好像有点神奇,E题莫名是道原题,导致有很多选手直接过掉了(Claris 表演24s过题 ...