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.
Comments
John - Thursday, 3 September, 2015
This looks very much like Spring.
Jonathan S. Fisher - Thursday, 3 September, 2015
Is it possible to access @Named CDI Beans in the EL?
Arturo - Friday, 4 September, 2015
Thanks! It looks easy to use.
Michael Scharhag - Friday, 4 September, 2015
@Jonathan S. Fisher:
Yes this should be possible. At least with GlassFish this works without problems for me:
@Named("foo")
public class MyClass {..}
JSP:
<p>${foo.myMethod()}</p>
Not Impressed - Tuesday, 8 September, 2015
Seriously, how does something this half assed get added to the official release? I mean, the intention is noble, but this MVC framework is just embarrassing as far as features go. Is there any notion of validation, view bag data, authentication, security, partials, project structure, front end integration? Is there anything in this at all besides some attributes to connect a model to a view? Is this really in the official spec? Java is getting it's lunch eaten by C#. No offence, but unless Java picks it the fuck up, it's going to be a dinosaur in no time. This 'framework' certainly isn't helping.
Steve M - Wednesday, 9 September, 2015
Ozark also works on latest Payara as well as GlassFish nightlies. https://github.com/payara/Payara
Rogério - Wednesday, 16 September, 2015
I am left wondering what's the point of this MVC framework? See, we already have a nice UI solution in Java EE: JSF 2 + facelets, with an implementation like PrimeFaces. I implemented a full Java EE 7 web app a few months ago, and the codebase was much better than this example. PrimeFaces uses AJAX communication by default, in a single-page model. This new MVC framework instead proposes cumbersome JAX-RS calls, and (apparently) the old-fashioned multi-page model. Oh, and I managed to get full code coverage with integration tests. So, what's really the need that Java EE 8 MVC is attempting to address? Looks like a solution looking for a problem, and a bad one at that.
Leave a reply