背景介绍

项目中用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. intellij idea 破解补丁激活

    一.说明 idea激活可以用JetBrains account,Activation Code注册码或者填License server网址,使用注册码的方式可以参考lanyun提供的注册码,但是有效时 ...

  2. Flask 应用上下文和请求上线文原理图

  3. centos7.2编译安装zabbix-3.0.4

    安装zabbix-3.0.4 #安装必备的包 yum -y install gcc* make php php-gd php-mysql php-bcmath php-mbstring php-xml ...

  4. Jmeter-8-FTP测试

    1. 此处要深刻理解FTP的用法. 2. Get的时候填写的Remote File 路径/, 此处是相对路径. 实际为/home/user/ 3. Local file 此处要写到具体的文件. 4. ...

  5. 【NOIP】提高组2016 愤怒的小鸟

    [题意]Universal Online Judge [算法]状态压缩型DP [题解]看数据范围大概能猜到是状压了. 根据三点确定一条抛物线,枚举两个点之间的抛物线,再枚举有多少点在抛物线上(压缩为状 ...

  6. POJ 2533 Longest Ordered Subsequence LIS O(n*log(n))

    题目链接 最长上升子序列O(n*log(n))的做法,只能用于求长度不能求序列. #include <iostream> #include <algorithm> using ...

  7. 大聊Python----IO口多路复用

    什么是IO 多路复用呢? 我一个SocketServer有500个链接连过来了,我想让500个链接都是并发的,每一个链接都需要操作IO,但是单线程下IO都是串行的,我实现多路的,看起来像是并发的效果, ...

  8. Android中自定义属性attr.xml的格式详解

    1. reference:参考某一资源ID.     (1)属性定义:             <declare-styleable name = "名称">      ...

  9. Java——关于static关键字的那些事总结

    前言: 先说说今天为啥要谈这个东西,虽然学Java已经有两年了,但是今天,本着温故而知新的态度,仔细的第三次翻看了<Head Firt Java>这本书,虽然这本书介绍的很多东西都特别基础 ...

  10. Mojo_1_第一个简单例子

    use Mojolicious::Lite; #根目录,Get方法打开 #正接显示文本text get '/' => sub{ my $service = shift; $service-> ...