| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| |
| <!-- |
| Copyright (c) 2009, 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
| --> |
| |
| <html> |
| <head> |
| <title>Getting Started with Java EE Applications</title> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" > |
| <meta name="description" content="A tutorial that introduces NetBeans IDE support for some of the new features in Java EE 6."> |
| <link rel="stylesheet" href="../../../netbeans.css"> |
| <meta name="author" content="ken ganfield"> |
| </head> |
| <body> |
| <h1>Getting Started with Java EE Applications</h1> |
| |
| <p>This document provides a brief introduction to some of the features introduced |
| as part of Java Enterprise Edition 6 (Java EE 6) specification. |
| To illustrate the new features, this tutorial will demonstrate how to create a simple Java EE web application that contains |
| an EJB 3.1 stateless session bean facade for an entity class. |
| You will use wizards in the IDE to generate the entity class and the session bean. |
| The code generated by the wizard uses queries that are defined in the Criteria API |
| that is part of JPA 2.x and contained in the Java EE 6 specification. |
| You will then create a named managed bean that accesses the session facade and |
| a presentation layer that uses the Facelets view framework as specified in JSF 2.x.</p> |
| |
| <p>This tutorial is based on the blog post |
| <a href="http://www.adam-bien.com/roller/abien/entry/simplest_possible_jsf_2_ejb" title="http://www.adam-bien.com/roller/abien/entry/simplest_possible_jsf_2_ejb" target="_blank"> |
| Simplest Possible JSF 2 / EJB 3.1 / JPA Component - With WAR Deployment</a> by Adam Bien. |
| You can find additional Java EE examples at Adam Bien's Kenai project |
| <a href="http://kenai.com/projects/javaee-patterns" title="http://kenai.com/projects/javaee-patterns" target="_blank"> |
| Java EE Patterns and Best Practices</a> and in his book "Real World Java EE Patterns - Rethinking Best Practices", |
| available at <a href="http://press.adam-bien.com" title="http://press.adam-bien.com" target="_blank">http://press.adam-bien.com</a>.</p> |
| |
| |
| <p><b>Tutorial Exercises</b></p> |
| <img src="../../../images_www/articles/73/netbeans-stamp-80-74-73.png" class="stamp" alt="Content on this page applies to NetBeans IDE 7.2, 7.3, 7.4 and 8.0" title="Content on this page applies to the NetBeans IDE 7.2, 7.3, 7.4 and 8.0" > |
| <ul> |
| <li><a href="#Exercise_1">Creating the Web Application Project</a></li> |
| <li><a href="#Exercise_2">Creating the Entity Class and Session Facade</a> |
| <ul> |
| <li><a href="#Exercise_2a">Creating the Entity Class</a></li> |
| <li><a href="#Exercise_2b">Creating the Session Facade</a></li> |
| </ul> |
| </li> |
| <li><a href="#Exercise_3">Creating the JSF Managed Bean and JSF Pages</a> |
| <ul> |
| <li><a href="#Exercise_3a">Creating the Managed Bean</a></li> |
| <li><a href="#Exercise_3b">Modifying the Index Page</a></li> |
| <li><a href="#Exercise_3c">Creating the Results Page</a></li> |
| </ul> |
| |
| </li> |
| <li><a href="#Exercise_4">Running the Project</a></li> |
| <li><a href="#Exercise_5">Downloading the Solution Project</a></li> |
| </ul> |
| <p><b>To follow this tutorial, you need the following software and resources.</b></p> |
| <table> |
| <tbody> |
| <tr> |
| <th class="tblheader" scope="col">Software or Resource</th> |
| <th class="tblheader" scope="col">Version Required</th> |
| </tr> |
| <tr> |
| <td class="tbltd1"><a href="https://netbeans.org/downloads/index.html">NetBeans IDE</a></td> |
| <td class="tbltd1">7.2, 7.3, 7.4, 8.0, Java EE version</td> |
| </tr> |
| <tr> |
| <td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java Development Kit (JDK)</a></td> |
| <td class="tbltd1">version 7 or 8</td> |
| </tr> |
| <tr> |
| <td class="tbltd1">GlassFish Server Open Source Edition</td> |
| <td class="tbltd1">3.x, 4.x</td> |
| </tr> |
| </tbody> |
| </table> |
| |
| <p><b>Prerequisites</b></p> |
| <p>This document assumes you have some basic knowledge of, or programming experience with, the following technologies:</p> |
| <ul> |
| <li>Java Programming</li> |
| <li>NetBeans IDE</li> |
| </ul> |
| <p class="tips">You can download <a href="https://netbeans.org/projects/samples/downloads/download/Samples/JavaEE/SimpleEE6App72.zip">a zip archive of the finished project</a>.</p> |
| |
| <!-- ===================================================================================== --> |
| |
| |
| <a name="Exercise_1"></a> |
| <!--Exercise 1: --> |
| <h2>Creating the Web Application Project</h2> |
| <p>In this exercise you create a simple web application. |
| When you create the web application you will specify GlassFish Server |
| as the target Java EE container. |
| The GlassFish server is Java EE-compatible and includes the |
| JSF 2.x libraries that are required in this application.</p> |
| |
| <p>In the New Project wizard you can choose Java EE 6 Web or Java EE 7 Web as the Java EE version. |
| Java EE 6 Web and Java EE 7 Web are lightweight Java EE profiles that contain a subset of the |
| full Java EE platform. |
| The Java EE Web profiles are designed for web applications that do not require advanced Java EE technologies |
| such as support for remote interfaces, the full EJB 3.1 specification and the Java Message Service (JMS) API.</p> |
| |
| <p>The Web profiles support the transaction processing and persistence management |
| that are commonly used in enterprise web applications. |
| You can use Java EE Web profiles for web applications that use session |
| beans with a local interface or no interface. |
| The full Java EE profile is required if the application uses a remote interface.</p> |
| |
| |
| <ol> |
| <li>Choose File > New Project (Ctrl-Shift-N; ⌘-Shift-N on Mac) from the main menu.</li> |
| <li>Select Web Application from the Java Web category and click Next.</li> |
| <li>Type <strong>SimpleEE6App</strong> for the project name and set the project location. </li> |
| <li>Deselect the Use Dedicated Folder option, if selected. Click Next.<br /> |
| (For this tutorial there is little reason to copy project libraries to a dedicated folder because |
| you will not need to share libraries with other users or projects.)<br /> |
| </li> |
| <li>Select the GlassFish server and set the Java EE Version to Java EE 6 Web or Java EE 7 Web. |
| Click Next.<br/> |
| <img src="../../../images_www/articles/74/javaee/gettingstarted/newappwizard.png" alt="New Project wizard" title="Server and Settings panel in New Project wizard" class="margin-around b-all"> |
| |
| |
| </li> |
| <li>Select JavaServer Faces in the Frameworks pane. Click Finish. |
| <p>By default, the IDE will select the JSF 2.x library if you are developing a |
| Java EE web application and deploying to the GlassFish server. |
| The version of the default JSF library depends on the version bundled with your GlassFish server. |
| The JSF 2.x library enables you to use Facelets as the page language and also provides support for JSF 1.2 and JSP.</p> |
| <img src="../../../images_www/articles/74/javaee/gettingstarted/newappwizard2.png" alt="New Project wizard" title="Frameworks panel in New Project wizard" class="margin-around b-all"></li> |
| </ol> |
| |
| <p>When you click Finish, the IDE creates the project and opens the project in the Projects window. |
| The IDE automatically creates the page <tt>index.xhtml</tt> and opens the page in the editor.</p> |
| |
| |
| |
| <a name="Exercise_2"></a> |
| <h2>Creating the Entity Class and Session Facade</h2> |
| |
| <p>In this section you will create an entity class and a session facade for the entity class. |
| An entity class is a plain old Java object (POJO), a simple Java class that is identified as an entity |
| by the <tt>@Entity</tt> annotation. |
| Starting with the Java EE 5 specification, you can use entity classes as persistent objects to represent tables in a database. |
| The Java Persistence API enables you to use persistence in web applications without the need to create an EJB module. |
| </p> |
| |
| <p>The session facade for the entity class in this application is a stateless session bean. |
| The Enterprise JavaBean (EJB) 3.1 architecture introduced as part of the Java EE 6 specification |
| enables you to create session beans without the business interfaces that were required in EJB 3.0. |
| The Java EE 6 specification also allows you to package EJB components directly in a WAR archive. |
| This simplifies development of smaller web applications by eliminating |
| the need to create separate EJB modules that are packaged as a JAR archive in an EAR archive. |
| However, for larger enterprise applications that are distributed across different machines, |
| you will still want to create EAR archives to separate your business logic from the presentation layer.</p> |
| |
| <p class="tips">For more about using EJB 3.1 in the IDE, |
| see the tutorial <a href="javaee-entapp-ejb.html">Creating an Enterprise Application with EJB 3.1</a>.</p> |
| |
| <p class="tips">For more details about entity classes, |
| see the chapter <a href="http://docs.oracle.com/javaee/7/tutorial/doc/persistence-intro.htm">Introduction to the Java Persistence API</a> |
| in the <a href="http://download.oracle.com/javaee/7/tutorial/doc/">Java EE 7 Tutorial</a>.</p> |
| |
| <p class="tips">For more information about session beans, |
| see the chapter <a href="http://docs.oracle.com/javaee/7/tutorial/doc/ejb-intro002.htm">What is a Session Bean?</a> |
| in the <a href="http://download.oracle.com/javaee/7/tutorial/doc/">Java EE 7 Tutorial</a>.</p> |
| |
| |
| <div class="indent"> |
| <a name="Exercise_2a"></a> |
| <h3>Creating the Entity Class</h3> |
| <p>In this exercise you will use the New Entity Class wizard to create a simple persistent entity class. |
| You will also use the wizard to create a persistence unit that defines the data source and entity manager used in the application. |
| You will add one field in the class to represent the data in your table and generate a getter and setter for the new field.</p> |
| |
| <p>An entity class must have a primary key. |
| When you create the entity class using the wizard, the IDE by default generates the field <tt>id</tt> and |
| annotates the field with the <tt>@Id</tt> annotation to declare the field as the primary key. |
| The IDE also adds the <tt>@GeneratedValue</tt> annotation and specifies the key generation strategy for the primary id field.</p> |
| |
| <p>Using Java Persistence in your project greatly simplifies application development by removing the need for configuring |
| deployment descriptors to provide object-relational mapping information for persistent fields or properties. |
| Instead, you can use annotations to define these properties directly in a simple Java class.</p> |
| |
| <p>Entity persistence is managed by the EntityManager API. The EntityManager API handles the |
| persistence context, and each persistence context is a group of entity instances. |
| When developing your application, you can use annotations in your class to specify the persistent context instance |
| of your entity instances. The life-cycle of the entity instances is then handled by the container.</p> |
| |
| <p>To create the entity class, perform the following steps.</p> |
| <ol> |
| <li>Right-click the project node and choose New > Other.</li> |
| <li>Select Entity Class from the Persistence category. Click Next.</li> |
| <li>Type <strong>Message</strong> for the Class Name.</li> |
| <li>Type <strong>entities</strong> for the Package.</li> |
| <li>Select Create Persistence Unit. Click Next.</li> |
| <li>Select a datasource (for example, select <tt>jdbc/sample</tt> if you want to use JavaDB). |
| <p>The datasource for <tt>jdbc/sample</tt> is bundled with the IDE when you install the IDE and the GlassFish server, |
| but you can specify a different datasource if you want to use a different database.</p> |
| <p>You can keep the other default options (persistence unit name, EclipseLink persistence provider). |
| Confirm that the persistence unit is using the Java Transaction API |
| and that the Table Generation Strategy is set to Create |
| so that the tables based on your entity classes are created when the application is deployed. |
| </p></li> |
| |
| <li>Click Finish in the New Entity Class wizard. |
| <p>When you click Finish, the IDE creates the entity class and opens the class in the editor. |
| You can see that the IDE generated the id field <tt>private Long id;</tt> and annotated the field with <tt>@Id</tt> |
| and <tt>@GeneratedValue(strategy = GenerationType.AUTO)</tt>.</p></li> |
| <li>In the editor, add the <tt>message</tt> field (in bold) below the <tt>id</tt> field. |
| <pre class="examplecode"> |
| private Long id; |
| <strong>private String message;</strong></pre> |
| </li> |
| <li>Right-click in the editor and choose Insert Code (Alt-Insert; Ctrl-I on Mac) and then select Getter and Setter.</li> |
| <li>In the Generate Getters and Setters dialog box, select the <tt>message</tt> field and click Generate. |
| <p>The IDE generates getter and setter methods for the field <tt>message</tt>.</p> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/getters-dialog.png" alt="Create Persistence Unit wizard" title="Create Persistence Unit wizard" class="margin-around b-all"></li> |
| |
| <li>Save your changes.</li> |
| </ol> |
| |
| <p>The entity class represents a table in the database. |
| When you run this application, a database table for Message will be automatically created. |
| The table will contain the columns <tt>id</tt> and <tt>message</tt>.</p> |
| |
| <p>If you look at the persistence unit in the XML editor, you can see that |
| the application will use the Java Transaction API (JTA) |
| (<tt>transaction-type="JTA"</tt>). |
| This specifies that the responsibility for managing the lifecycle of entities in the persistence context is assigned to the container. |
| This results in less code because the entity lifecycle is managed by the container and not by the application. |
| For more about using JTA to manage transactions, see the <a href="http://www.oracle.com/technetwork/java/javaee/jta/index.html" target="_blank">Java Transaction API</a> documentation.</p> |
| |
| |
| <a name="Exercise_2b"></a> |
| <h3>Creating the Session Facade</h3> |
| |
| <p>In this exercise you will use a wizard to create a stateless session facade for the Message entity. |
| The EJB 3.1. specification states that business interfaces for session beans are now optional. |
| In this application where the client accessing the bean is a local client, you have the option to use a local interface or a no-interface |
| view to expose the bean.</p> |
| |
| <p>To create the session bean, perform the following steps.</p> |
| <ol> |
| <li>Right-click the project node and choose New > Other.</li> |
| <li>Select Session Beans for Entity Classes from the Enterprise JavaBeans category. Click Next.</li> |
| <li>Select the <tt>Message</tt> entity and click Add. Click Next.</li> |
| <li>Type <strong>boundary</strong> for the package. Click Finish. |
| <p>Notice that you did not need to create a business interface for the session bean. |
| Instead, in this application the bean will be exposed to a local managed bean using a no-interface view.</p> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/sessionwizard.png" alt="Session Beans for Entity Classes wizard" title="Session Beans for Entity Classes wizard" class="margin-around b-all"></li> |
| </ol> |
| |
| <p>When you click Finish, the IDE generates the session facade class <tt>MessageFacade.java</tt> and |
| <tt>AbstractFacade.java</tt> and opens the files in the editor. |
| As you can see in the generated code, the annotation <tt>@Stateless</tt> is used to |
| declare <tt>MessageFacade.java</tt> as a stateless session bean component. |
| <tt>MessageFacade.java</tt> extends <tt>AbstractFacade.java</tt>, which |
| contains the business logic and manages the transaction.</p> |
| |
| <pre class="examplecode"> |
| @Stateless |
| public class MessageFacade extends AbstractFacade<Message> { |
| @PersistenceContext(unitName = "SimpleEE6AppPU") |
| private EntityManager em; |
| </pre> |
| |
| <p>When you create the facade for the entity using the wizard, by default the IDE adds the |
| <tt>PersistenceContext</tt> annotation (<tt>@PersistenceContext(unitName = "SimpleEE6AppPU")</tt>) |
| to inject the entity manager resource into the session bean component and to specify the name of the persistence unit. |
| In this example the name of the persistence unit is declared explicitly, |
| but the name is optional if the application has only one persistence unit.</p> |
| |
| <p>The IDE also generates methods in <tt>AbstractFacade.java</tt> to create, edit, remove and find entities. |
| The EntityManager API defines the methods that are used to interact with the persistence context. |
| You can see that the IDE generates some commonly used default query methods that can be used to find entity objects. |
| The <tt>findAll</tt>, <tt>findRange</tt> and <tt>count</tt> methods use methods defined in the Criteria API for creating queries. |
| The Criteria API is part of the JPA 2.x specification that is included in the Java EE 6 specification.</p> |
| |
| </div> |
| |
| |
| |
| <!-- ===================================================================================== --> |
| <a name="Exercise_3"></a> |
| <h2>Creating the JSF Managed Bean and JSF Pages</h2> |
| |
| <p>In this section you will create the presentation layer for the application using JavaServer Faces (JSF) 2.x |
| and a managed backing bean that is used by the JSF pages. |
| The JSF 2.x specification adds support for Facelets as the preferred view technology for JSF-based applications. |
| Starting with JSF 2.x, you can also use the <tt>@ManagedBean</tt> annotation in your source code to declare a class a managed bean. |
| You are no longer required to add entries in the <tt>faces-config.xml</tt> file to declare JSF managed beans. |
| You can use bean names in JSF pages to access methods in the managed bean.</p> |
| <p class="tips">For more about IDE support for the JavaServer Faces 2.x specification, |
| see <a href="../web/jsf20-support.html">JSF 2.x Support in NetBeans IDE</a>.</p> |
| <p class="tips">For more about the JavaServer Faces 2.x specification, see the |
| <a href="http://docs.oracle.com/javaee/7/tutorial/doc/jsf-intro.htm" target="_blank">JavaServer Faces Technology</a> chapter in |
| the Java EE 7 Tutorial.</p> |
| |
| <div class="indent"> |
| |
| <a name="Exercise_3a"></a> |
| <h3>Creating the Managed Bean</h3> |
| <p>In this exercise you will create a simple JSF managed bean that is used to access the session facade. |
| The JSF 2.x specification enables you to use annotations in the bean class |
| to identify the class as a JSF managed bean, to specify the scope and to specify a name for the bean.</p> |
| |
| <p>To create the managed bean, perform the following steps.</p> |
| <ol> |
| <li>Right-click the project node and choose New > Other.</li> |
| <li>Select JSF Managed Bean from the JavaServer Faces category. Click Next.</li> |
| <li>Type <strong>MessageView</strong> for the Class Name. |
| <p>You will use the Managed Bean name <tt>MessageView</tt> as the value for the <tt>inputText</tt> and |
| <tt>commandButton</tt> in the JSF page <tt>index.xhtml</tt> when calling methods in the bean.</p> |
| </li> |
| <li>Type <strong>my.presentation</strong> for the Package.</li> |
| <li>Type <strong>MessageView</strong> for the Name that will be used for the managed bean. |
| <p class="notes"><strong>Note.</strong> When you create a managed bean using the wizard, the IDE will |
| by default assign a name to the bean based on the name of the bean class and beginning with a lower-case letter. |
| For this tutorial and demonstration purposes, you are explicitly assigning the bean a name |
| that begins with an upper-case letter. |
| When you reference the bean in the JSF pages you will use <tt>MessageView</tt> instead of <tt>messageView</tt>. |
| If you did not explicitly assign the name, you would use the default <tt>messageView</tt> in the JSF page.</p></li> |
| <li>Set Scope to Request. Click Finish.</li> |
| </ol> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/newjsfbean.png" alt="New JSF Managed Bean wizard" title="New JSF Managed Bean wizard" class="margin-around b-all"> |
| |
| <p>When you click Finish, the IDE creates the bean class and opens the class in the editor. |
| In the Projects window you will see the following files.</p> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/projectswindow.png" alt="Project window showing file structure" title="Project window showing file structure" class="margin-around b-all"> |
| |
| <p>In the editor, you can see that the IDE added the <tt>@ManagedBean</tt> and <tt>@RequestScoped</tt> |
| annotations and the name of the bean.</p> |
| <pre class="examplecode"> |
| @ManagedBean(name="MessageView") |
| @RequestScoped |
| public class MessageView { |
| |
| /** Creates a new instance of MessageView */ |
| public MessageView() { |
| } |
| |
| } |
| </pre> |
| |
| <p>You will now add an <tt>@EJB</tt> annotation to use dependency injection to obtain |
| a reference to the MessageFacade session bean. |
| You will also call the <tt>findAll</tt> and <tt>create</tt> methods that are exposed in the facade. |
| The IDE's code completion can help you when typing the methods.</p> |
| <ol> |
| <li>Right-click in the editor and choose Insert Code (Alt-Insert; Ctrl-I on Mac) |
| and choose Call Enterprise Bean in the popup menu.</li> |
| <li>Select MessageFacade in the Call Enterprise Bean dialog box. Click OK.<br/> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/callbean.png" alt="Call Enterprise Bean dialog" title="Call Enterprise Bean dialog" class="margin-around b-all"> |
| <p>When you click OK, the IDE adds the following code (in bold) to inject the bean.</p> |
| <pre class="examplecode"> |
| public class MessageView { |
| |
| /** Creates a new instance of MessageView */ |
| public MessageView() { |
| } |
| |
| // Injects the MessageFacade session bean using the @EJB annotation |
| <strong>@EJB |
| private MessageFacade messageFacade;</strong> |
| } |
| </pre> |
| </li> |
| <li>Add the following code to create a new instance. |
| <pre class="examplecode">/** Creates a new instance of MessageView */ |
| public MessageView() { |
| this.message = new Message(); |
| }</pre> |
| </li> |
| <li>Add the following code to the class. |
| <pre class="examplecode"> |
| // Creates a new field |
| private Message message; |
| |
| |
| // Calls getMessage to retrieve the message |
| public Message getMessage() { |
| return message; |
| } |
| |
| // Returns the total number of messages |
| public int getNumberOfMessages(){ |
| return messageFacade.findAll().size(); |
| } |
| |
| // Saves the message and then returns the string "theend" |
| public String postMessage(){ |
| this.messageFacade.create(message); |
| return "theend"; |
| } |
| </pre> |
| </li> |
| <li>Right-click in the editor and choose Fix Imports (Alt-Shift-I; ⌘-Shift-I on Mac) and save your changes.</li> |
| </ol> |
| <p class="tips">You can use the code completion in the editor to help you type your code. |
| </p> |
| |
| |
| <p>Notice that the <tt>postMessage</tt> method returns the string "theend". |
| The JSF 2.x specification enables the use of implicit navigation rules in applications that use Facelets technology. |
| In this application, no navigation rules are configured in <tt>faces-config.xml</tt>. |
| Instead, the navigation handler will try to locate a suitable page in the application. |
| In this case, the navigation handler will try to locate a page named <tt>theend.xhtml</tt> when the <tt>postMessage</tt> method is invoked.<p> |
| |
| |
| <a name="Exercise_3b"></a> |
| <h3>Modifying the Index Page</h3> |
| |
| <p>In this exercise you will make some simple changes to the <tt>index.xhtml</tt> page to add some UI components. |
| You will add a form with an input text field and a button.</p> |
| <ol> |
| <li>Open <tt>index.xhtml</tt> in the editor.</li> |
| <li>Modify the file to add the following simple form between the <tt><h:body></tt> tags. |
| <pre class="examplecode"> |
| <h:body> |
| <strong><f:view> |
| <h:form> |
| <h:outputLabel value="Message:"/><h:inputText value="#{MessageView.message.message}"/> |
| <h:commandButton action="#{MessageView.postMessage}" value="Post Message"/> |
| </h:form> |
| </f:view></strong> |
| </h:body></pre> |
| <p class="tips">The JSF code completion can help you when you type the code. |
| </p> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/jsfcodecompletion1.png" alt="Code completion in the source editor" title="Code completion in the source editor" class="margin-around b-all"> |
| <p class="notes"><strong>Note.</strong> If you copy and paste the code into the file, |
| you will see a warning in the left margin next to the line containing <tt><f:view></tt>. |
| You can place your insert cursor in the line and type Alt-Space to open the hint |
| on how to resolve the error. The hint informs you that you need to add the |
| <tt>xmlns:f="http://xmlns.jcp.org/jsf/core"</tt> library declaration.</p> |
| </li> |
| <li>Save your changes. |
| </li> |
| </ol> |
| |
| <p>The <tt>inputText</tt> and <tt>commandButton</tt> components will invoke the methods in the named JSF managed bean <tt>MessageView</tt>. |
| The <tt>postMessage</tt> method will return "theend", and the navigation handler will look for a page named <tt>theend.xhtml</tt>.</p> |
| |
| <a name="Exercise_3c"></a> |
| <h3>Creating the Results Page</h3> |
| |
| <p>In this exercise you will create the JSF page <tt>theend.xhtml</tt>. |
| The page will be displayed when the user clicks the Post Message button in <tt>index.xhtml</tt> and invokes the <tt>postMessage</tt> method |
| in the JSF managed bean.</p> |
| <ol> |
| <li>Right-click the project node and choose New > Other.</li> |
| <li>Select JSF Page from the JavaServer Faces category. Click Next.</li> |
| <li>Type <strong>theend</strong> as the File Name.</li> |
| <li>Confirm that the Facelets option is selected. Click Finish.<br> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/result-jsf-page.png" alt="screenshot of New JSF File wizard" title="Creating the theend JSF file in the New JSF File wizard" class="margin-around b-all"> |
| </li> |
| <li>Modify the file by typing the following between the <h:body> tags. |
| <pre class="examplecode"> |
| <h:body> |
| <strong><h:outputLabel value="Thanks! There are "/> |
| <h:outputText value="#{MessageView.numberOfMessages}"/> |
| <h:outputLabel value=" messages!"/></strong> |
| </h:body></pre> |
| </li> |
| </ol> |
| <p class="tips">When you start typing, the IDE automatically adds the |
| <tt>xmlns:h="http://xmlns.jcp.org/jsf/html"</tt> tag library definition to the file for the JSF elements.</p> |
| </div> |
| |
| <a name="Exercise_4"></a> |
| <h2>Running the Application</h2> |
| <p>You are now finished coding the application. You can now test the application in your browser.</p> |
| <ol> |
| <li>Right-click the project node in the Projects window and choose Run. |
| <p>When you choose Run, the IDE builds and deploys the application and opens <tt>index.xhtml</tt> in your browser.</p></li> |
| <li>Type a message in the text field. Click Post Message. <br/> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/browser1.png" alt="Application in browser" title="Application in browser" class="margin-around b-all"> |
| |
| </li> |
| </ol> |
| |
| <p>When you click Post Message, the message is saved to the database and the number of messages is retrieved and displayed.</p> |
| <img src="../../../images_www/articles/72/javaee/gettingstarted/browser2.png" alt="Application in browser showing results" title="Application in browser showing results" class="margin-around b-all"> |
| |
| |
| <a name="Exercise_5"></a> |
| <h2>Downloading the Solution Project</h2> |
| <p>You can download the sample projects used in this tutorial in the following ways.</p> |
| <ul> |
| <li>Download <a href="https://netbeans.org/projects/samples/downloads/download/Samples/JavaEE/SimpleEE6App72.zip">a zip archive of the finished project</a>.</li> |
| <li>Checkout the project sources from the NetBeans Samples by performing the following steps: |
| <ol> |
| <li>Choose Team > Subversion > Checkout from the main menu.</li> |
| <li>In the Checkout dialog box, enter the following Repository URL:<br/> |
| <tt>https://svn.netbeans.org/svn/samples~samples-source-code</tt><br/> |
| Click Next.</li> |
| <li>Click Browse to open the Browse Repository Folders dialog box.</li> |
| <li>Expand the root node and select <strong>samples/javaee/SimpleEE6App</strong>. Click OK.</li> |
| <li>Specify the Local Folder for the sources (the local folder must be empty).</li> |
| <li>Click Finish. |
| <p>When you click Finish, the IDE initializes the local folder as a Subversion repository |
| and checks out the project sources.</p> |
| </li> |
| <li>Click Open Project in the dialog that appears when checkout is complete.</li> |
| </ol> |
| |
| <p class="notes"><strong>Notes.</strong></p> |
| <ul> |
| <li>You need a Subversion client to checkout the sources. |
| For more about installing Subversion, see the section on <a href="../ide/subversion.html#settingUp">Setting up Subversion</a> in the <a href="../ide/subversion.html">Guide to Subversion in NetBeans IDE</a>.</li> |
| </ul> |
| </li> |
| </ul> |
| |
| |
| <br> |
| <div class="feedback-box" ><a href="/about/contact_form.html?to=3&subject=Feedback:%20Getting%20Started%20with%20Java%20EE%206%20Applications">Send Feedback on This Tutorial</a></div> |
| <br style="clear:both;" /> |
| <!-- ======================================================================================= --> |
| <h2><a name="nextsteps"></a>See Also</h2> |
| <p>For more information about using NetBeans IDE to develop Java EE applications, see the following resources: |
| </p> |
| <ul> |
| <li><a href="javaee-intro.html">Introduction to Java EE Technology</a></li> |
| <li><a href="../web/jsf20-support.html">JSF 2.x Support in NetBeans IDE</a></li> |
| <li><a href="../../trails/java-ee.html">Java EE & Java Web Learning Trail</a></li> |
| </ul> |
| <p>You can find more information about using Java EE technologies to develop applications in the |
| <a href="http://download.oracle.com/javaee/7/tutorial/doc/">Java EE 7 Tutorial</a>.</p> |
| <p>To send comments and suggestions, get support, and keep informed on the latest |
| developments on the NetBeans IDE Java EE development features, <a href="../../../community/lists/top.html">join |
| the nbj2ee mailing list</a>.</p> |
| </body> |
| </html> |