Skip Headers
NetBeans Developing Applications with NetBeans IDE
Release 7.4

E40142-08
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

12 Using Web Application Frameworks

This chapter lists various Web application frameworks supported by NetBeans IDE and describes how to use the IDE for Web application development.

This chapter contains the following sections:

12.1 About Using Web Application Frameworks

The IDE provides built-in support for various web frameworks, including:

Framework support in the IDE is generally specific to the framework you are working with. However, support typically consists of the following:

12.2 Working with the JavaServer Faces Framework

JavaServer Faces (JSF) technology is a server-side user interface component framework for building Java technology-based web applications. The main components of JavaServer Faces technology are as follows:

JavaServer Faces technology provides a well-defined programming model and various tag libraries. These features significantly ease the burden of building and maintaining web applications with server-side UIs. With minimal effort, you can:

For a complete description of the JSF framework, including documentation and tutorials, see: http://www.oracle.com/technetwork/java/javaee/javaserverfaces-139869.html.

The IDE provides functional support for the JavaServer Faces framework by including the following features:

Note:

Because JSF 2.0 (JSR-314) requires Java EE 6, support for JSF 2.0 is available based on your project's Java EE version. IDE continues to provide support for JSF 1.2 (JSR 127). This support is automatically available to you if your project does not rely on Java EE 6.

For more information on the JSF Framework, see the following resources:

12.2.1 How to Create a New Project with JSF Support

You can create a new JavaServer Faces application using the New Web Application wizard. JSF support is determined based on the Java EE version you specify in the wizard.

Java EE Version JSF Version

7

2.2

6

2.0

5

1.2


When you create a new web application with JSF support, an empty project template is generated that contains various JSF-specific artifacts. JSF 2.x support is defined by the following:

  • Facelets files (.xhtml) are the default page language

  • JSF 2.x libraries are added to the project's classpath

  • The Faces servlet and servlet mapping are added to the project's deployment descriptor

For JSF 1.2, support constitutes the following:

  • JSP files (.jsp) are the default page language

  • JSF 1.2 libraries are added to the project's classpath

  • The Faces servlet and servlet mapping are added to the project's deployment descriptor

  • A Faces configuration file (faces-config.xml) is included in the project

To create a new web application with JSF support:

  1. Choose File > New Project.

  2. From the Java Web category, select Web Application.

  3. Continue with the wizard until you reach the Frameworks panel. Select JavaServer Faces.

    After selecting JavaServer Faces, various configuration options become available to you. You can determine how your project has access to JSF libraries. Click the Configuration tab to specify how the Faces servlet will be registered in the project's deployment descriptor, or change the project's default page language.

  4. Click Finish. The new project is generated and opens in the IDE.

After you create a web application with JSF support, you can begin using JSF-specific wizards for your project.

12.2.2 How to Add JSF Support to an Existing Web Application

If you want to add JSF support to an existing Java web application, you can do so from your project's Properties window.

The "support" available to you is determined by the JSF version your project uses. For Java EE 7 Web, JSF 2.2 is the default JSF version; for Java EE 6, JSF 2.0 is the default JSF version; projects relying on Java EE 5 (and previous versions) use JSF 1.2.

To add JSF support to an existing web application:

  1. In the Projects window, right-click a standard web project's node and choose Properties.

  2. Click the Frameworks category.

  3. Click the Add button, then from the list of available frameworks, select JavaServer Faces.

  4. Click OK to confirm your selection and exit the dialog box, then click OK to exit the Project Properties window.

Note:

You can also add support for JSF to the application as one of the steps in the wizard when Generating JSF Pages from Entity Classes.

12.2.3 How to Create a JSF Page

Use the JSF Page wizard to create Facelets and JSP pages for your project.

To create a new JSF page:

  1. In the Projects window, right-click your project node and choose New > JSF Page. The File wizard opens.

    (If JSF Page is not listed, choose Other. Then select the JavaServer Faces category and JSF Page file type. Click Next.)

  2. In the File Name field, type in a name for the file.

  3. In Location, specify the top-level location for the file. ('Web Pages' is the default option, and places the file in the project's web root.

  4. In Folder, specify a folder within the selected location, if you require. The Created File field provides a read-only path to the new location for the file.

  5. Under Options, indicate the page type you want to create. (Facelets is the default option for Java EE 6 and Java EE 7 projects.) If you select the JSP option, you can also specify whether you want to create a JSP fragment.

  6. Click Finish. The new file is generated and opens in the editor.

12.2.4 How to Edit a JSF page

You can edit JSF pages using the IDE's source editor. From the Projects window, you can double-click any JSF-related file node to open it in the editor.

Facelets Files

The IDE's editor provides the following support when editing Facelets pages.

  • Code completion:

    • JSF and Facelets tags: Press Ctrl-Space on JSF and Facelets tags to invoke a pop-up listing valid tag entries for completing a tag you've begun typing.

    • JSF namespaces: If you apply code-completion for a tag whose namespace is not yet declared in the file, the namespace is automatically added to the page's <html> tag.

    • JSF namespace paths: If you manually declare a namespace, press Ctrl-Space between the quotes of a namespace declaration to invoke a list of possible paths.

    • EL (Expression Language) expressions: When typing EL expressions in the editor, press Ctrl-Space to call up a list of possible entries for completing the expression. The pop-up list typically includes JSF managed beans and their properties, property bundle messages, and implicit objects.

  • Documentation: Press Ctrl-Space on JSF and Facelets tags to invoke a documentation pop-up that describes the given tag.

  • Hints and Error Messages: The editor provides you with warning and error messages while you edit JSF pages. Errors display with a red badge in the left margin of the editor, and the corresponding code is underlined in red. You can hover your mouse over the badge or underlined code to view a tooltip describing the cause of the error. The editor checks whether:

    • a declared library exists

    • the library matched by the tag prefix contains such a component or tag

    • the tag contains all required attributes

    • all entered attributes are defined in the component's interface

    • undeclared components exist on the page

    • taglib declarations exist without usages

  • Syntax highlighting:

    • JSF and Facelets tags display in blue.

    • Tag attributes display in green.

    • Tag attribute values display in orange.

    • EL expressions display in black, with light-green background.

Configuration Files

JSF 2.2 and JSF 2.0 do not require the Faces configuration file (faces-config.xml), however, if you add this file to your project, the following support is available.

Configuration Dialogs

Right-click menu support provides several dialogs that enable you to create entries into the configuration file:

  • Navigation Case dialog: Right-click in the editor and choose Insert > Navigation Case.

  • Navigation Rule dialog: Right-click in the editor and choose Insert > Navigation Rule.

  • Managed Bean dialog: Right-click in the editor and choose Insert > Managed Bean.

Hyperlinking

Hyperlinking is available in JSF configuration files, enabling you to quickly navigate between a source and its reference. To make use of hyperlinking in configuration files, hover the mouse over a JSF class entity while pressing Ctrl. The class declaration displays as a hyperlink. Clicking on the link opens the appropriate Java class in the Source Editor. In Facelets (and JSP) pages, you can press Ctrl while hovering over managed beans, or accessor methods used within JSF tags. Clicking the hyperlink that displays opens the respective class or property in the Source Editor.

12.2.5 How to Create a JSF Facelets Template

You can create Facelets Templates for your JSF 2.x application using the Facelets Template wizard. The wizard creates an XHTML template file using <h:head> and <h:body> tags, and places associated stylesheets in the resources/css folder of your application's web root. You can choose from eight unique layout styles, and specify whether the layout is implemented using CSS or an HTML <table> element.

After you create a Facelets template, you can use the Facelets Template Client wizard to generate pages that reference the template. The client will be formatted according to the referenced template.

To create a new Facelets template:

  1. In the Projects window, right-click your project node and choose New > Facelets Template. The Facelets Template wizard opens.

    (If Facelets Template is not listed, choose Other. Then select the JavaServer Faces category and Facelets Template file type. Click Next.)

  2. In the File Name field, type in a name for the template.

  3. In Location, specify the top-level location for the file. ('Web Pages' is the default option, and places the file in the given project's web root.

  4. In Folder, specify a folder within the selected location, if you require. The Created File field provides a read-only path to the new location for the template file.

  5. For Layout Style, specify whether you want the layout to be generated using CSS styles or an HTML table.

  6. Select the icon that corresponds to the layout you want generated. (A black layout icon indicates that the icon is selected.)

  7. Click Finish. The new Facelets template is generated and opens in the editor. The wizard generates a default.css file, and a cssLayout.css or tableLayout.css file, depending on your layout selection.

12.2.6 How to Create Composite Components

JSF 2.x has simplified the process of creating composite user interface (UI) components, which can be reused in web pages. You can use the IDE's Composite Component wizard to generate a Facelets template for a JSF composite component.

You can access the Composite Component wizard from the JavaServer Faces category in the IDE's File wizard (Ctrl-N). However, a more intuitive way to prompt the wizard is by highlighting the code snippet from a Facelets page in the editor, then choosing Convert to Composite Component from the right-click menu.

To create a new composite component:

  1. Open a Facelets page contained in your project. In the Projects window, double-click any Facelets Page node to open it in the editor.

  2. In the editor, highlight the snippet you want to create a component from.

  3. Right-click the highlighted snippet and choose Convert to Composite Component. The Composite Component wizard opens, containing the selected snippet in its Implementation Section panel.

  4. In the File Name field, type in a name for the composite component.

  5. In Location, specify the top-level location for the file. ('Web Pages' is the default option, and places the file in the project's web root.

  6. In Folder, specify a folder that will contain the composite component. The resources/ezcomp folder is provided by default. If the folder does not already exist, the wizard creates it.

    The Created File field provides a read-only path to the new location for the component.

  7. Click Finish. The new composite component source file is generated in the specified location, and a new component tag is inserted into the location in the editor where you highlighted the snippet. The namespace for the component is also automatically added to the page's <html> tag.

12.2.7 How to Create a JSF Managed Bean

You can create JSF managed beans for your application using the IDE's Managed Bean wizard. In JSF 2.x, any metadata that you specify in the wizard is translated into annotations that are applied to the managed bean once it is generated. If you prefer to register the managed bean with the application using a Faces configuration file (faces-config.xml) instead, the wizard provides the option of doing so.

To create a new JSF managed bean:

  1. In the Projects window, right-click your project node and choose New > JSF Managed Bean. The JSF Managed Bean wizard opens.

    (If JSF Managed Bean is not listed, choose Other. Then select the JavaServer Faces category and JSF Managed Bean file type. Click Next.)

  2. In the Class Name field, type in a name for the managed bean.

  3. In Location, specify the top-level location for the file. ('Source Packages' is the default option, and places the file in the project's src/java folder.

  4. In Package, specify a package name for the new class. If you specify the name of a package that does not exist, it will be created upon completing the wizard. The Created File field provides a read-only path to the new location for the file.

  5. (Optional) If you want to register the managed bean with the application using a Faces configuration file, select the 'Add data to configuration file' option. Doing so will prevent the wizard from generating annotations in the new class. If a faces-config.xml does not yet exist for the application, one will be created upon completing the wizard.

  6. (Optional) Specify configuration details:

    • Name: Specifies the name which the managed bean can be invoked by. If left blank, the managed bean can be accessed using the name of the managed bean class, but with the first letter in lowercase.

    • Scope: Specifies the scope into which a newly created instance of the specified managed bean will be stored. (Default is request; choose none if you want to refrain from placing bean instances in any scope).

    • Description: A textual description of the element, which can be flagged with a language code using the xml:lang attribute.

  7. Click Finish. The new managed bean is generated and opens in the editor.

12.2.8 How to Work with JSF Components in the Palette

You can make use of JSF components listed in the Palette (Window > Palette). Components in the Palette represent code snippets which you can drag-and-drop into the opened file in the Source Editor. JSF components include:

  • Metadata: Invokes a dialog to add name-value pairs within JSF metadata tags. For example, if you specify 'myId' and 'myValue' as a name-value pair, the following code snippet is produced as shown in Example 12-1.

    Example 12-1 JSF Metadata Tags

    <f:metadata>
       <f:viewParam id='myId' value='myValue'/>
    </f:metadata>
    

    The <f:metadata> tag requires that you add the JSF core tag library to your page's root element (typically the <html> tag): xmlns:f="http://java.sun.com/jsf/core"

  • JSF Form: Adds the code snippet to the page as shown in Example 12-2.

    Example 12-2 JSF Form Code Snippet

    <f:view>
       <h:form>
       </h:form>
    </f:view>
    

    The JSF Form component requires that you add the JSF core and html tag libraries to your page's root element (typically the <html> tag) as shown in Example 12-3.

    Example 12-3 JSF Core and HTML Tag Libraries

    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    
  • JSF Form from Entity: Invokes a dialog enabling you to associate data from an entity class to fields contained in a JSF form.

  • JSF Data Table: Adds the code snippet to the page. as shown in Example 12-4.

    Example 12-4 JSF Data Table Code Snippet

    <f:view>
      <h:form>
        <h:dataTable value="#{}" var="item">
        </h:dataTable>
      </h:form>
    </f:view>
    

    The JSF Form component requires that you add the JSF core and html tag libraries to your page's root element (typically the <html> tag) as shown in Example 12-5.

    Example 12-5 JSF Core and HTML Tag Libraries

    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    
  • JSF Data Table from Entity: Invokes a dialog enabling you to associate data from an entity class to fields contained in a JSF data table.

The five JSF components included in the Palette can equally be accessed by typing 'jsf' in the editor, then pressing Ctrl-Space.

12.2.9 How to Create a JSF Form for Entity Data

You can use the Form from Entity dialog box to generate a JSF form that contains fields for all properties contained in an entity class. You must already have a JSF managed bean created to handle any user data associated with the form.

If you use this dialog without having an associated managed bean, you can enter a name for the managed bean in the dialog, and that name will be used in the page regardless of whether it is valid or not. You can then create a managed bean using the IDE's Managed Bean wizard, or if you use the JSF Pages from Entity Classes wizard, managed beans are generated for all selected entity classes.

To generate a JSF form from an entity class:

  1. Open any Facelets page in the editor.

  2. Make sure that you have declared the JSF tag libraries in the page's root element (typically the <html> tag), as shown in Example 12-6.

    Example 12-6 JSF Tag Libraries Declared

    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    
  3. Either type 'jsf' in the editor, press Ctrl-Space, then select the JSF Form from Entity option, or, in the Palette, click and drag the JSF Form from Entity component into the desired location in the editor and release the mouse button. The JSF Form from Entity dialog box opens.

  4. In the Entity Bean drop-down, select the entity class that you want to generate the form from. The IDE scans the project for all entity classes and makes them accessible from the drop-down list.

  5. In the Managed Bean Property field, select a property. The property you choose must return an instance of the entity bean you selected in the Entity Bean drop-down. (This is typically a getter method for the given property.)

  6. (Optional) Select the Generate read only view option to create a form that contains read-only fields. When this option is selected, the IDE applies <h:outputText> tags for form fields, whereas <h:inputText> tags are applied when the option is not selected.

  7. Click OK. The IDE generates code for your Facelets page. For example, a Customer entity class containing a customerId property is displayed as shown in Example 12-7.

    Example 12-7 Customer Entity Class

    <f:view>
      <h:form>
        <h1><h:outputText value="Create/Edit"/></h1>
        <h:panelGrid columns="2">
        <h:outputLabel value="CustomerId:" for="customerId" />
        <h:inputText id="customerId" value="#{customerController.selected.customerId}" title="CustomerId" required="true" requiredMessage="The CustomerId field is required."/>
         ...
        [ Other fields added here. ]
         ...
        </h:panelGrid>
      </h:form>
    </f:view>
    
  8. Optionally, remove any of the entries for columns that you do not want to display in your Facelets page.

You can customize the template that specifies the generated code for the JSF Form from Entity dialog. To do so, click the Customize Template link in the dialog, and in the template file make any changes to better suit your work patterns. Save the file. The next time you use the dialog, the changes you made to the template will appear in the generated code.

12.2.10 How to Generate a JSF Data Table from an Entity Class

You can use the Data Table from Entity dialog to generate a JSF data table that contains columns for all properties contained in an entity class. In order to make use of this facility, you must already have a JSF managed bean created to handle any back-end data associated with the entity class.

If you use this dialog without having an associated managed bean, you can enter a name for the managed bean in the dialog, and that name will be used in the page regardless of whether it is valid or not. You can then create a managed bean using the IDE's Managed Bean wizard, or if you use the JSF Pages from Entity Classes wizard, managed beans are generated for all selected entity classes.

To generate a JSF data table from an entity class:

  1. Open any Facelets page in the editor.

  2. Make sure that you have declared the JSF tag libraries in the page's root element (typically the <html> tag), as shown in Example 12-8.

    Example 12-8 JSF Tag Libraries Declared

    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    
  3. Either type 'jsf' in the editor, press Ctrl-Space, then select the JSF Data Table from Entity option, or, in the Palette, click and drag the JSF Data Table from Entity component into the desired location in the editor and release the mouse button. The JSF Table from Entity dialog box opens.

  4. In the Entity Bean drop-down list, select the entity class that you want to generate the data table from. The IDE scans the project for all entity classes and makes them accessible from the drop-down list.

  5. In the Managed Bean Property field, select a property. The property you choose must return a List of objects that correspond to the entity bean you selected in the Entity Bean drop-down.

  6. Click OK. The IDE generates code for your Facelets page. For example, a managed bean that contains a property that returns a List of Product objects would display similarly to what is shown in Example 12-9.

    Example 12-9 Generated Code for a Managed Bean

    <f:view>
      <h:form>
        <h1><h:outputText value="List"/></h1>
        <h:dataTable value="#{productController.productItems}" var="item">
          <h:column>
            <f:facet name="header">
              <h:outputText value="ProductId"/>
            </f:facet>
            <h:outputText value="#{item.productId}"/>
          </h:column>
           ...
          [ Other columns added here. ]
           ...
        </h:dataTable>
      </h:form>
    </f:view>
    
  7. (Optional) Remove any of the entries for columns that you do not want to display in your Facelets page.

You can customize the template that specifies the generated code for the JSF Form from Entity dialog. To do so, click the Customize Template link in the dialog, and in the template file make any changes to better suit your work patterns. Save the file. The next time you use the dialog, the changes you made to the template will appear in the generated code.

12.2.11 How to Generate JSF Pages from Entity Classes

If you have already generated entity classes from a database in your project, you can use the JSF Pages from Entity Classes wizard to generate a collection of files, classes and front-end pages that enable you to interact with the database. The purpose of the wizard is to provide template code that facilitates CRUD functionality with a back-end data store.

To generate JSF pages from an entity class:

  1. Choose File > New from the main menu.

  2. From the JavaServer Faces category, select JSF Pages from Entity Class and click Next. The wizard displays all of the entity classes in the project.

  3. Specify the entity classes for which you want to generate JSF pages. To do so, click Add (or Add All) to move entity classes to the Selected Entity Classes list.

  4. Click Next.

  5. In Step 3: Generate JSF Pages and Classes, specify a name for the following fields:

    • Session Bean Package. This package contains the session facades for the entity classes.

    • JSF Classes Package. This package contains JSF session-scoped, managed beans.

    • JSF Pages Folder. This folder contains the Facelets pages generated for the entity classes.

    • Localization Bundle Name. The properties bundle contains all the text used in the Facelets pages as key-value pairs.

  6. Click Finish.

12.3 Working with the Spring Framework

The IDE provides built-in support for the Spring Framework. The Spring Framework libraries (versions 3.2 and 2.5) are packaged with the IDE and are available in the Ant Library Manager. They can also be automatically added to the project classpath during project creation. Support for Spring XML bean configuration files is also provided. The configuration files are managed by the following functional components:

The following functionality is provided for Spring bean configuration files:

Spring Web MVC Support integrates the IDE's Spring Framework 3.2 core support for Java applications, and adds functional support for developing MVC-structured web applications by including the following features:

For more information on the Spring Framework, see the following resources:

The IDE provides Spring support for various Java-based applications, such as Java SE, EJB, and web applications. You can add Spring support to a project by either adding the Spring Framework 3.2 library to a project's classpath, or by creating a new Spring configuration file using the Spring XML Configuration File wizard. Along with creating the configuration file, this wizard will also offer to add the Spring library to the classpath if it has not already been added.

To add the Spring Framework library to a project classpath:

  1. In the Projects window, right-click the Libraries node and choose Add Library.

  2. In the Add Libraries dialog, select the Spring Framework 3.2.0 library and click Add Library.

The Spring Framework libraries are added to the project classpath, and they become visible from the project's Libraries node.

12.3.1 How to Create a New Project with Spring MVC Framework Support

The Spring framework libraries and supporting files are bundled with the IDE.

To create a new web application with Spring Web MVC support:

  1. Choose File > New Project. The New Project wizard displays.

  2. Under Categories select Web, then under Projects, select Web Application. Click Next.

  3. Continue through the wizard to Step 4, the Frameworks panel, and select Spring Web MVC 3.2.

  4. Under Spring Web MVC Configuration, specify the dispatcher name and mapping you want to use in the application. Click Finish.

Note:

The JSTL library version 1.1, is bundled by default with the web application. To exclude JSTL, in the Frameworks panel click on the Libraries tab and deselect the Include JSTL option.

Note:

Dispatcher mappings are based on the Java Servlet Specification Section SRV.11.2. In the web application deployment descriptor, the following syntax is used to define standard mappings:

  • A string beginning with a '/' character and ending with a '/*' postfix is used for path mapping.

  • A string beginning with a '*.' prefix is used as an extension mapping.

  • A string containing only the '/' character indicates the default servlet of the application. In this case the servlet path is the requested URI minus the context path and the path info is null.

  • All other strings are used for exact matches only.

12.3.2 How to Add Spring MVC Framework Support to an Existing Application

The Spring framework libraries and supporting files are bundled with the IDE.

To add Spring Web MVC support to an existing web application:

  1. In the Projects window, right-click a web application project node and choose Properties.

  2. Under Categories, click Frameworks. Then under Used Frameworks click the Add button.

  3. In the Add a Framework dialog that displays, select Spring Web MVC and click OK.

  4. Optionally specify any configuration settings, such as the default dispatcher name and mapping. You can also specify the version of Spring libraries to add to your classpath (3.2 or 2.5). Click OK to exit the Project Properties dialog.

Spring framework libraries and artifacts are added to the selected project, such as applicationContext.xml and ${dispatcher}-servlet, where ${dispatcher} is the user-specified dispatcher name.

12.3.3 How to Create a Spring Configuration File

The IDE provides support for managing Spring XML configuration files. You can use the New Spring XML Configuration File wizard to create a configuration file.

To create a new Spring configuration file:

  1. Right-click on a project node in the Projects window and choose New > Other.

  2. This step depends on the type of your project. For a web project, select the Spring category under Categories. For a Java SE or EJB project select the Other category. Then under File Types, select Spring XML Configuration File. Click Next.

  3. In the Name and Location panel, specify a name and location for the file. To specify a location, you can type in a path relative to your project in the Folder text field. Otherwise, click Browse to navigate to a specific location. If you leave the text field blank, the file will be created in the top-most directory.

  4. If you have already added configuration file groups to your project, you can optionally add the new configuration file to any of those groups under Add File to Configuration File Group(s).

  5. Add namespaces to the configuration file. The namespaces displayed are the standard set provided by the Spring Framework library. Click Finish.

The IDE generates the new configuration file and opens it in the Source Editor.

After the file is generated based on your settings, you can manage it and other configuration files by accessing the Spring Framework category in the Project Properties window (Choose Properties from a project's right-click menu).

12.3.4 How to Organize Spring Configuration Files

The IDE provides support for organizing Spring configuration files. This support allows you to specify all your configuration files and organize them into groups, so that they can refer to each other in features such as code completion and hyperlinking. The Spring Configuration Group panel, located in the Project Properties window, enables you to organize Spring configuration files.

The Spring Framework category contains two tabs: Configuration Files and Configuration File Groups. The Configuration Files tab lists all the Spring configuration files in a project, and the Configuration File Groups lists all configuration file groups.

12.3.4.1 Accessing the Spring Configuration Group Panel

To access the Spring Configuration Group panel:

  1. Make sure the Spring support has been enabled, for example by adding the Spring Framework library to the project.

  2. Right-click on the project node in the Projects window and choose Properties.

  3. Under Categories, select Spring Framework.

12.3.4.2 Specifying Spring Configuration Files

Select the Configuration Files tab. You can use the Add File and Remove File buttons to add and remove files from the list, or you can use the Detect Files button to let the Spring support try to autodetect the configuration files.

Note:

Files created using the New Spring XML Configuration File wizard are added to this list automatically by the wizard.

Note:

If you do not mention a configuration file here, it will be ignored by the Spring features that process all Spring configuration files in the project, such as refactoring. Features which work in single files, such as code completion, will continue to work. It is good practice to specify all your configuration file in this list.

12.3.4.3 Organizing Spring Configuration Files into Groups

Groups are useful for setting up parent/child relationships between configuration files. For example, in a web application is typical to have a applicationContext.xml file containing business logic service beans, and a spring-servlet.xml file containing Spring Web MVC beans such as controllers. The beans in spring-servlet.xml are often initialized with service beans from applicationContext.xml, and this fact needs to be reflected in features such as code completion and hyperlinking. In order to set up such a relationship, you would create a group containing these two files.

Select the Configuration File Groups tab. You can use the Add Group, and Remove Group button to add and remove groups. After adding a group, you can add/remove files to/from it using the Add Files and Remove Files buttons.

12.4 Working with the Struts Framework

The IDE provides built-in support for developing MVC-structured web applications with Struts 1.3.10. Functional support includes the following features:

For more information on the Struts Framework, see the following resources:

12.4.1 How to Create a New Project with Struts Framework Support

The Struts tag libraries and supporting files, such as the struts-config.xml file, are bundled with the IDE.

To create a new application with Struts support:

  1. Choose File > New Project.

  2. From the Java Web category, select Web Application.

  3. Continue with the wizard until you reach the Frameworks panel. Select Struts and click Finish. The new project is generated and opens in the IDE.

After you add Struts support, you can use Struts-specific wizards for your project, including the New Struts Action Wizard and the New Struts ActionForm Bean Wizard. Right-click the struts-config.xml file in the Projects window to access Struts-specific wizards.

12.4.2 How to Add Struts Framework Support to an Existing Application

The Struts tag libraries and supporting files, such as the struts-config.xml file, are bundled with the IDE.

To add Struts support to an existing web application:

  1. In the Projects window, right-click a standard web application project's node and choose Properties.

  2. Click Frameworks and select Struts.

  3. Click OK to confirm your selection and exit the Project Properties dialog box.

After you add Struts support, you can use Struts-specific wizards for your project, including the New Struts Action Wizard and the New Struts ActionForm Bean Wizard.

12.5 Working with the Hibernate Framework

Hibernate is a framework that enables your applications to interact with databases using Object Relational Mapping (ORM). The IDE provides Hibernate support for various Java-based applications, such as Java SE, EJB, and web applications.

The IDE has wizards for generating the following files in your application:

The IDE also offers support for editing the following Hibernate files and queries:

For more about Hibernate features and using Hibernate in your application, see the following site at http://www.hibernate.org/.

The following steps outline the basic process of working with Hibernate:

  1. Create a project and open a database connection in the Services window.

  2. Configure the project:

    1. Create a Hibernate configuration file.

    2. Edit the configuration file.

    3. Create the Hibernate utility class.

  3. Create Classes by doing either of the following:

    • Create individual POJOs and mapping files.

    • Use the POJOs and Hibernate mapping files from database wizard.

  4. Code Application:

    1. Create HQL queries.

    2. Edit source files.

12.5.1 How to Create a New Project with Hibernate Support

The Hibernate libraries and wizards to generate supporting files are bundled with the IDE.

To create a new web application with Hibernate support:

  1. Choose File > New Project. The New Project wizard displays.

  2. Under Categories select Java Web, then under Projects, select Web Application. Click Next.

  3. Continue through the wizard to Step 4, the Frameworks panel, and select Hibernate 3.6.10.

  4. Under the Hibernate 3.6.10 Configuration section of the panel, choose the database connection for the application from the drop down list. Click Finish.

When you click Finish the IDE creates the Hibernate configuration file hibernate.cfg.xml in a source package with the name <default package>. The wizard also adds the Hibernate library to the project classpath. After you create a web application with Hibernate support, you can use wizards to generate Hibernate mapping files, a helper file and POJOs from a database.

12.5.2 How to Add Hibernate Support to an Existing Application

The IDE provides Hibernate support for various Java-based applications, such as Java SE, EJB, and web applications. You can add Hibernate support to a project by either adding the Hibernate library to a project's classpath, or by creating a new Hibernate configuration file using the Hibernate Configuration File wizard. Along with creating the configuration file, this wizard will automatically add the Hibernate library to the classpath if it has not already been added.

Note:

If you are creating a web application project with the New Project wizard you can add Hibernate support to the web application by selecting Hibernate in the Frameworks page of the wizard.

To add the Hibernate library to a project classpath:

  1. In the Projects window, right-click the Libraries node and choose Add Library.

  2. In the Add Libraries dialog, select the Hibernate library and click Add Library.

The Hibernate support libraries are added to the project classpath, and they become visible from the project's Libraries node.

12.5.3 How to Create the Hibernate Configuration File

To use Hibernate your application must have a Hibernate configuration file that contains the database connection details enabling the application to connect to a database. The file also contains information about the location of Hibernate mapping files. You can edit this file if you need to change the database connection details. You can have multiple Hibernate configuration files, but the default name for the configuration file used by Hibernate is hibernate.cfg.xml.

A Hibernate configuration file is created automatically if you create a web application and you select the Hibernate framework in the New Project wizard.

You can also create a Hibernate configuration file using the New File wizard if you want to use Hibernate in your application, for example in a Java SE application.

To create hibernate.cfg.xml using the New File wizard:

  1. Open the New File wizard.

  2. Select Hibernate from the Categories list and Hibernate Configuration Wizard from the File Types list. Click Next.

  3. Type hibernate.cfg for the name and specify a location. Click Next.

  4. Select a Database Connection from the drop down list. Click Finish.

When you click Finish, the file hibernate.cfg.xml is created in the location you specified and the file opens in the Source Editor. If you did not specify a location the file is created in a source package with the name <default package>. The wizard also adds the Hibernate library to the classpath if it has not already been added.

12.5.4 How to Edit the Hibernate Configuration File

The hibernate.cfg.xml editor enables you edit the configuration properties for an application that uses the Hibernate framework. You can edit the hibernate.cfg.xml file in a visual editor or edit the XML directly. In the visual editor you can use the Add, Edit and Remove buttons to open dialog boxes to modify the properties. In the XML editor you can use the IDE's code completion to assist you.

You open the editor by expanding the project's Source Packages node in the Projects window and double-clicking the hibernate.cfg.xml file located under the <default package> node.

Many of the properties in the configuration file are supplied and modified by other wizards in the IDE. For example, if you create Hibernate mapping files using the Hibernate Mapping File wizard, hibernate.cfg.xml is automatically updated with information about the mapping files.

The hibernate.cfg.xml visual editor contains the following sections for editing the configuration settings:

  • JDBC Properties. This section enables you to set the JDBC connection information for your datasource, including the JDBC driver and the location of your datasource.

  • Datasource Properties. This section enables you to set the datasource properties, including the username and password for accessing the datasource.

  • Mappings. This section enables you to specify the Hibernate mapping files (.hbm.xml) used in the application.

  • Cache. This section enables you to add Class and Collection Cache properties.

  • Events. This section enables you to specify the listener classes for handling Hibernate session events.

  • Optional Properties. This section enables you to configure optional Hibernate properties such as JDBC connection properties, Hibernate SQL dialects and transaction properties.

  • Security. This section enables you to configure security roles.

You can also edit any of the properties in the XML editor and use code completion to set the properties and values.

12.5.5 How to Create Hibernate Mapping Files

Hibernate mapping files are XML files that contain object relational mapping (ORM) data that Hibernate uses to determine how classes are mapped to table columns and primary keys.

You can use the following wizards to generate Hibernate mapping files:

  • Hibernate Mapping Wizard. This wizard generates a single mapping file for a specified class. After you create the mapping file you need to edit the mapping file in the XML editor to map the fields in the class to the corresponding columns in the table. You can use the IDE's code completion feature to help you edit the mapping file.

  • Hibernate Mapping Files and POJOs from Database. If you use this wizard you can set the option for the IDE to automatically generate Hibernate mapping files for the tables you select in the wizard. The wizard automatically modifies the Hibernate configuration file to specify the Hibernate mapping files.

To create a Hibernate mapping file for a class:

  1. Right-click the package node containing the class to be mapped and choose New > Other to open the New File wizard.

  2. Select Hibernate from the Categories list and Hibernate Mapping Wizard from the File Types list. Click Next.

  3. Type <CLASS_NAME>.hbm for the file name, where <CLASS_NAME> is the name of the class that you want to map. Click Next.

  4. Specify the Class that you want to map to a table.

    You can click the button to locate the class.

  5. Choose the corresponding database table from the drop down list. Click Finish.

When you click Finish, the mapping file is created in the source package you specified and the file opens in the Source Editor. The Hibernate configuration file is modified to specify the mapping file.

12.5.6 How to Generate Hibernate Mapping Files and POJOs from a Database

In addition to writing Hibernate mapping files and POJOs (Plain Old Java Objects) from scratch, you can also generate the mapping files and POJOs from a connected database with the Hibernate Mapping Files and POJOs from Database wizard.

To generate Hibernate mapping files and POJOs from a database:

  1. Right-click the project node in the Projects window and choose New > Other to open the New File wizard.

  2. In the New File wizard, select Hibernate Mapping Files and POJOs from Database from the Hibernate category.

  3. Select the Configuration File (hibernate.cfg.xml) and Reverse Engineering File (hibernate.reveng) from the dropdown lists.

    Note:

    The configuration file contains the database connection details. If a connection to the database is not open you may be prompted to supply a username and password to connect to the database. The database must be running.

  4. Specify any General Settings options for the generated files.

  5. Select Domain Code to generate the POJOs.

  6. Select Hibernate XML Mappings to generate the mapping files.

  7. Select an existing package from the Package drop down list or type the name of a new package.

  8. Click Finish.

When you click Finish the IDE generates the following files:

  • POJOs based on the selected specified tables (if you selected Domain Code in the wizard)

  • Hibernate mapping files based on the specified tables (if you selected Hibernate XML Mappings in the wizard)

  • A hibernate.reveng reverse engineering file in the specified location

12.5.7 How to Create a Hibernate Reverse Engineering File

To use the Hibernate Mapping Files and POJOs from a Database wizard, you need to first create a Hibernate reverse engineering file (hibernate.reveng.xml). The IDE provides a wizard for generating the file based on the data contained in the Hibernate configuration file and the tables you are using in your application.

The reverse engineering file is an XML file that can be used to modify the default settings used when generating Hibernate files from the metadata of the database specified in hibernate.cfg.xml. The file can be used to explicitly specify the database schema that is used, to filter out tables that should not be used and how JDBC types are mapped to Hibernate types.

To create hibernate.reveng.xml using the New File wizard:

  1. Open the New File wizard.

  2. Select Hibernate from the Categories list and Hibernate Reverse Engineering Wizard from the File Types list. Click Next.

  3. Type hibernate.reveng for the name and specify the Folder where you want to save the file. Click Next.

  4. Select the Configuration File from the drop down list.

    Note:

    The configuration file contains the database connection details. If a connection to the database is not open you may be prompted to supply a username and password to connect to the database. The database must be running.

  5. Select any tables in the Available Tables pane and click the Add to move the tables to the Selected Tables pane. Any tables related to the tables you select are automatically added to the list in the Selected Tables pane.

  6. Click Finish.

When you click Finish, the file hibernate.reveng.xml is created in the location you specified and the file opens in the Source Editor.

12.5.8 How to Create a Hibernate Utility Helper File

To use Hibernate you need to create a helper class that handles startup and that accesses Hibernate's SessionFactory to obtain a Session object so that you can load and store objects. The helper class calls configure() and loads the hibernate.cfg.xml configuration file. The helper class then builds the SessionFactory to obtain the Session object.

To create the HibernateUtil.java helper class:

  1. In the Projects window, right-click the Source Packages node and choose New > Other to open the New File wizard.

  2. Select Hibernate from the Categories list and HibernateUtil.java from the File Types list. Click Next.

  3. Type HibernateUtil for the class name. Click Finish.

When you click Finish, the file HibernateUtil.java opens in the Source Editor. You do not need to edit the file.

12.5.9 How to Create and Execute a HQL Statement or Script

You can use the Hibernate Query Language (HQL) Editor to write, edit and execute HQL queries within the IDE. After you create and test the HQL query in the editor you can copy the query to the source code and mapping files. You can right-click in the query editor window to select, copy and paste queries.

The syntax of the HQL query language is very similar to the syntax used in SQL queries but HQL is fully object-oriented and can use objects and properties to represent SQL queries. The syntax of HQL queries is not dependent upon the database the queries are run against. The HQL queries are converted at runtime to the SQL query appropriate for the database. In addition to supporting standard clauses and aggregate functions, HQL also supports polymorphic queries and subqueries.

For more about Hibernate Query Language, see Hibernate documentation at http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html.

To create and execute a HQL statement or script:

  1. Expand the <default packages> node under the Source Packages node in the Projects window.

  2. Right-click the hibernate.cfg.xml configuration file and choose Run HQL Query to open the HQL Editor.

  3. Enter the HQL query in the top pane of the editor window.

  4. Click Run HQL (Run SQL) in the toolbar. Alternatively, you can right-click in the query editor window and choose Run HQL from the popup menu.

When you execute an HQL query, you see the result of the query in the bottom pane of the HQL editor window. The HQL query is executed against the database specified in the project's Hibernate configuration file (hibernate.cfg.xml) that is selected in the editor toolbar. You can click the SQL button above the bottom pane to view the SQL query that corresponds to your HQL query.

12.6 Working with the Grails Framework

Grails aims to bring the "coding by convention" paradigm to Groovy. It's an open-source web application framework that leverages the Groovy language and complements Java Web development.

The IDE's support for Grails encompasses the following features:

Grails ships with a lot of command line functionality out of the box. The IDE provides menu items and other tools that invoke the related scripts:

12.6.1 How to Create a New Project with Grails Framework Support

The Grails framework libraries and supporting files are bundled with the IDE.

To create a new web application with Grails support:

  1. Choose Tools > Options > Miscellaneous and set your Grails installation directory in the Grails tab.

  2. Choose File > New Project. The New Project wizard displays.

  3. Under Categories select Groovy, then under Projects, select Grails Application. Click Next.

  4. Set a project name and location. Click Finish.

The IDE creates a new Grails application and displays its structure in the Projects window.

12.6.2 How to Install a Grails Plugin into a Grails Application

You can use the IDE to install plugins into a Grails application. The plugins can be obtained from the online Grails plugin repository, as well as locally from disk.

To install a plugin into a Grails application:

  1. Right-click a Grails application and choose Plugins.

  2. Select one of the following:

    • Installed. Shows the plugins currently installed in the application. Select an installed plugin and click Uninstall to uninstall it.

    • New plugins. Click Reload Plugins to let the Plugins Manager access the online Grails repository and display the results in the New Plugins tab. Select a plugin and click Install to install the plugin. Alternatively, browse to a Grails plugin locally using the Browse button.

  3. Click Close.

If you installed a new Grails plugin, you can view it by opening the Files window (Ctrl-2) and then expanding the "plugins" folder.