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

MVC 1.0 in Java EE 8 – Getting Started with NetBeans 8.1 and Payara 4.1

DZone's Guide to

MVC 1.0 in Java EE 8 – Getting Started with NetBeans 8.1 and Payara 4.1

In JEE 8 there will be an action-based web framework called MVC. This article gives a brief overview of MVC 1.0 and enables interested people to get started with MVC quickly.

· 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.

In JEE 8 there will be an action-based web framework. Its name is Model-View-Controller or MVC for short. Model-View-Controller leverages existing JEE technologies, uses JAX-RS and integrates CDI and Bean Validation.
This article gives a brief overview of MVC 1.0 and enables interested people to get started with MVC quickly.

Getting Started

The current version of the MVC reference implementation named Ozark is an early draft 2 so you need to download the latest version of Payara or Glassfish to run MVC on it. If you are a NetBeans user as well, you have to download the Release Candidate of NetBeans 8.1 to get support for the current versions of Payara 4.1.153 and Glassfish 4.1.1.

After downloading the prefered application server, two maven dependencies have to be added to the pom.xml in case of an web profile based Java EE application.

That's all! Now yor project setup is ready to start with the Model-View-Controller API.
Note:
You can also use the Full Profile with all features, but there is no need to use it if you want to build a web application with MVC 1.0.

Controller

An MVC Controller is a JAX-RS resource class or method(s) decorated by a @Controller annotation. It is the glue between a Model and a View because it linkes both together like in the following snippet.

In this getting started post the @Controller annotation will be used on class level only, but there will be additional posts about Controllers in detail.

This hello() method will be called when firing a HTTP GET at the hello path of this Controller. The method has no parameters and uses a RequestScoped CDI Models Bean javax.mvc.Models to put content into the returned hello.jsp. In this case the Map contains the value "Hello MVC 1.0" which can be accessed within the the returned hello.jsp.

Note: It's also possibile to use your own CDI Beans as Models instead of the MVC javax.mvc.Models class.

Model

Like shown before, MVC Models are used to combine data-models and views (view templates). In the previous Controller method the Models map is injected via CDI @Inject and in the hello() method the Map is used to put dynamic content into the hello.jsp page like described before. The map contains an entry with the key "hello" and the value "Hello MVC 1.0". The models key and value will be used in the view section.

View

By returning the hello.jsp page in the hello() method in the previous Controller the view will be shown and it displays the value of the Models map by accessing the key "hello" and displays the value.







The output in the browser looks like:

Conclusion

With MVC there will be a very simple new web framework in JEE 8 and an action based alternative to the component based JSF. It is very simple and powerful and can easily be used by JEE developers.

In case of questions feel free to ping me at Twitter. Have fun with NetBeans, Payara and MVC :-)

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

Topics:
javaee mvc

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}