Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MVC 1.0 in Java EE 8: How to Work With Models

DZone's Guide to

MVC 1.0 in Java EE 8: How to Work With Models

Interesting in working with models? Check out this MVC 1.0 breakdown.

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

This is the second part of my MVC 1.0 blog series. While the last article 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.

javax.mvc.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 bjects 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.

Conclusion

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.

Bye,

Bennet

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
javaee mvc ,javaee8 ,glass fish ,payara ,javaee ,java

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}