Category Archives: web development

Working with nested Maps in JAVA

In this post I will show you a simple and easy way of working with JAVA Maps, more precisely, how to add an element (a.k.a value) to a multi nested maps:

That’s it. Simple, isn’t it? 😉

Source code: github

Date Range Validation with Annotations in JAVA

In this post I will show you how to validate date ranges with JAVA annotation.

simple date range validation
simple date range validation

XHTML page:

This is a simple JSF page with two date inputs and a submit button. When we press the button a the validation process will start via the backing bean that call a super class method.

Backing Bean:

Note that in line 3 we have the super class named: AbstractController. In line 16, in the action method, we call a validate method and we pass as a parameter the dto wich has the data itself.

Abstract Controller

In this abstract class we simple call the validate() method in the received DTO.

Data Transfer Object(DTO):

Here is the point where we annotate at the class-level with our special annotation. We simple use the annotation with 2 parameters. This parameters are optional and it’s purpose is only to personalize the error message. In case that your date fields are different from those in this example you must provide the annotation with more parameters, as you will see in the next class.


Here is where the magic happens. I will not discuss how annotations work, but I will cover the essential part: the constraint validation. The inner class DateIntervalValidator is responsible for the validation of the date fields with the help of reflection. We get the get methods of the two date fields and then we verify if endDate is after startDate. If that condition is false, we return false, otherwise we return true.
The message is required if you want personalize your message.
The start is required if your first date field is different from startDate. It indicates the name of the first field.
The end is required if your end date field is different from endDate. It indicates the name of the second date field.
The purpose the startDesc and endDesc is to use as a placeholder in the message parameter.


This class is the trigger of all the validation process. When in the DTO we call the validate method, is this method we are calling, because this class is the superclass of the DTO. It calls the annotation and validates the fields. If there are any erros it will collect them in a set, here called as result. Then we iterate all over the set and add the error messages to faces context.

The next few files demonstrate the same logic applied to 2 or more groups of date validations.

multiple date range validation
multiple date range validation


Backing Bean:


Plase feel free to comment or to suggest new features.
You can read more about this topic here
Download source code.

JAVA EE – CDI Interceptor Example

In this post I will explain you one simple way of using interceptors in Java EE.

We have a simple java project that only displays a name wich is setted in the Controller class by the init method. But as you will see next, the final result it’s not the name that is setted by the controler, because we have an interceptor that changes the name behind the scenes.

The index page index.xhtm makes a server request to display the name. But that request is going to be intercepted by our interceptor Then who makes the request to the controller is the interceptor and if the response is of the type String it changes it to another string. When the page renders it will show not the response from the controller but the object that our interceptor returns to it.

And that’s it. Even without the final user knows nothing about it, the response has been manipulated by our interceptor.

  • Project Scheme


  • index.xhtml



Note that in line 10 we have a special annotation. This marks this bean to be intercepted, as you will see next.  The code in the 20th line, just sets the variable name to John Doe.


This is a simple annotation class (out of the scope of this post, for now). The important thing here is the @InterceptorBinding annotation.


In the MyInterceptor class we have 3 important annotations:

@Interceptor  (line 10),
@LogToConsoleAndModifyResult  (line 11) and the
@AroundInvoke  (line 14).

The first one makes this bean an interceptor. The second, marks this interceptor to intercept all methods in classes  annotated with the the same annotation. The last one, marks the method that is going to manipulate all the data.

Note that this method must returns an Object and as a parameter it takes the invocation context.

First we have a logger that prints out to the console a message before the interception. Then with the help of the invocation context, it lets the original method to proceed and keeps the result. Then it prints again to the console the real name returned by the controller (John Doe). After this, it changes the return value to Jane Doe, that is exactly what is going to be displayed in the page as the final result.

  • beans.xml

Finally, in order to use interceptors we must register them in the beans.xml file (line 3). Another possibility is to annotate the controller with @Interceptors({MyInterceptor.class}) but this approach is not recommended.

  •  The Log

the log messages

Here are the three messages sent to the console.

  • The result page

web page

As you can see here the displayed name isn’t John Doe, as you might expect, but the modified result: Jane Doe.

Source code

Feel free to comment.

Logger CDI Injection

In this post I will show you how to inject an instance of Logger class using CDI (Context Dependency Injection) and SLF4J (Simple Logging Facade for Java).

To keep it simple, we only have a xhtml page (index), a managed bean ( and the producer ( The project configuration is as follows:



As we are using JSF with facelets, the index page looks like:

In this file we simply display the name that is setted in the controller (line 10).


In the controller we inject (@Inject) an instance of the Logger class and print to the console the name before and after the name is setted (lines 23 and 25). CDI knows which Logger to inject because we have a class that works as a factory of Loggers (LoggerProducer) with some special annotations.


In the LoggerProducer class we have an annotation (@Produces) in a method that returns a Logger instance. This is enough to CDI inject a Logger when needed. Notice that in line 13 we pass as a parameter the injection point (ip) in order to get the class where the logger is being injected (in this specific case ‘Controller.class’). This is only necessary beacuse the getLogger method needs it.

Finally, here are the results (the log and the web page):



As you can see here, we have two lines displaying the information that we expected: first the name is null and after the set of the variable name, name = John Doe.

the result page
the result page


That’s it. It’s just that simple!


First we create a factory of loggers, then we inject them whenever we need them:
@Produces to indicate CDI what we are producing and
@Inject to inject the instance ready to use.

Notice that this methodology works with other types rather than Logger. You may inject whatever you want by simply following the above technic.

Feel free to comment.

Source code