什么是用于Java EE Web应用程序的WEB-INF? [英] What is WEB-INF used for in a Java EE web application?
问题描述
我正在使用以下源代码结构编写Java EE Web应用程序:
I'm working on a Java EE web application with the following source code 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
I位感兴趣的是 WEB-INF
- 它包含 web.xml
,用于设置servlet的XML文件,Spring bean接线上下文和JSP标签和视图。
The bit 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.
问题标题的基本但我想要了解的是约束/定义这个结构。例如。 JSP文件总是必须在 WEB-INF
之内,还是可能在其他地方?还有其他什么可以进入 WEB-INF
?维基百科的 WAR文件条目提到类
用于Java类, lib
用于JAR文件 - 除了其他源文件位置之外,还不确定我是否完全掌握了这些文件。
The question title's basic but what I'm trying to understand is what constrains/defines this structure. E.g. would 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
$ b的servlet代码是可见的$ b和getResourceAsStream
ServletContext
上的方法调用,并且可以使用<$ c $公开
c> 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. TheWEB-INF
node is not part of the public document tree of the application. No file contained in theWEB-INF
directory may be served directly to a client by the container. However, the contents of theWEB-INF
directory are visible to servlet code using thegetResource
andgetResourceAsStream
method calls on theServletContext
, and may be exposed using theRequestDispatcher
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 /库以及它们自己的类文件或属性文件或任何其他敏感信息放在<$ c中$ c> 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>
WEB-INF / classes
和 WEB-INF / lib
维基百科中提到的文件夹WAR文件文章是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文件的转换是由构建过程完成。
The transition from the project structure into the resulting WAR file is done by a build process.
虽然您通常可以自由设计自己的构建过程,但现在大多数人都会使用标准化方法,例如as 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 / java
和 src / main / resources
)需要由Classloader加载才能启动应用程序。
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.
这篇关于什么是用于Java EE Web应用程序的WEB-INF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!