阅读Web应用程序资源 [英] Reading Web Application Resources

查看:80
本文介绍了阅读Web应用程序资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发一个简单的Web应用程序(Eclipse + JBoss + Apache Tomcat)以生成XML文件.

Developing a simple web application (Eclipse + JBoss + Apache Tomcat) to generate XML files.

业务区域"列表针对数据库查询,列集群"列表使用选定的业务区域"项目查询数据库.这两个都是存储外部文本文件的唯一查询.

The "Business Area" list queries against the database, and the "Column Cluster" list queries the database using the selected "Business Area" items. Both of these are unique queries that are stored external text files.

文件当前存储在以下位置:

The files are currently stored in the following locations:

  • WebContent/META-INF/business-areas.sql
  • WebContent/META-INF/column-clusters.sql

然后将这些种子用作PreparedStatement的种子.

These are then used to seed PreparedStatements.

读取SQL代码的方法可能类似于:

The method to read the SQL code might resemble:

  private String getSQL() {
    String result = "";

    try {
      BufferedReader br = open( "business-areas.sql" );
      String line = null;

      while( (line = br.readLine()) != null ) {
        result += line;
      }

      br.close();
    }
    catch( Exception e ) {
      e.printStackTrace();
    }

    return result;
  }

问题

我想知道:

Questions

I would like to know:

  1. 将此类资产存储为Web应用程序的一部分的最佳实践是什么? (也就是说,META-INF是个好位置,还是META-INF/resources是首选位置?)
  2. 您建议使用哪些API读取文件内容? (也就是说,我该如何编写open方法,以便它找到要打开的文件?)
  1. What are the best practices for storing such assets for deployment as part of a web app? (That is, is META-INF a good location, or is META-INF/resources preferred?)
  2. What APIs would you recommend for reading the file content? (That is, how do I write the open method so that it finds the files to open?)

我已经拥有建立数据库连接的JNDI,但是如果可能的话,宁愿不使用JNDI获取文件的句柄.

I already have JNDI in place to establish the database connection, but would rather not use JNDI to obtain handles to the files, if possible.

  • http://blogs.oracle.com/alexismp/entry/web_inf_lib_jar_meta
  • http://www.avajava.com/tutorials/lessons/where-do-i-put-resources-in-my-maven-project.html
  • http://docs.jboss.org/jbossweb/3.0.x/config/context.html
  • http://tomcat.apache.org/tomcat-4.0-doc/catalina/docs/api/org/apache/naming/resources/FileDirContext.html

谢谢!

推荐答案

正确的位置(也是常见的做法)是将它们放在您的source目录下,然后将其编译到WEB-INF/classes目录中.我不确定您对@Dave的响应中的类目录是否易变",但这就是大多数(如果不是全部)Java Web应用程序存储事物的方式. WEB-INF/classes不仅适用于Java类.通常会看到存储在source目录下的日志记录属性文件(例如log4j),Hibernate和Spring XML文件,并且您可以使用以下类似方法安全地访问文件:-

The right location (and also the common practice) is to place them under your source directory, which will then gets compiled into WEB-INF/classes directory. I'm not sure what you meant by "classes directory is volatile" in your response to @Dave, but this is how most (if not all) Java web apps store things. WEB-INF/classes is not just for Java classes. It's common to see logging properties file (like log4j), Hibernate and Spring XML files stored under source directory and you can safely access the files using something like this:-

// in this case, the business-areas.sql is located right under "source/sql" directory
InputStream is = getClass().getClassLoader().getResourceAsStream("sql/business-areas.sql");
BufferedReader br = new BufferedReader(new InputStreamReader(is));

关于使用META-INF的一些有用信息:> INF?

Some useful information about the use of META-INF: What's the purpose of META-INF?

这篇关于阅读Web应用程序资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆