-1

Introduction

I am working on a Java EE Web Application that is a basic CRUD app. It is built with Servlet and JSP technologies, without any Frameworks. I am doing it to make my hands dirty in plain Java EE things.


Project structure

Here are directories/packages of the project:

  • entity - entity classes which are simple POJOs for my domain.

  • dao - Data Access Object stuff.

  • service - represents Service layer that performs some additional computations/actions on top of the DAO objects.

  • util - utility classes to hash password, validate entered data, etc.

  • controller - Front Controller Design Pattern implementation; FrontController class that provides a centralized entry point of all requests.

    • action - it has implementation of Strategy Design Pattern; it has Action interface, ActionFactory with a Map<String, Action> actions which is mapping URL to the concrete action, and it has Action implementations such as SignUpActionImpl, DashboardActionImpl, etc.
  • filter - classes that implement Filter interface, used to make Filters for page encoding, restrict user for accessing specific pages, etc.


Question

How should project's directory layout look like? Is it OK to make it what is called package by feature?

  • 1
    There are some good questions here. But much like your proposed architecture this question has them all smushed together. Try teasing them apart into focused questions. I'll tell you this for free: if you need a util package you are failing to organize well. Give me a clue what should and shouldn't go in here. – candied_orange Dec 31 '16 at 22:16
  • 1
0

Your application packages should be categorized on what they actually hold, a category should relate to a feature your application will provide to either yourself/other developers who will work on the code and to the client.

For example, lets say you want to add a Scheduler framework to run certain tasks every once and a while, that framework provides you with various features such as event listeners, builtin filters etc.. so instead of creating a package for all these features and calling it scheduler and making a mess, you can create a package for filters and place your filters inside, a package for listeners and place listeners inside, these packages will also hold any filters and listeners you already have from other frameworks and Java.

com.myapplication.filters
    ResponseFilter.java
    RequestFilter.java
    ....
com.myapplication.listeners
    SessionListener.java
    SchedulerJobListener.java
    ....

Of course you will also have a data access layer to get persistent data and end-points for you WS resources, so their packages would look like

com.myapplication.resources
com.myapplication.models

Other than that, I really like how MAVEN creates a project layout, the Netbeans IDE already comes configured for creating a MAVEN web app (among other options).

1

despite having a layered architecture, you should structure your code feature-wise. So you will have packages that encapsulate features and they are then a bit layered. otherwise, you will end up with features spread over your whole code-base and your package-names dont tell you much. e.g. "Controller" will then contain all controllers.

I prefer having a package "...login" which contains e.g. a loginService and the view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.