背景介绍

项目中用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和相对路径获取资源。

总结

两种方式,分两步,可得四种组合。

Technorati 标签: web资源获取

在class中获取web资源的更多相关文章

  1. JSP中的内置对象和Struts中的Web资源的详解

    JSP中的内置对象有如下几种: request :继承于HttpServletRequest, HttpServletRequest继承ServletRequest, 获得的Request对象的方法: ...

  2. PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次?【坑】

    PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次? 因为我这样做,结果后面的查询结果就无法显示了,目前尚不 ...

  3. 在Action 中访问web资源

    1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...

  4. struts2基础——请求与响应、获取web资源

    一.请求与响应 Action1.含义:(1) struts.xml 中的 action 元素,也指 from 表单的 action 属性,总之代表一个 struts2 请求.(2) 用于处理 Stru ...

  5. Struts2在Action中访问WEB资源

    什么是WEB资源? 这里所说的WEB资源是指:HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API. 为什么需要访问WEB资 ...

  6. struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)

    最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...

  7. Spring中获取web项目的根目录

    spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能; WebAppRootListe ...

  8. Struts2中获取Web元素request、session、application对象的四种方式

    我们在学习web编程的时候,一般都是通过requet.session.application(servletcontext)进行一系列相关的操作,request.session.和applicatio ...

  9. Struts2学习第四课 通过Aware接口获取WEB资源

    使用XxxAware接口 看代码: package logan.struts2.study; import java.util.Map; import org.apache.struts2.inter ...

随机推荐

  1. 2017 济南综合班 Day 4

    T1 外星人 二维前缀和 #include<cstdio> #define N 1001 using namespace std; bool v[N][N]; int sum[N][N]; ...

  2. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  3. c# 设置和取消文件夹共享及执行Dos命令

    /// <summary> /// 设置文件夹共享 /// </summary> /// <param name="FolderPath">文件 ...

  4. OScached页面缓存的入门使用

    OSCache的使用: 一,环境的搭建: 1,把oscache.jar file放在 /WEB-INF/lib 目录下(Put the oscache.jar file in the /WEB-INF ...

  5. 省队集训Day1 总统选举

    [题目大意] 一个$n$个数的序列,$m$次操作,每次选择一段区间$[l, r]$,求出$[l, r]$中出现超过一半的数. 如果没有超过一半的数,那么就把答案钦定为$s$,每次会有$k$个数进行改变 ...

  6. 【51NOD】数据流中的算法

    [算法]数学 [题解] 1.平均数:累加前缀和.//听说要向下取整? 2.中位数:双堆法,大于中位数存入小顶堆,小于中位数存入大顶堆,保证小顶堆内数字数量≥大顶堆,奇数则取小堆顶,偶数则取两堆顶/2. ...

  7. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

  8. 【HNOI】d 最小割

    [题目大意]给定一个n*m的土地,每块可以种a或b作物,每种作物在不同的位置有不同的收成,同时,有q个子矩阵中,全部种指定的作物(a或b)会有一定的加成收成,求最大收成. [数据范围] 50% n,m ...

  9. 第一章:获取服务器服务banner

    #!c:\\perl\\bin\\perl.exe #读取服务器的首行(banner) use IO::Socket; my $service = '121.201.67.177:ssh'; my $ ...

  10. 如何用CMD开启3389与查看3389端口

    开启 REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections / ...