Using Web Fragments with Web Projects

This tutorial shows you the mechanism for creating a web fragment project with an associated web project. You are also provided with example projects to examine. In this tutorial, you will learn how to:

  • Create a web fragment and associated web project
  • Deploy and test a sample web project that includes a web fragment

Duration Time: 20 Minutes

Start Tutorial Now

Don't have MyEclipse? Download Now


1. Create a Web Fragment Project

The web fragment you create will be included in a new dynamic Web application, which you will create along with the Web Fragment project.

  1. SelectFile>New>Project, select Web Fragment Project, and click Next.
  2. Type webFrag into the Project name field.
  3. Accept the default target runtime, or select the runtime for your deployment server.
  4. Select the Add project to a Dynamic Web project checkbox, and click New to create a host Web project.

    Note: This tutorial uses the default JavaEE 6 version; however, JavaEE 7 is an available option when creating new projects.

    Creating a new Web Fragment project
  5.  Type webProj in the Name field, and click Finish to create the Web project.
  6. Click Finish to complete the Web Fragment project.
    FieldDescription

    Project name

    The project's name. Must be a valid Eclipse Java project name. 

    Location

    Check the checkbox to specify a custom file-system location for the new project and its resources.

    JavaEE version

    Specifies the JavaEE specification compliance level. Please ensure that you select the proper specification level for the JavaEE features your application requires and that your target deployment application server(s) support this specification level. Check with your application server vendor for information regarding Java EE specification level compliance.

    Add Maven support

    Enabling this option includes Maven project management support to your project. See Using Maven in MyEclipse for more information.

    Target RuntimeA run time is selected by default based on your chosen JavaEE version. A runtime must be selected for various containers (Spring, JPA, Struts, etc.) to appear on your project’s build path. You can change the runtime on the Runtime tab of the Project Properties> Project Facets page.
    Dynamic Web ProjectAllows you to add the web fragment to an existing Web project or create a new Web project.

    Completing the wizard creates and configures the webFrag Web Fragment project as well as the new Web project. The image below depicts the structure of the new project.

    Notice the JRE System Library folder in both projects. These are a libraries of JavaEE API Jars provided by MyEclipse to your project for compilation and code completion support. These API JAR files are placed only in the project's build path.

    Note: No JavaEE JAR files or other resources are physically copied into your project.

    Web Fragment and associated Web project

    If you create a Web Fragment project to be used with existing Web projects, you must define the dependent Web Fragment project through the existing Web project's Deployment Assembly property. To access Deployment Assembly properties, right-click the target Web project, and select Properties. Expand MyEclipse, select Deployment Assembly, and click Add. See Configuring a Dependent Java Project and MyEclipse Deployment Assembly for more details.

    Note: If you specify a dynamic Web project when creating a Web Fragment project, the dependency is automatically included in the deployment assembly.

    2. Deploy a Web Project

    When deploying a Web project that references a Web Fragment project, you deploy the Web project. Because the web fragment is referenced, it is deployed with the hosting Web project. In this section, you can try out the deployment of a sample Web project that references a web fragment.

    1. Download webfragment.zip, and import the existing project into MyEclipse. You will have two projects - WebFragmentHost and WebFragment.
    2. Right-click the MyEclipse Tomcat server in the Servers view, and select Add/Remove Deployments.

      Note: This project does not run on Tomcat 6.

      Deploying a project from the right-click context menu
    3. Select the WebFragmentHost project in the Available column, and click Add.

      Deploying to the MyEclipse Tomcat Server

    This deploys the WebFragmentHost project to the MyEclipse Tomcat work area in exploded WAR format.  The context-root under which the project is deployed is /WebFragmentHost and is accessible at http://localhost-name:8080/WebFragmentHost. Recall that the context-root is a user-defined Web project property. Access web properties by right-clicking the project, selecting Properties from the menu, expanding MyEclipse, and choosing Web.

    3. Test the Application

    1. Right-click WebFragmentHost, and select Run As>MyEclipse Server Application.
    2. Select the MyEclipse Tomcat server, and click OK. The WelcomeServlet.java file from the web fragment displays a page in the browser.

      Viewing the Web Fragment Host project in the web browser