Getting started with MVC
MVC is a new specification introduced in the upcoming Java EE 8.
It is based on the existing JAXRS.
At the moment I wrote down these posts, most of Java EE 8 specification are still in the early disscussion stage, and MVC 1.0 is also not finalized, maybe some changes are included in future. I will update the Wiki pages and codes aligned with final Java EE 8 specification when it is released.
I will use the latest Java 8, Glassfish 4.1.1, and NetBeans IDE for these posts.
Prerequisite
Oracle JDK 8 or OpenJDK 8
Oracle Java 8 is required, go to Oracle Java website to download it and install into your system.
Optionally, you can set JAVA_HOME environment variable and add <JDK installation dir>/bin in your PATH environment variable.
The latest Apache Maven
Download the latest Apache Maven from http://maven.apache.org, and uncompressed it into your local system.
Optionally, you can set M2_HOME environment varible, and also do not forget to append <Maven Installation dir>/bin your PATH environment variable.
NetBeans IDE
Download the latest NetBeans IDE from http://www.netbeans.org, and installed it into your local disk.
Glassfish Server
Download the latest Glassfish from Glassfish official website. Currently the latest GA version is 4.1.1. Extracted it into your local disk.
NOTE: You can download the JDK and NetBeans bundle from Oracle website instead of installing JDK and NetBeans IDE respectively.
After you installed all of these, start NetBeans IDE, add Glassfish into Service tab in NetBeans IDE.
The Sample application
To demonstrate the basic usage of MVC specification, I will port the task board sample which I have implemented in the Spring4 Sandbox to demonstrate Spring MVC.
Simply, it includes the following features.
List tasks by status, display the tasks in 3 columns(like a simple kanban).
Create a new task.
Edit and update task.
Update task status(move to different columns in the task list).
Delete task if it is done.
Create a project skeleton
First of first, you should create a simple project skeleton as start point.
If you are using NetBeans IDE, it is easy to create a Maven based Java EE 7 web project in IDE directly, and add the registered Glassfish as runtime server.
It should include Java EE 7 web artifact as the dependency.
Add additional MVC api and the implemnetation dependencies.
ozark is the default reference implementation of MVC 1.0 specificaiton, which will shipped with Glassfish 5. Currently it is still under active development, what we are using here may be changed in the future.
Here we used Glassfish 4.1.1 as target runtime, so you should include them in the deployment package.
When Glassfish 5 is ready for Java EE 8, these two dependencies can be excluded and removed from POM.
Declare the MVC application
MVC does not reinvent the wheel, it reuses the effort of JAXRS specification.
Similar with activating JAXRS application, You can declare a custom Application
as our MVC application entry.
TaskController
is a controller, it acts as the C in MVC pattern.
Controller
MVC uses a @Controller
annotation to declare a JAXES resource as Controller.
@View
annotation indicates the view(eg. JSP pages) a void method will return.
Model
Models
is a container to hold the model data that will be transferred to the view.
View
Have a look at the tasks.jspx
file, I just copied some code snippets here, please checkout the source codes for details.
No surprise, just pure JSP files, I used the JSP xml form in this sample.
By default the views should be put in the /WEB-INF/views folder in projects.
In this example, when you send a GET request to /ee8-mvc/mvc/tasks, the allTasks()
method will handle this request, then find data(tasks by status here) from database, and put the query results into a Models
container, in the view pages the model data can be accessed via el directly.
The path(/ee8-mvc/mvc/tasks) is the combination of context path, mvc application path, and controller path.
Source codes
Clone the codes from my GitHub.com account.
Open the mvc project in NetBeans IDE.
Run it on Glassfish server.
After it is deployed and running on the Glassfish application server, navigate http://localhost:8080/ee8-mvc/mvc/tasks in your favorite browser.
Last updated