我正在使用以下源代码目录结构的Java EE web应用程序:

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

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.


当前回答

当您部署Java EE web应用程序(使用框架或不使用框架)时,其结构必须遵循一些要求/规范。这些规范来自:

servlet容器(例如Tomcat) Java servlet API 您的应用程序域

The Servlet container requirements If you use Apache Tomcat, the root directory of your application must be placed in the webapp folder. That may be different if you use another servlet container or application server. Java Servlet API requirements Java Servlet API states that your root application directory must have the following structure : ApplicationName | |--META-INF |--WEB-INF |_web.xml <-- Here is the configuration file of your web app(where you define servlets, filters, listeners...) |_classes <--Here goes all the classes of your webapp, following the package structure you defined. Only |_lib <--Here goes all the libraries (jars) your application need

These requirements are defined by Java Servlet API. 3. Your application domain Now that you've followed the requirements of the Servlet container(or application server) and the Java Servlet API requirements, you can organize the other parts of your webapp based upon what you need. - You can put your resources (JSP files, plain text files, script files) in your application root directory. But then, people can access them directly from their browser, instead of their requests being processed by some logic provided by your application. So, to prevent your resources being directly accessed like that, you can put them in the WEB-INF directory, whose contents is only accessible by the server.-If you use some frameworks, they often use configuration files. Most of these frameworks (struts, spring, hibernate) require you to put their configuration files in the classpath (the "classes" directory).

其他回答

在WEB-INF目录下放置jsp页面有一个约定(不是必需的),这样它们就不能被深度链接或书签到。 这样,对jsp页面的所有请求都必须通过我们的应用程序进行定向,从而保证用户体验。

Servlet 2.4规范是这样描述WEB-INF的(第70页):

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 -应用程序的资源加载器访问,而不是直接对公众可见。

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

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

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

Wikipedia WAR files文章中提到的WEB-INF/classes和WEB-INF/lib文件夹就是Servlet规范在运行时需要的文件夹的例子。

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

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

从项目结构到结果WAR文件的转换是由构建过程完成的。

虽然您通常可以自由地设计自己的构建过程,但现在大多数人将使用标准化的方法,如Apache Maven。除此之外,Maven还定义了项目结构中的资源映射到结果工件中的资源的默认值(在本例中,结果工件是WAR文件)。在某些情况下,映射由普通复制过程组成;在其他情况下,映射过程包括转换,例如过滤或编译等。

举个例子:WEB-INF/classes文件夹稍后将包含所有编译过的java类和资源(src/main/java和src/main/resources),这些类和资源需要由Classloader加载来启动应用程序。

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

您应该在WEB-INF中放入您不想公开的任何页面或页面片段。通常,JSP或facet在WEB-INF之外,但在这种情况下,任何用户都可以轻松访问它们。如果您有一些授权限制,可以使用WEB-INF。

WEB-INF/lib可以包含您不想在系统级打包的第三方库(jar可以用于服务器上运行的所有应用程序),但仅适用于这个特定的应用程序。

一般来说,很多配置文件也会进入WEB-INF。

至于WEB-INF/classes——它存在于任何web-app中,因为这是存放所有编译源代码的文件夹(不是jar,而是你自己编写的编译过的。java文件)。

遵循此约定是出于安全考虑。例如,如果允许未经授权的人直接从URL访问根JSP文件,那么他们可以在不需要任何身份验证的情况下浏览整个应用程序,并且可以访问所有受保护的数据。

当您部署Java EE web应用程序(使用框架或不使用框架)时,其结构必须遵循一些要求/规范。这些规范来自:

servlet容器(例如Tomcat) Java servlet API 您的应用程序域

The Servlet container requirements If you use Apache Tomcat, the root directory of your application must be placed in the webapp folder. That may be different if you use another servlet container or application server. Java Servlet API requirements Java Servlet API states that your root application directory must have the following structure : ApplicationName | |--META-INF |--WEB-INF |_web.xml <-- Here is the configuration file of your web app(where you define servlets, filters, listeners...) |_classes <--Here goes all the classes of your webapp, following the package structure you defined. Only |_lib <--Here goes all the libraries (jars) your application need

These requirements are defined by Java Servlet API. 3. Your application domain Now that you've followed the requirements of the Servlet container(or application server) and the Java Servlet API requirements, you can organize the other parts of your webapp based upon what you need. - You can put your resources (JSP files, plain text files, script files) in your application root directory. But then, people can access them directly from their browser, instead of their requests being processed by some logic provided by your application. So, to prevent your resources being directly accessed like that, you can put them in the WEB-INF directory, whose contents is only accessible by the server.-If you use some frameworks, they often use configuration files. Most of these frameworks (struts, spring, hibernate) require you to put their configuration files in the classpath (the "classes" directory).