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

MVC 1.0 in Java EE 8: Getting Started Using Facelets

DZone's Guide to

MVC 1.0 in Java EE 8: Getting Started Using Facelets

A tutorial on using MVC with Facelets rather than JSP in Java EE 8.

· Java Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

MVC 1.0 is an action-based Model-View-Controller web framework, which will be a part of the future of Java EE 8. This post summarizes what needs to be done in order to use Facelets instead of default JSP as a view technology for MVC framework.

How to Set Up Facelets With MVC

Although MVC is a fresh new framework, the default view technology used in most examples — JSP — is rather old and sometimes cumbersome. On the other hand, the older brother JSF already builds on more modern and flexible Facelets.

Fortunately, the MVC framework has been designed to support many alternative view technologies out of the box, including Facelets.

FacesServlet needs to be mapped to *.xhtml. All xhtml pages should reside inside WEB-INF/views folder inside the web application, where the MVC framework searches for view files.

Here is a snippet in web.xml:

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

And that’s it! Now, you can start writing pages using facelets and reference them by path, which ends in xhtml.

An example Hello facelet page in WEB-INF/views/hello.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>#{hello}</h1>
    </h:body>
</html>

In order to display this page, just return "hello.xhtml" from the controller action and you’re done. The controller should also fill "hello" variable in the model.

Summary

Getting started with MVC framework is really easy once you understand the basics of JAX-RS and CDI. The only requirement is to run your application on one of the application servers that support Ozark reference implementation. It is also very easy to use various view technologies as alternatives to default JSPs.

MVC framework includes support for many view technologies and enables to plug in other technologies on top of it. Configuring Facelets required only to configure Faces servlet in web.xml — the same thing you need to do when setting up JSF.

For more details, see the oiginal article at my blog: http://itblog.inginea.eu

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:
javaee8 ,mvc ,facelets

Published at DZone with permission of Ondrej Mihalyi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}