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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
javaee8 ,mvc ,facelets

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}