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

查看:17
本文介绍了阅读 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

然后这些用于为 PreparedStatements 做种子.

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;
  }

问题

我想知道:

  1. 存储此类资产以作为网络应用的一部分进行部署的最佳做法是什么?(也就是说,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.

谢谢!

推荐答案

正确的位置(也是常见的做法)是将它们放在您的 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 的一些有用信息:META 的用途是什么-INF?

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

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

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