Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Web Project Structure Best Practice

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.

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?
like image 834
Mark Davidson Avatar asked Jan 31 '09 14:01

Mark Davidson


People also ask

What is the structure of a Java web application?

Web applications have a directory structure, which is fully accessible from a mapping to the application's document root (for example, /hello). The document root contains JSP files, HTML files, and static files such as image files. A WAR file (web archive file) contains a complete web application in compressed form.

How do you organize a spring boot project?

There is no specific layout or code structure for Spring Boot Projects. However, there are some best practices followed by developers that will help us too. You can divide your project into layers like service layer, entity layer, repository layer,, etc. You can also divide the project into modules.

What is the structure of a Maven project?

Maven project structure defines a folder in order to store all resources and files needed by a web application. Inside this folder you can put all the required files for a web application like jsp files, js files, html files, css files, template files, reports files, WEB-INF files (like web. xml), META-INF files, etc…


2 Answers

It really depends on your web framework.

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.

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

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.

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

That means packages for

  • 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

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

like image 156
kazanaki Avatar answered Oct 20 '22 01:10

kazanaki


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.

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

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.

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.

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

like image 21
Yoni Avatar answered Oct 20 '22 00:10

Yoni