MVC Web Application With ActiveJ
Creating an asynchronous Java web application using ActiveJ framework.
Join the DZone community and get the full member experience.Join For Free
In this tutorial we will create an asynchronous servlet that adds contacts to a list, parses requests and processes form validation with the help of ActiveJ framework. You can find the source code of this tutorial on GitHub.
We'll need the following dependencies:
We'll use the following ActiveJ technologies:
- ActiveInject - lightweight and powerful dependency injection library with great performance and no third-party dependencies.
- ActiveJ HTTP - high-performance asynchronous HTTP clients and servers.
- ActiveJ Launcher - takes care of full application lifecycle, service management, and logging. Perfectly compatible with ActiveInject.
This tutorial represents the MVC pattern:
- To model a
Contactrepresentation, we will create a plain Java class with fields (
address), constructor, and accessors to the fields:
Address class is pretty simple:
To simplify the tutorial, we will use an ArrayList to store the
ContactDAO interface and its implementation are used for this purpose:
- To build a view we will use a single HTML file, compiled with the help of the Mustache template engine:
AsyncServletwill be used as a controller. We will also add
RoutingServletfor routing requests to the needed endpoints. We'll get to this in a moment.
HttpDecoderExample class which extends
HttpServerLauncher. By extending
HttpServerLauncher we will take care of the server’s lifecycle and service management. Next, we provide two custom parsers based on
CONTACT_DECODER - which will be used for validation.
Decoder class provides you with tools for parsing requests.
Also, we need to create
applyTemplate(Mustache mustache, Map<String, Object> scopes) method to fill the provided Mustache template with the given data:
Next, let’s provide a
ContactDAOImpl factory method:
Now we have everything needed to create the controller
AsyncServlet to handle requests:
RoutingServlet two route paths are defined. The first one matches requests to the root route
/ - it simply displays a contact list. The second one,
/add - is an HTTP POST method that adds or declines adding new users. We will process this request parsing with the help of the aforementioned
Decoder by using
Either represents a value of two possible data types which are either
DecodeErrors). In order to determine whether a parse was successful or not, we check it’s value by using the
Finally, write down the
main method which will launch our application:
You've just created and launched an MVC web application with asynchronous and high-performance server! Now you can test the app by visiting localhost:8080.
Published at DZone with permission of Valeria Listratova. See the original article here.
Opinions expressed by DZone contributors are their own.