简体   繁体   English

Java Web项目结构最佳实践

[英]Java Web Project Structure Best Practice

I am starting a new Java Web Project which is using Hibernate and a standard MVC Architecture. 我正在开始一个使用Hibernate和标准MVC架构的新Java Web Project。 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. 但是我还没有找到任何指导方针。

So what I am curious to know is 所以我很想知道的是

  • Is anyone aware of any best practise guidelines for the layout of a Java Web Project? 是否有人了解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? 人们是否倾向于通过演示,业务和应用程序等不同层次来拆分包?

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 etc). 因此,请阅读框架附带的文档(Spring MVC,Struts,JSF等)。

Another good proposal is to use Maven Archetypes to generate a skeleton for your specific framework. 另一个好建议是使用Maven Archetypes为您的特定框架生成骨架。 Some web frameworks (such as seam) have even their own code generation tool that lays the foundations for your web project. 一些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.customers
  • com.mycompany.myproject.departments com.mycompany.myproject.departments
  • com.mycompany.myproject.billing com.mycompany.myproject.billing
  • com.mycompany.myproject.reports com.mycompany.myproject.reports
  • com.mycompany.myproject.admin com.mycompany.myproject.admin

and NOT 并不是

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

The second structure is too generic, tends to resolve around huge packages with unrelated stuff and is hard to maintain. 第二种结构过于通用,倾向于解决具有不相关内容的大型包,并且难以维护。

To continue my previous answer, I have many web projects. 为了继续我以前的答案,我有很多网络项目。 In all of them the structure under src is more or less the same. 在所有这些中,src下的结构或多或少相同。 The packages are roughly separated to 3 logical layers. 这些包大致分为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. 其次,有一个用于hibernate模型/ db访问层的层。 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. 如果你使用hibernate进行数据库访问,那么模型是由hibernate类定义的,所以我把它们放在与dao对象相同的区域。 Eg com.sample.model holds the hibernate data objects and com.sample.model.dao hold the dao objects. 例如,com.sample.model保存hibernate数据对象,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),那么有时候我发现将数据对象放在业务逻辑层而不是数据库访问层更方便。

(The rest of the stuff typically falls under the business layer). (其余的东西通常属于业务层)。

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. 首先,遵循流行的ide,ala Eclipse,Netbeans等的传统结构。例如,在Eclipse中,所有内容都已经安排了WEB-INF和META-INF文件夹,因此打包和部署很容易。 Classes source code (typically under src) is automatically copied to WEB-INF/classes. 类源代码(通常在src下)自动复制到WEB-INF / classes。 There are a few other considerations: 还有一些其他考虑因素:

  1. If you are using MVC, then it is possible that you don't need to access your JSPs directly. 如果您正在使用MVC,那么您可能不需要直接访问JSP。 If so, keep the JSP source code under WEB-INF/jsp, for security reasons. 如果是这样,出于安全原因,请将JSP源代码保留在WEB-INF / jsp下。
  2. Similarly, keep custom tag files under WEB-INF/tags. 同样,在WEB-INF /标签下保留自定义标签文件。
  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. 将javascript文件保存在js文件夹中,将css文件保存在样式文件夹中等。所有文件夹应与WEB-INF处于同一级别,以模仿真实部署。
  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. 显然你的Hibernate daos不需要和你的servlet在同一个包中。
  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. 如果你在同一个软件包中得到太多的servlet,可以考虑根据功能对它们进行子包装,但是它们有一个共同的祖先包很好 - 它有助于提高可读性。

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 java folder in the example here, maybe it was obvious, but it was left out in the above link for some reason. 我在这里的示例中包含了java文件夹,也许很明显,但由于某种原因它被遗漏在上面的链接中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM