MVC 1.0 in Java EE 8 - How to work with Models

4 min read
This is the second part of my MVC 1.0 blog series. While the last blogpost was about an introduction to MVC 1.0 in general, this one focuses on Models in MVC. 
Like described before Models are responsible for combining data-models and views (templates) to produce web application pages.
In MVC 1.0 there are two kinds of Models. The developer can either use the javax.mvc.Models map to combine data-models with views or he can develop his own CDI based Models.


Working with Single Objects

Single Objects like Strings can easily be putted into the Models map. The following example shows how to work with Models and corresponding Views. It also shows how multiple different Models can be used to display informations within a View.

To access this value of this map the respective key has to be "hello".
The key of the map can be accessed via ${key} expression whereas key is the key of the corresponding map. In this scenario the key to get "Hello MVC 1.0" is "hello".

Note: There is no IDE support for this javax.mvc.Models map which means the developer has to take care to write the key and values of this map correctly. Otherwise this will result in an runtime error when triggering the url.

Working with multiple Objects

With this kind of Model you have to access the properties of this map directly via ${key} expression. This is useful because this map is able to store arbitary kinds of Objects. Beside as putting simple Strings into the map it is also possible to put multiple, different Objects into it. For example Persons and Customers like in the following snippet.

The way to access the keys is a little bit different in comparison to simple keys like Strings.
The properties of an Object e.g. Person have to be accessed via key.propertyname instead of just key like in the String example before.

CDI-based Models

A more save way to work with MVC Models are the  CDI-based Models. The JSR 371 Spec recommends to "use CDI-based models wherever supported and thus take advantage of existing CDI and Expression Language integration on the platform".

Working with Single Objects

By using CDI-based Models the same Controller looks like:

The Models class has been replaced by the Person entity and the first- and lastname are set with the setter method of the Person.
The way to access properties within a view is a little bit different in comparison with single Objects when using CDI-based models.
In comparison to javax.mvc.Models by using CDI-based Models it is possible to annotate the Model with other Scopes than RequestScoped. For example RequestScoped.

Note: The CDI Bean needs to be public, a default Constructor and public getter and setter methods. In addition to that it also needs to be annotated with @Named and a Scope. In most of the use cases this will be RequestScoped.

Working with multiple Objects

It is also possibile to inject multiple (different) models within a single Controller. The way to work with multiple models is similar to single objects. The properties can be set via setter method or even via Constructor and can be accessed within a View as well.


In MVC there are two ways to handle models. While the javax.mvc.Models class is a map with a String as key and an Object as value, the CDI-based Models are simple CDI beans.
The javax.mvc.Models map can contain everything you want. This can be useful in use cases where no additional Model class is necessary.
CDI-based Models instead of Models can be used for redirects, forwardings and other use cases where a Scope different than RequestScope is required.


Be Social, Share!