WEB-INF 在 Java EE Web 应用程序中的用途是什么? [英] What is WEB-INF used for in a Java EE web application?

查看:30
本文介绍了WEB-INF 在 Java EE Web 应用程序中的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有以下源代码目录结构的 Java EE Web 应用程序:

I'm working on a Java EE web application with the following source code directory structure:

src/main/java                 <-- multiple packages containing Java classes
src/test/java                 <-- multiple packages containing JUnit tests
src/main/resources            <-- includes properties files for textual messages
src/main/webapp/resources     <-- includes CSS, images and all Javascript files
src/main/webapp/WEB-INF
src/main/webapp/WEB-INF/tags
src/main/webapp/WEB-INF/views

我感兴趣的文件夹是 WEB-INF:它包含 web.xml、用于设置 servlet 的 XML 文件、Spring bean 连接上下文和 JSP 标签以及意见.我试图了解是什么限制/定义了这个结构.例如.JSP 文件总是必须在 WEB-INF 内还是可以在其他地方?WEB-INF 中是否还有其他内容?维基百科的 WAR files 条目提到了 Java 类的 classes 和用于 JAR 文件的 lib - 除了其他源文件位置之外,我不确定我是否完全掌握了何时需要这些文件.

The folder I'm interested in is WEB-INF: It contains web.xml, XML files for setting up servlets, Spring bean wiring contexts and JSP tags and views. I'm trying to understand what constrains/defines this structure. E.g. do JSP files always have to be within WEB-INF or could they be somewhere else? And is there anything else that might go in WEB-INF? Wikipedia's WAR files entry mentions classes for Java classes and lib for JAR files - not sure I've fully grasped when these would be needed in addition to the other source file locations.

推荐答案

Servlet 2.4 规范 谈到了 WEB-INF(第 70 页):

The Servlet 2.4 specification says this about WEB-INF (page 70):

应用程序层次结构中存在一个特殊目录,名为WEB-INF.这个目录包含了所有相关的东西不在应用程序文档根目录中的应用程序.WEB-INF 节点不是公共文档树的一部分应用.不能提供 WEB-INF 目录中包含的文件通过容器直接发送给客户端.然而,该文件的内容WEB-INF 目录对使用 getResource 的 servlet 代码可见和 getResourceAsStream 方法调用 ServletContext,并且可能使用 RequestDispatcher 调用公开.

A special directory exists within the application hierarchy named WEB-INF. This directory contains all things related to the application that aren’t in the document root of the application. The WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext, and may be exposed using the RequestDispatcher calls.

这意味着 WEB-INF 资源可被您的 Web 应用程序的资源加载器访问,而不是直接对公众可见.

This means that WEB-INF resources are accessible to the resource loader of your Web-Application and not directly visible for the public.

这就是为什么许多项目将他们的资源(如 JSP 文件、JAR/库和他们自己的类文件或属性文件或任何其他敏感信息)放在 WEB-INF 文件夹中的原因.否则,可以通过使用简单的静态 URL 来访问它们(例如用于加载 CSS 或 Javascript).

This is why a lot of projects put their resources like JSP files, JARs/libraries and their own class files or property files or any other sensitive information in the WEB-INF folder. Otherwise they would be accessible by using a simple static URL (usefull to load CSS or Javascript for instance).

从技术角度来看,您的 JSP 文件可以在任何地方.例如在 Spring 中,您可以将它们显式配置为 WEB-INF:

Your JSP files can be anywhere though from a technical perspective. For instance in Spring you can configure them to be in WEB-INF explicitly:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" 
    p:suffix=".jsp" >
</bean>

维基百科WAR files 文章是 Servlet 规范在运行时所需的文件夹示例.

The WEB-INF/classes and WEB-INF/lib folders mentioned in Wikipedia's WAR files article are examples of folders required by the Servlet specification at runtime.

区分项目结构和生成的 WAR 文件结构很重要.

在某些情况下,项目的结构会部分反映 WAR 文件的结构(对于静态资源,例如 JSP 文件或 HTML 和 JavaScript 文件,但并非总是如此.

The structure of the project will in some cases partially reflect the structure of the WAR file (for static resources such as JSP files or HTML and JavaScript files, but this is not always the case.

从项目结构到生成的 WAR 文件的转换是通过构建过程完成的.

虽然您通常可以自由设计自己的构建过程,但现在大多数人会使用标准化的方法,例如 Apache Maven.Maven 为项目结构中的哪些资源映射到结果工件中的哪些资源定义了默认值(在这种情况下,结果工件是 WAR 文件).在某些情况下,映射由简单的复制过程组成,而在其他情况下,映射过程包括转换,例如过滤或编译等.

While you are usually free to design your own build process, nowadays most people will use a standardized approach such as Apache Maven. Among other things Maven defines defaults for which resources in the project structure map to what resources in the resulting artifact (the resulting artifact is the WAR file in this case). In some cases the mapping consists of a plain copy process in other cases the mapping process includes a transformation, such as filtering or compiling and others.

一个示例:WEB-INF/classes 文件夹稍后将包含所有已编译的 java 类和资源(src/main/javasrc/main/resources) 需要由类加载器加载以启动应用程序.

One example: The WEB-INF/classes folder will later contain all compiled java classes and resources (src/main/java and src/main/resources) that need to be loaded by the Classloader to start the application.

另一个示例:WEB-INF/lib 文件夹稍后将包含应用程序所需的所有 jar 文件.在 maven 项目中,依赖项会为您管理,maven 会自动为您复制所需的 jar 文件到 WEB-INF/lib 文件夹.这解释了为什么在 Maven 项目中没有 lib 文件夹.

Another example: The WEB-INF/lib folder will later contain all jar files needed by the application. In a maven project the dependencies are managed for you and maven automatically copies the needed jar files to the WEB-INF/lib folder for you. That explains why you don't have a lib folder in a maven project.

这篇关于WEB-INF 在 Java EE Web 应用程序中的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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