DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Java EE 6 Pet Catalog with GlassFish and MySQL
  • Beans Custom Validation Constraints
  • How to Embed a jBPM Process in a Java EE Application
  • Tutorial: Deploying Java EE Apps on Azure (Part 3)

Trending

  • Breaking Down Silos: The Importance of Collaboration in Solution Architecture
  • Decoding Business Source Licensing: A New Software Licensing Model
  • Best Practices for Developing Cloud Applications
  • TypeScript: Useful Features
  1. DZone
  2. Coding
  3. Java
  4. Java EE 8 MVC: Getting Started With Ozark

Java EE 8 MVC: Getting Started With Ozark

Java EE's first action-based MVC framework finally arrives with Java EE 8. Get familiar with the early release of the framework now before the final release in Q3 2016.

Michael Scharhag user avatar by
Michael Scharhag
·
Sep. 03, 15 · Tutorial
Like (2)
Save
Tweet
Share
17.45K Views

Join the DZone community and get the full member experience.

Join For Free

About a year ago a new action-based MVC framework, simply called MVC, was announced for Java EE 8. MVC (specified in JSR 371) is based on JAX-RS and integrates with Java EE technologies like CDI and Bean Validation. The reference implementation for MVC 1.0 is Ozark.

This is the first article of a multi-part tutorial I am planning to write about Java EE MVC. In this post we will see how to get a basic Java EE MVC application running with Ozark. Upcoming articles will provide more details to specific sections.

Getting Started With Ozark

Please note that the MVC specification is still an early draft, the final specification is planned to be released in Q3 2016. To have a look at Java EE MVC in this early state, we need a recent nightly build version of GlassFish and the current Ozark milestone release. The Ozark team recommends GlassFish b13 03-16-2015 for the current Ozark version.

Besides GlassFish we need the following dependencies to create an MVC application:

<dependencies>
  <dependency>
    <groupId>com.oracle.ozark</groupId>
    <artifactId>ozark</artifactId>
    <version>1.0.0-m01</version>
    <scope>compile</scope>
  </dependency>
  <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
  </dependency>
</dependencies>

As mentioned above, Java EE MVC is based on JAX-RS. So things might look very familiar to you, if you already know about JAX-RS.

To create our MVC application we first need a JAX-RS Application class:

@ApplicationPath("getting-started")
public class GettingStartedApplication extends Application {

}

This subclass of javax.ws.rs.core.Application can be used to define additional JAX-RS components. In this example we do not need any special configuration, so the class can stay empty. With @ApplicationPath we define the base path for our application.

Creating the Controller

A controller is responsible for processing incoming requests. Based on the incoming request it executes business logic, updates the model and returns the view that should be rendered. A simple Java EE MVC Controller looks like this:

@Controller
@Path("hello")
public class HelloController {

  @Inject
  Models models;

  @GET
  public String sayHello(@QueryParam("name") String name) {
    String message = "Hello " + name;
    models.put("message", message);
    return "/WEB-INF/jsp/hello.jsp";
  }
}

The Controller class is annotated with @Controller and @Path. This indicates that the class is a Java EE MVC Controller that listens for requests on /getting-started/hello.

With CDI an instance of Models is injected to the controller. The Models class represents the MVC model. It is filled with data by the controller and is then passed to the view. Models is basically a Map<String, Object> that can contain arbitrary data.

The sayHello() method processes incoming HTTP GET requests (indicated by @GET). With @QueryParam request parameters can be bound to method parameters. Inside sayHello() the request parameter name is used to create a text message, which is then added to the Model. The returned String defines the path to the view that should be rendered.

Creating the View

Views in Java EE MVC applications are typically HTML pages with CSS and JavaScript files. In this example our view is a simple JSP file located at /WEB-INF/jsp/hello.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Getting started</title>
  </head>
  <body>
    <h1>${message}</h1>
  </body>
</html>

Inside JSP files, model properties can be accessed via EL. Here, we use ${message} to access the model value with the key message.

The Java EE MVC specification defines two standard template engines for views: JSP and Facelets. However, other template engines can easily be integrated. We will have a look at the integration of other view technologies in an upcoming post.

Running the Application

Now we are ready to start GlassFish and deploy our new MVC application. After that, we can send a GET request to our controller and see what it returns. Do not forget that the controller expects a name parameter.

For example

GET /getting-started/hello?name=john

will result in a HTML page containing the message Hello John

Summary

Java EE MVC is the new upcoming Java MVC web framework. It uses a lot of existing Java technologies like JAX-RS, CDI and JSP. The framework itself is quite simple and easy to understand. The complete MVC 1.0 specification is only around 33 pages long and very easy to read.

We can use the current milestone release of the MVC 1.0 reference implementation Ozark to get a feeling for the upcoming Java EE 8 framework.

You can find the full source code of the example application on GitHub.

In future blog posts we will have a look at parameter validation, exception handling and other view technologies.

Java EE Java (programming language) application

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java EE 6 Pet Catalog with GlassFish and MySQL
  • Beans Custom Validation Constraints
  • How to Embed a jBPM Process in a Java EE Application
  • Tutorial: Deploying Java EE Apps on Azure (Part 3)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: