Java Web 项目结构最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/498917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:39:45  来源:igfitidea点击:

Java Web Project Structure Best Practice

javamodel-view-controllerprojectstructurepackages

提问by Mark Davidson

I am starting a new Java Web Project which is using Hibernate and a standard MVC Architecture. I have just started to layout the projects structure and while doing this I started to look around to see if there was any standards in this area, about where Controllers should go and generally the best way to lay everything out. However I have not really found any guidelines.

我正在启动一个使用 Hibernate 和标准 MVC 架构的新 Java Web 项目。我刚刚开始布局项目结构,在此过程中,我开始环顾四周,看看在这个领域是否有任何标准,关于控制器应该去哪里以及通常布局所有东西的最佳方式。但是,我还没有真正找到任何指导方针。

So what I am curious to know is

所以我很想知道的是

  • Is anyone aware of any best practise guidelines for the layout of a Java Web Project?
  • Does anyone have a particular set of hard rules that they always follow for different types of project?
  • Do people tend to split packages by the different layers such as presentation, business, and application?
  • 有没有人知道有关 Java Web 项目布局的任何最佳实践指南?
  • 有没有人有一套特定的硬性规则,他们总是针对不同类型的项目遵循这些规则?
  • 人们是否倾向于按不同的层(例如表示层、业务层和应用程序层)拆分包?

采纳答案by Yoni

To continue my previous answer, I have many web projects. In all of them the structure under src is more or less the same. The packages are roughly separated to 3 logical layers.

继续我之前的回答,我有很多网络项目。在所有这些中, src 下的结构或多或少是相同的。这些包大致分为 3 个逻辑层。

First is the presentation layer, as you said, for servlets, app listeners, and helpers.

首先是表示层,如您所说,用于 servlet、应用程序侦听器和帮助程序。

Second, there is a layer for the hibernate model/db access layer. The third layer for business logic. However, sometimes the boundary between these layers is not clear. If you are using hibernate for db access then the model is defined by hibernate classes so I put them in the same area as the dao objects. E.g. com.sample.model holds the hibernate data objects and com.sample.model.dao hold the dao objects.

其次,有一层用于休眠模型/数据库访问层。第三层为业务逻辑。然而,有时这些层之间的界限并不明确。如果您使用 hibernate 进行 db 访问,那么模型由 hibernate 类定义,因此我将它们与 dao 对象放在同一区域。例如 com.sample.model 保存休眠数据对象,com.sample.model.dao 保存 dao 对象。

If using straight jdbc (usually with Spring), then sometimes I find it more convenient to put the data objects closer to the business logic layer rather than with the db access layer.

如果直接使用 jdbc(通常使用 Spring),那么有时我会发现将数据对象放在更靠近业务逻辑层而不是 db 访问层更方便。

(The rest of the stuff typically falls under the business layer).

(其余的东西通常属于业务层)。

回答by kazanaki

It really depends on your web framework.

这实际上取决于您的 Web 框架。

For example if you use Wicket, java files and webpages co-exist in the same directory while in most other frameworks, pages (.jsp files or whatever is your presentation engine) and code-behind stuff (java files ) are completely separate.

例如,如果您使用 Wicket,java 文件和网页共存于同一目录中,而在大多数其他框架中,页面(.jsp 文件或任何您的演示引擎)和代码隐藏内容(java 文件)是完全分开的。

So read the documentation that comes with your framework (Spring MVC, Struts, JSF e.t.c).

因此,请阅读您的框架(Spring MVC、Struts、JSF 等)附带的文档。

Another good proposal is to use Maven Archetypes to generate a skeleton for your specific framework. Some web frameworks (such as seam) have even their own code generation tool that lays the foundations for your web project.

另一个好的建议是使用 Maven Archetypes 为您的特定框架生成骨架。一些 web 框架(比如 seam)甚至有自己的代码生成工具,为你的 web 项目奠定了基础。

My only good suggestion (that is not mentioned by Yoni) for the src directory is to make packages according to business purpose and NOT according to type/layer

我对 src 目录唯一的好建议(Yoni 没有提到)是根据业务目的制作包,而不是根据类型/层

That means packages for

这意味着包

  • com.mycompany.myproject.customers
  • com.mycompany.myproject.departments
  • com.mycompany.myproject.billing
  • com.mycompany.myproject.reports
  • com.mycompany.myproject.admin
  • com.mycompany.myproject.customers
  • com.mycompany.myproject.departments
  • com.mycompany.myproject.billing
  • com.mycompany.myproject.reports
  • com.mycompany.myproject.admin

and NOT

并不是

  • com.mycompany.myproject.entities
  • com.mycompany.myproject.tables
  • com.mycompany.myproject.graphs
  • com.mycompany.myproject.dialogs
  • com.mycompany.myproject.servlets
  • com.mycompany.myproject.entities
  • com.mycompany.myproject.tables
  • com.mycompany.myproject.graphs
  • com.mycompany.myproject.dialogs
  • com.mycompany.myproject.servlets

The second structure is too generic, tends to resolve around huge packages with unrelated stuff and is hard to maintain.

第二种结构过于通用,倾向于解决包含不相关内容的大型包并且难以维护。

回答by Yoni

First, the to follow the conventional structure of a popular ide, ala Eclipse, Netbeans, etc. In Eclipse, for example, everything is arranged already with a WEB-INF and META-INF folders, so packaging and deployment is easy. Classes source code (typically under src) is automatically copied to WEB-INF/classes. There are a few other considerations:

首先,遵循流行的ide,ala Eclipse,Netbeans等的常规结构。例如在Eclipse中,一切都已经安排好了WEB-INF和META-INF文件夹,所以打包和部署很容易。类源代码(通常在 src 下)会自动复制到 WEB-INF/classes。还有一些其他的考虑:

  1. If you are using MVC, then it is possible that you don't need to access your JSPs directly. If so, keep the JSP source code under WEB-INF/jsp, for security reasons.
  2. Similarly, keep custom tag files under WEB-INF/tags.
  3. Keep javascript files in js folder, css files in style folder, etc. All folders should be at the same level as WEB-INF to mimic a real deployment.
  4. It is good to separate your code into packages according to layers. Obviously your Hibernate daos don't need to be at the same package as your servlets.
  5. If you end up with too many servlets in the same package, consider sub-packaging them accord to functionality, but it is nice that they have a common ancestor package - it helps with readability.
  1. 如果您使用的是 MVC,那么您可能不需要直接访问您的 JSP。如果是这样,出于安全原因,请将 JSP 源代码保留在 WEB-INF/jsp 下。
  2. 同样,将自定义标签文件保存在 WEB-INF/tags 下。
  3. 将 javascript 文件保存在 js 文件夹中,css 文件保存在 style 文件夹中等。所有文件夹都应与 WEB-INF 处于同一级别以模拟真实部署。
  4. 根据层将您的代码分成包是很好的。显然,您的 Hibernate daos 不需要与您的 servlet 位于同一个包中。
  5. 如果您最终在同一个包中有太多 servlet,请考虑根据功能对它们进行子打包,但它们有一个共同的祖先包是很好的 - 这有助于提高可读性。

回答by Leif Gruenwoldt

Use the Maven webapp archetype layout.

使用Maven webapp 原型布局。

project
|-- pom.xml
`-- src
    `-- main
        |-- java
        `-- webapp
            |-- WEB-INF
            |   `-- web.xml
            `-- index.jsp

I've included the javafolder in the example here, maybe it was obvious, but it was left out in the above link for some reason.

我已将java文件夹包含在此处的示例中,也许很明显,但由于某种原因,它在上面的链接中被遗漏了。