在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 ...
随机推荐
- Beagleboneblack的MLO文件干了些啥
Beagleboneblack在启动linux之前还有三个启动阶段: ROM code --> MLO --> u-boot --> kernel 先看看ROM code干了 ...
- 局部性原理的点滴应用场景 use of localityprinciple
话说九月份博士入学面试的时候被问到了一个问题:请说明一下局部性原理在计算机科学中的应用场景?(哈哈,不记得怎么问的了,大概是这个意思)但是巴拉巴拉整半天却也只说出了一个Cache,后来补充的也都是跟C ...
- C语言数据结构-栈
一.栈的定义 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加). 移出元 ...
- Frogs' Neighborhood(POJ1659+Havel-Hakimi定理)
题目链接:http://poj.org/problem?id=1659 题目: 题意:根据他给你的每个点的度数构造一张无向图. 思路:自己WA了几发(好菜啊……)后看到discuss才知道这个要用Ha ...
- HDU 1026 Ignatius and the Princess I (广搜)
题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...
- idea编写的java代码,在cmd运行乱码解决方案
1.解决方案 使用txt打开,另存为的时候选择编码为ANSI 即可.
- 网页实现插入图片—css与html的区别
Q1.二者有何区别?A1.写在css里面的图片是以背景图形式存在的,而写在html里的是以<img>标签形式存在的,在网页加载的过程中,以css背景图存在的图片会等到结构加载完成(网页的内 ...
- Eureka服务下线(Cancel)源码分析
Cancel(服务下线) 在Service Provider服务shut down的时候,需要及时通知Eureka Server把自己剔除,从而避免其它客户端调用已经下线的服务,导致服务不可用. co ...
- 一个python拖库字段的小脚本
import requests import re all_column = dict() all_db = "db_zf,dg_activity,dg_activity_log,dg_ad ...
- 分布式队列Celery
Celery是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任务 ...