facebook

Using Web Fragments with Web Projects

This tutorial shows you the mechanism for creating a web fragment project with an associated web project in MyEclipse. 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

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. Select File>New>Web Fragment Project.
  2. Type webFrag into the Project name field and select your Java EE version. Accept the default target runtime, or select the runtime for your deployment server.
    2a. Create a Web Project to Host the Web Fragment
    1. Select the Add project to a Dynamic Web project checkbox, and click New to create a host Web project.
    2. Type webProj in the Name field, ensure you use the same Java EE version chosen for the fragment project.
    3. Click Finish to create the Web project.

    Creating a new Web Fragment project

    Following is a brief description of each field:

    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.
    Java EE 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 (Java EE 7 and lower)—Enabling this option includes Maven project management support to your project. See Using Maven in MyEclipse for more information. For Java EE 8 and higher versions, MyEclipse will always create Maven projects.
    Target Runtime—A 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 Project—Allows you to add the web fragment to an existing Web project or create a new Web project.

  3. Click Finish to complete the Web Fragment project.

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


Web Fragment 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>Deployment>Deployment Assembly This menu option was updated in MyEclipse 2017. For prior versions, click here. , 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, this dependency is automatically included in the web project’s deployment assembly.

2. Deploy and Test Web Fragment

To deploy a Web Fragment project, you must deploy the hosting web project. Because the web fragment is referenced, it is deployed with the host 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 WebFragmentHost, and select Run As>MyEclipse Server Application.
  3. Select the MyEclipse Tomcat server, and click Finish. The WelcomeServlet.java file from the web fragment displays in your browser.
    Deploying to the MyEclipse Tomcat Server
    Servlet Web Fragment Example in the browser
This wizard first 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:8080/WebFragmentHost. The context-root is a user-defined Web project property. Access web properties by right-clicking the project, selecting Properties>Project Facets>Web.