[英]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 所以我很想知道的是
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 这意味着包
and NOT 并不是
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:
还有一些其他考虑因素:
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.