© Ioannis Kostaras, Constantin Drabo, Josh Juneau, Sven Reimers, Mario Schröder, Geertjan Wielenga 2020
I. Kostaras et al.Pro Apache NetBeanshttps://doi.org/10.1007/978-1-4842-5370-0_5

5. Apache NetBeans Java EE Development

Ioannis Kostaras1 , Constantin Drabo2, Josh Juneau3, Sven Reimers4, Mario Schröder5 and Geertjan Wielenga6
(1)
The Hague, South Holland, The Netherlands
(2)
Ouagadougou, Burkina Faso
(3)
Chicago, IL, USA
(4)
Salem, Germany
(5)
Berlin, Germany
(6)
Amsterdam, The Netherlands
 

Java EE support has been included with Apache NetBeans since release 11.0. Prior to this release, Java EE support had to be added to Apache NetBeans 9.0+ as a plugin, since it was still licensed under Oracle. If the plugin was added, the functionality for developing, debugging, and maintaining Java EE was the same in Apache NetBeans 9.0 as it is in release 11.0 and beyond. The support provides the ability to add application server containers that can be managed from within the IDE. Full support for Maven web applications and Java EE technologies such as Enterprise JavaBeans (EJB), Contexts and Dependency Injection (CDI), and JavaServer Faces (JSF) allows developers the convenience of auto-completion, code fragments, and easy syntax recognition. The bottom line is that Apache NetBeans is a tremendous tool for development of Java EE applications … both older and modern.

In this chapter, we’ll take a look at the support, in detail, that Apache NetBeans provides for Java Enterprise development. You will learn how to manage containers from within the IDE, develop back-end business logic, and Java Persistence API (JPA) queries. In the end, you will have a basic understanding of the conveniences provided by Apache NetBeans for full stack Java EE development.

Configuring Server Containers

Before you can associate application projects with a container for deployment or testing, you must register one or more application server containers for use by Apache NetBeans. It is a good idea to register a container with Apache NetBeans even in the case where you are developing microservices that will be deployed to a container, such as Payara Micro or Wildfly. Note that it is good practice to only configure application server containers for development purposes within Apache NetBeans, and not for deployment to production containers.

To add a local or remote application server container to Apache NetBeans, perform the following tasks:
  1. 1.

    Navigate to the Services window and right-click on the Servers menu selection. Click Add Server, as shown in Figure 5-1.

     
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig1_HTML.jpg
Figure 5-1

Add Application Server Container to Apache NetBeans IDE

  1. 2.

    When the Add Server Instance dialog appears, choose the server type that you wish to add (Figure 5-2).

     
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig2_HTML.jpg
Figure 5-2

Add Server Instance

  1. 3.

    On the next screen, enter the path to the application server installation that you would like to configure within Apache NetBeans (Figure 5-3). Once you have chosen the location, click the Finish button.

     
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig3_HTML.jpg
Figure 5-3

Set Server Location

  1. 4.

    It is now possible to deploy applications to the application server container(s) that have been registered within the IDE. To do so, right-click on an enterprise application project (Maven Web Application, etc.) and then specify the server of choice within the project properties. Note that you can also perform some basic application server tasks by selecting the application server from within the Servers window (Figure 5-4).

     
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig4_HTML.jpg
Figure 5-4

Expand and Administer Server within Apache NetBeans

Creating a Maven Web Application

There are a variety of configurations from which to choose for the creation of Java Enterprise projects within Apache NetBeans. This chapter covers the creation of Maven Web Applications in detail, which is the de facto standard and default project selection for Java EE and Jakarta EE development within Apache NetBeans.

To begin the creation of a new Maven Web Application project, open the New Project dialog by choosing FileNew Project. In the New Project dialog, you will see a number of different project categories listed in the left-hand list box. Selecting one of the categories will display the project types for the selected category within the right-hand list box. Select the Java with Maven category, and then select Web Application as the project type (Figure 5-5).
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig5_HTML.jpg
Figure 5-5

New Maven Web Application

Note

The Java with Maven category allows the creation of projects using the old-style Java EE and J2EE configurations such as EJB Module and Enterprise Application. In most cases, these projects are no longer being used for creating new applications. However, they remain supported by Apache NetBeans.

After a project type is chosen and Next is clicked, the New Web Application dialog will open. Enter a project name and location, as shown in Figure 5-6. Once finished, choose Next.
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig6_HTML.jpg
Figure 5-6

New Web Application Dialog

In the Server and Settings screen, choose the application server container that you wish to use for development purposes (see Configuring Application Servers in NetBeans), along with the Java EE version that you wish to use. If you plan to use CDI, then select the designated check box (Figure 5-7).
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig7_HTML.jpg
Figure 5-7

Setting the Application Server Container

Developing JSF Applications

Utilizing the Apache NetBeans IDE, it is easy to develop JSF applications due to the wizards and file recognition. In this section, we will walk through the development of a JSF application from a standard Maven Web Application project. Therefore, if you haven’t done so already, utilize the information in the previous section to create a bare-bones project.

Creating JSF Application Files

If you wish to create JSF views, then right-click on the Web Pages node, and if you wish to create a Java source file for JSF, right-click on the Source Packages node. Right-clicking on the appropriate node ensures that the files are generated within the correct project area. From within the context menu, choose New, and then Other… in order to open the New File dialog. Within the dialog, choose JavaServer Faces from the “Categories” list box to open the JSF file types within the left-side list box (Figure 5-8). In this example, right-click on Web Pages and create a new JSF Page.
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig8_HTML.jpg
Figure 5-8

Create JSF File

The JSF file types include the following options:
  • JSF Page

  • JSF Managed Bean (This creates a CDI Controller when using Java EE 7+)

  • JSF Faces Configuration

  • JSF Composite Component

  • JSF Resource Library Contract

  • JSF Pages from Entity Classes

  • JSF Faces Component

  • Faces Template

  • Faces Template Client

Note

As of Java EE 7+ and Jakarta EE 8, you should not be creating JSF Managed Beans any longer. This is because the JSF Managed Bean technology is being deprecated in favor of CDI beans. Therefore, the JSF Managed Bean option creates a CDI Controller.

After clicking Next, the JSF Page file selection opens a dialog that can be used to generate a new JSF Page (Figure 5-9). The dialog allows one to choose a file location and name, and it also contains the ability to apply different options for the page type. The page type options include Facelets (default), JSP File, or JSP Segment. The examples throughout this chapter use the Facelets page type.
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig9_HTML.jpg
Figure 5-9

New JSF Page Dialog

The JSF Managed Bean file selection opens a dialog that allows one to generate a CDI Managed Bean controller class (Figure 5-10). The dialog provides the ability to add the bean data to the faces-config.xml file, if desired, as well as choose the scope of the bean.

Note

The faces-config.xml (JSF Faces Configuration) file is not generated by default, as it is not a required file for a JSF project. However, it can be added using the Other..., JavaServer Faces context menu.

../images/479166_1_En_5_Chapter/479166_1_En_5_Fig10_HTML.jpg
Figure 5-10

New JSF Managed Bean

The JSF Faces Configuration file selection is used to generate a faces-config.xml file for a project. However, this option is not required if you choose to create a JSF project within the NetBeans Project Creation wizard.

The JSF Composite Component file selection opens a dialog that can be used to create a composite component file. The dialog does not provide many options other than the ability to choose a file location and name. The generated file contains the skeleton of a composite component, as listed in the following lines:
<?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:cc="http://xmlns.jcp.org/jsf/composite">
  <!-- INTERFACE -->
  <cc:interface>
  </cc:interface>
  <!-- IMPLEMENTATION -->
  <cc:implementation>
  </cc:implementation>
</html>

The JSF Pages from Entity Classes file selection can be quite powerful in that it allows you to choose an Entity Class from which to generate one or more JSF pages, resulting in the page(s) being bound to the entity class for generation and update of those entity records. In order to use this option, the project must contain at least one entity class.

Developing Entity Classes

The Apache NetBeans IDE provides facilities to help develop Entity Bean classes, either manually or based upon a selected database table. To access the entity class wizards, right-click a project’s Source Packages folder to open the context menu, and then choose NewOther to open the New File dialog. Once it is open, choose the Persistence category from the left-side list box to display the file types in the right-side list box (Figure 5-11).
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig11_HTML.jpg
Figure 5-11

Persistence File Types

The Entity Class file type allows you to generate a blank entity class. The Entity Classes from Database file type allows you to create entity class(es) based upon the selected database tables. In doing so, all of the requisite code for mapping the entity class(es) to the selected database tables are automatically generated for you.

Using Java Persistence Query Language (JPQL)

Apache NetBeans includes a feature that allows one to query a database utilizing the Java Persistence Query Language (JPQL) syntax. This can be quite helpful for those who are using JPQL in their EJB session beans or RESTful web services. To utilize the JPQL query tool, expand an Apache NetBeans web project that contains a persistence.xml configuration file in the project’s Configuration Files directory. Then perform the following steps:
  1. 1.

    Right-click the persistence.xml configuration file to open the context menu.

     
  2. 2.

    Click Run JPQL Query to open the tool (Figure 5-12), type the query, and click the Run button on the upper right-hand side of the query editor.

    NoteTo add a persistence.xml configuration file for JPA configuration, right-click on the project and choose: Other…, Persistence, Persistence Unit.

     
../images/479166_1_En_5_Chapter/479166_1_En_5_Fig12_HTML.jpg
Figure 5-12

JPQL Tool

Deploying and Debugging

The Apache NetBeans IDE makes the development life cycle for enterprise applications very easy. To deploy and test a Maven Web Application, ensure that a local application server container target that is registered within Apache NetBeans is set correctly within the “Run” tab of the project properties. Once set, a project can be deployed by right-clicking on the project and choosing “Run” from the contextual menu. Once chosen, the project WAR file is deployed to the target application server if everything is compiled correctly, and the application is opened in the default browser.

Note

A different default browser can be configured for each project within Apache NetBeans by selecting the browser in the toolbar (Figure 5-13).

../images/479166_1_En_5_Chapter/479166_1_En_5_Fig13_HTML.jpg
Figure 5-13

Choosing Default Browser

To debug a project, right-click on the project and choose “Debug” from the contextual menu. This will cause the target application server container to start in “Debug” mode, and the project WAR file will be deployed accordingly. When the application server is started in debug mode, breakpoints can be set within the project and watches can also be configured, just as is the case with standard Java SE applications. Please refer to Chapter 6 for more details on debugging and profiling applications.

Other Java EE Support Features

The Apache NetBeans 11.0 IDE contains support for a number of file types to aid the development of Java EE applications. There have been a number of wizards covered previously in this chapter, and in this section, we will take a brief look at some of the file support.

By right-clicking on a project package, then choosing “New” ➤ “Other” the “New File” dialog appears. This dialog contains a number of categories, including “Web,” “HTML5/JavaScript,” “Bean Validation,” and “Contexts and Dependency Injection” to name a few. Inside each of these categories are a number of file options. Each of the supported file types includes auto-complete for the syntax, color coding, and nice formatting.

Perhaps the most widely used category is the “Web” category. In this category are the following file types (geared toward front-end development):
  • JSP

  • JSF

  • Servlet

  • Filter

  • Web Application Listener

  • WebSocket Endpoint

  • HTML

  • XHTML

  • Cascading Style Sheet

  • JavaScript

  • Json

  • Tag Libraries

Some of the file types will be placed into the “Web Pages” folder of a web project, while others will create Java source files or XML configuration. Each of the file types in this section are geared toward front-end development.

Other file type categories that are geared toward enterprise development include the following:
  • HTML/JavaScript

  • JavaServer Faces

  • Bean Validation

  • Struts

  • Spring Framework

  • Enterprise JavaBeans

  • Contexts and Dependency Injection

  • Selenium Test

  • Persistence

  • Web Services

  • GlassFish

  • WebLogic

  • Other

Summary

The Apache NetBeans IDE includes exceptional support for Java EE applications. It is easy to build a Java EE project from the ground up, with options for generating front-end, back-end, and configuration files. By adding a container for deployment, the IDE allows for easy debugging and deploy-on-save options. Apache NetBeans helps boost productivity for beginner and advanced enterprise developers alike.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.221